test_basic.R 2.99 KB
Newer Older
Guolin Ke's avatar
Guolin Ke committed
1
2
3
4
5
6
7
8
9
10
11
context("basic functions")

data(agaricus.train, package='lightgbm')
data(agaricus.test, package='lightgbm')
train <- agaricus.train
test <- agaricus.test

windows_flag = grepl('Windows', Sys.info()[['sysname']])

test_that("train and predict binary classification", {
  nrounds = 10
12
  bst <- lightgbm(data = train$data, label = train$label, num_leaves = 5,
Guolin Ke's avatar
Guolin Ke committed
13
14
15
16
17
18
    nrounds = nrounds, objective = "binary", metric="binary_error")
  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)
19
20
  expect_equal(length(pred), 1611)

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


test_that("train and predict softmax", {
  lb <- as.numeric(iris$Species) - 1

  bst <- lightgbm(data = as.matrix(iris[, -5]), label = lb,
                 num_leaves = 4, learning_rate = 0.1, nrounds = 20, min_data=20, min_hess=20,
                 objective = "multiclass", metric="multi_error", num_class=3)

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

40
41
  pred <- predict(bst, as.matrix(iris[, -5]))
  expect_equal(length(pred), nrow(iris) * 3)
Guolin Ke's avatar
Guolin Ke committed
42
43
44
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
})


test_that("use of multiple eval metrics works", {
  bst <- lightgbm(data = train$data, label = train$label, num_leaves = 4,
                learning_rate=1, nrounds = 10, objective = "binary",
                metric = list("binary_error","auc","binary_logloss") )
  expect_false(is.null(bst$record_evals))
})


test_that("training continuation works", {
  dtrain <- lgb.Dataset(train$data, label = train$label, free_raw_data=FALSE)
  watchlist = list(train=dtrain)
  param <- list(objective = "binary", metric="binary_logloss", num_leaves = 5, learning_rate = 1)

  # for the reference, use 10 iterations at once:
  bst <- lgb.train(param, dtrain, nrounds = 10, watchlist)
  err_bst <- lgb.get.eval.result(bst, "train", "binary_logloss", 10)
  # first 5 iterations:
  bst1 <- lgb.train(param, dtrain, nrounds = 5, watchlist)
  # test continuing from a model in file
  lgb.save(bst1, "lightgbm.model")
  # continue for 5 more:
  bst2 <- lgb.train(param, dtrain, nrounds = 5, watchlist, init_model = bst1)
  err_bst2 <- lgb.get.eval.result(bst2, "train", "binary_logloss", 10)
  expect_lt(abs(err_bst - err_bst2), 0.01)

  bst2 <- lgb.train(param, dtrain, nrounds = 5, watchlist, init_model = "lightgbm.model")
  err_bst2 <- lgb.get.eval.result(bst2, "train", "binary_logloss", 10)
  expect_lt(abs(err_bst - err_bst2), 0.01)
})

Guolin Ke's avatar
Guolin Ke committed
75
76
77
78

test_that("cv works", {
  dtrain <- lgb.Dataset(train$data, label=train$label)
  params <- list(objective="regression", metric="l2,l1")
79
  bst <- lgb.cv(params, dtrain, 10, nfold=5, min_data=1, learning_rate=1, early_stopping_rounds=10)
Guolin Ke's avatar
Guolin Ke committed
80
81
  expect_false(is.null(bst$record_evals))
})