test_basic.R 3.27 KB
Newer Older
Guolin Ke's avatar
Guolin Ke committed
1
2
context("basic functions")

3
4
data(agaricus.train, package = "lightgbm")
data(agaricus.test, package = "lightgbm")
Guolin Ke's avatar
Guolin Ke committed
5
6
7
train <- agaricus.train
test <- agaricus.test

8
windows_flag <- grepl("Windows", Sys.info()[["sysname"]])
Guolin Ke's avatar
Guolin Ke committed
9
10

test_that("train and predict binary classification", {
11
  nrounds <- 10L
12
13
14
  bst <- lightgbm(
    data = train$data
    , label = train$label
15
    , num_leaves = 5L
16
17
18
19
    , nrounds = nrounds
    , objective = "binary"
    , metric = "binary_error"
  )
Guolin Ke's avatar
Guolin Ke committed
20
21
22
23
24
  expect_false(is.null(bst$record_evals))
  record_results <- lgb.get.eval.result(bst, "train", "binary_error")
  expect_lt(min(record_results), 0.02)

  pred <- predict(bst, test$data)
25
  expect_equal(length(pred), 1611L)
26

27
28
29
30
  pred1 <- predict(bst, train$data, num_iteration = 1L)
  expect_equal(length(pred1), 6513L)
  err_pred1 <- sum((pred1 > 0.5) != train$label) / length(train$label)
  err_log <- record_results[1L]
Guolin Ke's avatar
Guolin Ke committed
31
32
33
34
35
  expect_lt(abs(err_pred1 - err_log), 10e-6)
})


test_that("train and predict softmax", {
36
  lb <- as.numeric(iris$Species) - 1L
Guolin Ke's avatar
Guolin Ke committed
37

38
  bst <- lightgbm(
39
    data = as.matrix(iris[, -5L])
40
    , label = lb
41
    , num_leaves = 4L
42
    , learning_rate = 0.1
43
44
45
    , nrounds = 20L
    , min_data = 20L
    , min_hess = 20.0
46
47
    , objective = "multiclass"
    , metric = "multi_error"
48
    , num_class = 3L
49
  )
Guolin Ke's avatar
Guolin Ke committed
50
51
52
53
54

  expect_false(is.null(bst$record_evals))
  record_results <- lgb.get.eval.result(bst, "train", "multi_error")
  expect_lt(min(record_results), 0.03)

55
56
  pred <- predict(bst, as.matrix(iris[, -5L]))
  expect_equal(length(pred), nrow(iris) * 3L)
Guolin Ke's avatar
Guolin Ke committed
57
58
59
60
})


test_that("use of multiple eval metrics works", {
61
62
63
  bst <- lightgbm(
    data = train$data
    , label = train$label
64
65
66
    , num_leaves = 4L
    , learning_rate = 1.0
    , nrounds = 10L
67
    , objective = "binary"
68
    , metric = list("binary_error", "auc", "binary_logloss")
69
  )
Guolin Ke's avatar
Guolin Ke committed
70
71
72
73
74
  expect_false(is.null(bst$record_evals))
})


test_that("training continuation works", {
75
  testthat::skip("This test is currently broken. See issue #2468 for details.")
76
77
78
79
80
  dtrain <- lgb.Dataset(
    train$data
    , label = train$label
    , free_raw_data = FALSE
  )
81
  watchlist <- list(train = dtrain)
82
83
84
  param <- list(
    objective = "binary"
    , metric = "binary_logloss"
85
86
    , num_leaves = 5L
    , learning_rate = 1.0
87
  )
Guolin Ke's avatar
Guolin Ke committed
88
89

  # for the reference, use 10 iterations at once:
90
91
  bst <- lgb.train(param, dtrain, nrounds = 10L, watchlist)
  err_bst <- lgb.get.eval.result(bst, "train", "binary_logloss", 10L)
Guolin Ke's avatar
Guolin Ke committed
92
  # first 5 iterations:
93
  bst1 <- lgb.train(param, dtrain, nrounds = 5L, watchlist)
Guolin Ke's avatar
Guolin Ke committed
94
95
96
  # test continuing from a model in file
  lgb.save(bst1, "lightgbm.model")
  # continue for 5 more:
97
98
  bst2 <- lgb.train(param, dtrain, nrounds = 5L, watchlist, init_model = bst1)
  err_bst2 <- lgb.get.eval.result(bst2, "train", "binary_logloss", 10L)
Guolin Ke's avatar
Guolin Ke committed
99
100
  expect_lt(abs(err_bst - err_bst2), 0.01)

101
102
  bst2 <- lgb.train(param, dtrain, nrounds = 5L, watchlist, init_model = "lightgbm.model")
  err_bst2 <- lgb.get.eval.result(bst2, "train", "binary_logloss", 10L)
Guolin Ke's avatar
Guolin Ke committed
103
104
105
  expect_lt(abs(err_bst - err_bst2), 0.01)
})

Guolin Ke's avatar
Guolin Ke committed
106
107

test_that("cv works", {
108
109
110
111
112
  dtrain <- lgb.Dataset(train$data, label = train$label)
  params <- list(objective = "regression", metric = "l2,l1")
  bst <- lgb.cv(
    params
    , dtrain
113
114
115
116
117
    , 10L
    , nfold = 5L
    , min_data = 1L
    , learning_rate = 1.0
    , early_stopping_rounds = 10L
118
  )
Guolin Ke's avatar
Guolin Ke committed
119
120
  expect_false(is.null(bst$record_evals))
})