test_lgb.plot.interpretation.R 2.84 KB
Newer Older
1
2
3
4
VERBOSITY <- as.integer(
    Sys.getenv("LIGHTGBM_TEST_VERBOSITY", "-1")
)

5
6
.sigmoid <- function(x) {
    1.0 / (1.0 + exp(-x))
7
}
8
9
.logit <- function(x) {
    log(x / (1.0 - x))
10
11
12
13
14
15
}

test_that("lgb.plot.interepretation works as expected for binary classification", {
    data(agaricus.train, package = "lightgbm")
    train <- agaricus.train
    dtrain <- lgb.Dataset(train$data, label = train$label)
16
    set_field(
17
        dataset = dtrain
18
19
        , field_name = "init_score"
        , data = rep(
20
21
22
23
24
25
26
27
28
            .logit(mean(train$label))
            , length(train$label)
        )
    )
    data(agaricus.test, package = "lightgbm")
    test <- agaricus.test
    params <- list(
        objective = "binary"
        , learning_rate = 0.01
29
30
31
32
        , num_leaves = 63L
        , max_depth = -1L
        , min_data_in_leaf = 1L
        , min_sum_hessian_in_leaf = 1.0
33
        , verbosity = VERBOSITY
34
35
36
37
    )
    model <- lgb.train(
        params = params
        , data = dtrain
38
        , nrounds = 3L
39
    )
40
    num_trees <- 5L
41
42
43
    tree_interpretation <- lgb.interprete(
        model = model
        , data = test$data
44
        , idxset = seq_len(num_trees)
45
46
47
    )
    expect_true({
        lgb.plot.interpretation(
48
49
            tree_interpretation_dt = tree_interpretation[[1L]]
            , top_n = 5L
50
51
52
53
54
55
        )
        TRUE
    })

    # should also work when you explicitly pass cex
    plot_res <- lgb.plot.interpretation(
56
57
        tree_interpretation_dt = tree_interpretation[[1L]]
        , top_n = 5L
58
59
60
61
62
63
64
65
66
67
68
        , cex = 0.95
    )
    expect_null(plot_res)
})

test_that("lgb.plot.interepretation works as expected for multiclass classification", {
    data(iris)

    # We must convert factors to numeric
    # They must be starting from number 0 to use multiclass
    # For instance: 0, 1, 2, 3, 4, 5...
69
    iris$Species <- as.numeric(as.factor(iris$Species)) - 1L
70
71

    # Create imbalanced training data (20, 30, 40 examples for classes 0, 1, 2)
72
    train <- as.matrix(iris[c(1L:20L, 51L:80L, 101L:140L), ])
73
    # The 10 last samples of each class are for validation
74
75
76
    test <- as.matrix(iris[c(41L:50L, 91L:100L, 141L:150L), ])
    dtrain <- lgb.Dataset(data = train[, 1L:4L], label = train[, 5L])
    dtest <- lgb.Dataset.create.valid(dtrain, data = test[, 1L:4L], label = test[, 5L])
77
78
79
    params <- list(
        objective = "multiclass"
        , metric = "multi_logloss"
80
        , num_class = 3L
81
        , learning_rate = 0.00001
82
        , min_data = 1L
83
84
85
86
    )
    model <- lgb.train(
        params = params
        , data = dtrain
87
        , nrounds = 3L
88
        , verbose = VERBOSITY
89
    )
90
    num_trees <- 5L
91
92
    tree_interpretation <- lgb.interprete(
        model = model
93
94
        , data = test[, 1L:4L]
        , idxset = seq_len(num_trees)
95
96
    )
    plot_res <- lgb.plot.interpretation(
97
98
        tree_interpretation_dt = tree_interpretation[[1L]]
        , top_n = 5L
99
100
101
    )
    expect_null(plot_res)
})