Commit 2e93cdab authored by rgranvil's avatar rgranvil Committed by Qiwei Ye
Browse files

Add R package test cases for feature penalties (#1484)

* Fix lgb.Dataset construct issue with 1:n sequence in R v 3.5

* Add tests for feature penalties
parent 2a598c8c
...@@ -234,7 +234,7 @@ Dataset <- R6Class( ...@@ -234,7 +234,7 @@ Dataset <- R6Class(
handle <- lgb.call("LGBM_DatasetGetSubset_R", handle <- lgb.call("LGBM_DatasetGetSubset_R",
ret = handle, ret = handle,
ref_handle, ref_handle,
private$used_indices, c(private$used_indices), # Adding c() fixes issue in R v3.5
length(private$used_indices), length(private$used_indices),
params_str) params_str)
......
data(agaricus.train, package='lightgbm')
data(agaricus.test, package='lightgbm')
train <- agaricus.train
test <- agaricus.test
test_that("Feature penalties work properly", {
# Fit a series of models with varying penalty on most important variable
var_name <- "odor=none"
var_index <- which(train$data@Dimnames[[2]] == var_name)
bst <- lapply(seq(1, 0, by = -0.1), function(x) {
feature_penalties <- rep(1, ncol(train$data))
feature_penalties[var_index] <- x
lightgbm(
data = train$data,
label = train$label,
num_leaves = 5,
learning_rate = 0.05,
nrounds = 20,
objective = "binary",
feature_penalty = paste0(feature_penalties, collapse = ","),
metric="binary_error",
verbose = -1
)
})
var_gain <- lapply(bst, function(x) lgb.importance(x)[Feature == var_name, Gain])
var_cover <- lapply(bst, function(x) lgb.importance(x)[Feature == var_name, Cover])
var_freq <- lapply(bst, function(x) lgb.importance(x)[Feature == var_name, Frequency])
# Ensure that feature gain, cover, and frequency decreases with stronger penalties
expect_true(all(diff(unlist(var_gain)) <= 0))
expect_true(all(diff(unlist(var_cover)) <= 0))
expect_true(all(diff(unlist(var_freq)) <= 0))
expect_lt(min(diff(unlist(var_gain))), 0)
expect_lt(min(diff(unlist(var_cover))), 0)
expect_lt(min(diff(unlist(var_freq))), 0)
# Ensure that feature is not used when feature_penalty = 0
expect_length(var_gain[[length(var_gain)]], 0)
})
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment