test_parameters.R 2.6 KB
Newer Older
1
2
3
4
5

context("feature penalties")

data(agaricus.train, package = 'lightgbm')
data(agaricus.test, package = 'lightgbm')
6
7
8
9
10
11
12
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)
13

14
15
16
17
  bst <- lapply(seq(1, 0, by = -0.1), function(x) {
    feature_penalties <- rep(1, ncol(train$data))
    feature_penalties[var_index] <- x
    lightgbm(
18
19
20
21
22
23
24
25
26
      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
27
28
    )
  })
29

30
31
32
  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])
33

34
35
36
37
  # 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))
38

39
40
41
  expect_lt(min(diff(unlist(var_gain))), 0)
  expect_lt(min(diff(unlist(var_cover))), 0)
  expect_lt(min(diff(unlist(var_freq))), 0)
42

43
44
  # Ensure that feature is not used when feature_penalty = 0
  expect_length(var_gain[[length(var_gain)]], 0)
45
})
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77

expect_true(".PARAMETER_ALIASES() returns a named list", {
  param_aliases <- .PARAMETER_ALIASES()
  expect_true(is.list(param_aliases))
  expect_true(is.character(names(param_aliases)))
  expect_true(is.character(param_aliases[["boosting"]]))
  expect_true(is.character(param_aliases[["early_stopping_round"]]))
  expect_true(is.character(param_aliases[["metric"]]))
  expect_true(is.character(param_aliases[["num_class"]]))
  expect_true(is.character(param_aliases[["num_iterations"]]))
})

expect_true("training should warn if you use 'dart' boosting, specified with 'boosting' or aliases", {
  for (boosting_param in .PARAMETER_ALIASES()[["boosting"]]){
    expect_warning({
      result <- lightgbm(
        data = train$data
        , label = train$label
        , num_leaves = 5
        , learning_rate = 0.05
        , nrounds = 5
        , objective = "binary"
        , metric = "binary_error"
        , verbose = -1
        , params = stats::setNames(
          object = "dart"
          , nm = boosting_param
        )
      )
    }, regexp = "Early stopping is not available in 'dart' mode")
  }
})