Skip to content

Commit

Permalink
[R-package] Added tests on creating a Booster from a Dataset (#3007)
Browse files Browse the repository at this point in the history
  • Loading branch information
jameslamb authored Apr 25, 2020
1 parent 4667d50 commit 22d6d1f
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions R-package/tests/testthat/test_lgb.Booster.R
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,55 @@ test_that("If a string and a file are both passed to lgb.load() the file is used
pred2 <- predict(bst2, test$data)
expect_identical(pred, pred2)
})

context("Booster")

test_that("Creating a Booster from a Dataset should work", {
set.seed(708L)
data(agaricus.train, package = "lightgbm")
data(agaricus.test, package = "lightgbm")
dtrain <- lgb.Dataset(
agaricus.train$data
, label = agaricus.train$label
)
bst <- Booster$new(
params = list(
objective = "binary"
),
train_set = dtrain
)
expect_true(lgb.is.Booster(bst))
expect_equal(bst$current_iter(), 0L)
expect_true(is.na(bst$best_score))
expect_true(all(bst$predict(agaricus.train$data) == 0.5))
})

test_that("Creating a Booster from a Dataset with an existing predictor should work", {
set.seed(708L)
data(agaricus.train, package = "lightgbm")
nrounds <- 2L
bst <- lightgbm(
data = as.matrix(agaricus.train$data)
, label = agaricus.train$label
, num_leaves = 4L
, learning_rate = 1.0
, nrounds = nrounds
, objective = "binary"
)
data(agaricus.test, package = "lightgbm")
dtest <- Dataset$new(
data = agaricus.test$data
, label = agaricus.test$label
, predictor = bst$to_predictor()
)
bst_from_ds <- Booster$new(
train_set = dtest
)
expect_true(lgb.is.Booster(bst))
expect_equal(bst$current_iter(), nrounds)
expect_equal(bst$eval_train()[[1L]][["value"]], 0.1115352)
expect_equal(bst_from_ds$current_iter(), nrounds)
dumped_model <- jsonlite::fromJSON(bst$dump_model())
expect_identical(bst_from_ds$eval_train(), list())
expect_equal(bst_from_ds$current_iter(), nrounds)
})

0 comments on commit 22d6d1f

Please sign in to comment.