[R] Replace xgboost() with xgb.train() in most tests and examples (#9941)

This commit is contained in:
david-cortes 2024-01-02 14:20:01 +01:00 committed by GitHub
parent 32cbab1cc0
commit 9e33a10202
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
27 changed files with 156 additions and 150 deletions

View File

@ -383,8 +383,9 @@ NULL
#' #'
#' @examples #' @examples
#' data(agaricus.train, package='xgboost') #' data(agaricus.train, package='xgboost')
#' bst <- xgboost(data = agaricus.train$data, label = agaricus.train$label, max_depth = 2, #' bst <- xgb.train(data = xgb.DMatrix(agaricus.train$data, label = agaricus.train$label),
#' eta = 1, nthread = 2, nrounds = 2, objective = "binary:logistic") #' max_depth = 2, eta = 1, nthread = 2, nrounds = 2,
#' objective = "binary:logistic")
#' #'
#' # Save as a stand-alone file; load it with xgb.load() #' # Save as a stand-alone file; load it with xgb.load()
#' xgb.save(bst, 'xgb.model') #' xgb.save(bst, 'xgb.model')

View File

@ -272,9 +272,8 @@ xgb.Booster.complete <- function(object, saveraw = TRUE) {
#' train <- agaricus.train #' train <- agaricus.train
#' test <- agaricus.test #' test <- agaricus.test
#' #'
#' bst <- xgboost( #' bst <- xgb.train(
#' data = train$data, #' data = xgb.DMatrix(train$data, label = train$label),
#' label = train$label,
#' max_depth = 2, #' max_depth = 2,
#' eta = 0.5, #' eta = 0.5,
#' nthread = nthread, #' nthread = nthread,
@ -316,9 +315,8 @@ xgb.Booster.complete <- function(object, saveraw = TRUE) {
#' #'
#' set.seed(11) #' set.seed(11)
#' #'
#' bst <- xgboost( #' bst <- xgb.train(
#' data = as.matrix(iris[, -5]), #' data = xgb.DMatrix(as.matrix(iris[, -5]), label = lb),
#' label = lb,
#' max_depth = 4, #' max_depth = 4,
#' eta = 0.5, #' eta = 0.5,
#' nthread = 2, #' nthread = 2,
@ -341,9 +339,8 @@ xgb.Booster.complete <- function(object, saveraw = TRUE) {
#' # compare with predictions from softmax: #' # compare with predictions from softmax:
#' set.seed(11) #' set.seed(11)
#' #'
#' bst <- xgboost( #' bst <- xgb.train(
#' data = as.matrix(iris[, -5]), #' data = xgb.DMatrix(as.matrix(iris[, -5]), label = lb),
#' label = lb,
#' max_depth = 4, #' max_depth = 4,
#' eta = 0.5, #' eta = 0.5,
#' nthread = 2, #' nthread = 2,

View File

@ -29,8 +29,10 @@
#' #'
#' train <- agaricus.train #' train <- agaricus.train
#' test <- agaricus.test #' test <- agaricus.test
#' bst <- xgboost( #' bst <- xgb.train(
#' data = train$data, label = train$label, max_depth = 2, eta = 1, #' data = xgb.DMatrix(train$data, label = train$label),
#' max_depth = 2,
#' eta = 1,
#' nthread = nthread, #' nthread = nthread,
#' nrounds = 2, #' nrounds = 2,
#' objective = "binary:logistic" #' objective = "binary:logistic"

View File

@ -32,8 +32,10 @@
#' #'
#' train <- agaricus.train #' train <- agaricus.train
#' test <- agaricus.test #' test <- agaricus.test
#' bst <- xgboost( #' bst <- xgb.train(
#' data = train$data, label = train$label, max_depth = 2, eta = 1, #' data = xgb.DMatrix(train$data, label = train$label),
#' max_depth = 2,
#' eta = 1,
#' nthread = nthread, #' nthread = nthread,
#' nrounds = 2, #' nrounds = 2,
#' objective = "binary:logistic" #' objective = "binary:logistic"

View File

@ -23,8 +23,8 @@
#' #'
#' train <- agaricus.train #' train <- agaricus.train
#' test <- agaricus.test #' test <- agaricus.test
#' bst <- xgboost(data = train$data, label = train$label, max_depth = 2, #' bst <- xgb.train(data = xgb.DMatrix(train$data, label = train$label), max_depth = 2,
#' eta = 1, nthread = nthread, nrounds = 2,objective = "binary:logistic") #' eta = 1, nthread = nthread, nrounds = 2,objective = "binary:logistic")
#' #'
#' raw <- xgb.save.raw(bst) #' raw <- xgb.save.raw(bst)
#' bst <- xgb.load.raw(raw) #' bst <- xgb.load.raw(raw)

View File

@ -9,8 +9,8 @@
#' data(agaricus.test, package='xgboost') #' data(agaricus.test, package='xgboost')
#' train <- agaricus.train #' train <- agaricus.train
#' test <- agaricus.test #' test <- agaricus.test
#' bst <- xgboost(data = train$data, label = train$label, max_depth = 2, #' bst <- xgb.train(data = xgb.DMatrix(train$data, label = train$label), max_depth = 2,
#' eta = 1, nthread = 2, nrounds = 2,objective = "binary:logistic") #' eta = 1, nthread = 2, nrounds = 2,objective = "binary:logistic")
#' raw <- xgb.serialize(bst) #' raw <- xgb.serialize(bst)
#' bst <- xgb.unserialize(raw) #' bst <- xgb.unserialize(raw)
#' #'

View File

@ -81,8 +81,8 @@ output_vector <- df[, Y := 0][Improved == "Marked", Y := 1][, Y]
# Following is the same process as other demo # Following is the same process as other demo
cat("Learning...\n") cat("Learning...\n")
bst <- xgboost(data = sparse_matrix, label = output_vector, max_depth = 9, bst <- xgb.train(data = xgb.DMatrix(sparse_matrix, label = output_vector), max_depth = 9,
eta = 1, nthread = 2, nrounds = 10, objective = "binary:logistic") eta = 1, nthread = 2, nrounds = 10, objective = "binary:logistic")
importance <- xgb.importance(feature_names = colnames(sparse_matrix), model = bst) importance <- xgb.importance(feature_names = colnames(sparse_matrix), model = bst)
print(importance) print(importance)

View File

@ -74,26 +74,26 @@ cols2ids <- function(object, col_names) {
interaction_list_fid <- cols2ids(interaction_list, colnames(train)) interaction_list_fid <- cols2ids(interaction_list, colnames(train))
# Fit model with interaction constraints # Fit model with interaction constraints
bst <- xgboost(data = train, label = y, max_depth = 4, bst <- xgb.train(data = xgb.DMatrix(train, label = y), max_depth = 4,
eta = 0.1, nthread = 2, nrounds = 1000, eta = 0.1, nthread = 2, nrounds = 1000,
interaction_constraints = interaction_list_fid) interaction_constraints = interaction_list_fid)
bst_tree <- xgb.model.dt.tree(colnames(train), bst) bst_tree <- xgb.model.dt.tree(colnames(train), bst)
bst_interactions <- treeInteractions(bst_tree, 4) bst_interactions <- treeInteractions(bst_tree, 4)
# interactions constrained to combinations of V1*V2 and V3*V4*V5 # interactions constrained to combinations of V1*V2 and V3*V4*V5
# Fit model without interaction constraints # Fit model without interaction constraints
bst2 <- xgboost(data = train, label = y, max_depth = 4, bst2 <- xgb.train(data = xgb.DMatrix(train, label = y), max_depth = 4,
eta = 0.1, nthread = 2, nrounds = 1000) eta = 0.1, nthread = 2, nrounds = 1000)
bst2_tree <- xgb.model.dt.tree(colnames(train), bst2) bst2_tree <- xgb.model.dt.tree(colnames(train), bst2)
bst2_interactions <- treeInteractions(bst2_tree, 4) # much more interactions bst2_interactions <- treeInteractions(bst2_tree, 4) # much more interactions
# Fit model with both interaction and monotonicity constraints # Fit model with both interaction and monotonicity constraints
bst3 <- xgboost(data = train, label = y, max_depth = 4, bst3 <- xgb.train(data = xgb.DMatrix(train, label = y), max_depth = 4,
eta = 0.1, nthread = 2, nrounds = 1000, eta = 0.1, nthread = 2, nrounds = 1000,
interaction_constraints = interaction_list_fid, interaction_constraints = interaction_list_fid,
monotone_constraints = c(-1, 0, 0, 0, 0, 0, 0, 0, 0, 0)) monotone_constraints = c(-1, 0, 0, 0, 0, 0, 0, 0, 0, 0))
bst3_tree <- xgb.model.dt.tree(colnames(train), bst3) bst3_tree <- xgb.model.dt.tree(colnames(train), bst3)
bst3_interactions <- treeInteractions(bst3_tree, 4) bst3_interactions <- treeInteractions(bst3_tree, 4)

View File

@ -1,6 +1,6 @@
data(mtcars) data(mtcars)
head(mtcars) head(mtcars)
bst <- xgboost(data = as.matrix(mtcars[, -11]), label = mtcars[, 11], bst <- xgb.train(data = xgb.DMatrix(as.matrix(mtcars[, -11]), label = mtcars[, 11]),
objective = 'count:poisson', nrounds = 5) objective = 'count:poisson', nrounds = 5)
pred <- predict(bst, as.matrix(mtcars[, -11])) pred <- predict(bst, as.matrix(mtcars[, -11]))
sqrt(mean((pred - mtcars[, 11]) ^ 2)) sqrt(mean((pred - mtcars[, 11]) ^ 2))

View File

@ -33,8 +33,9 @@ For more details and explanation about model persistence and archival, consult t
} }
\examples{ \examples{
data(agaricus.train, package='xgboost') data(agaricus.train, package='xgboost')
bst <- xgboost(data = agaricus.train$data, label = agaricus.train$label, max_depth = 2, bst <- xgb.train(data = xgb.DMatrix(agaricus.train$data, label = agaricus.train$label),
eta = 1, nthread = 2, nrounds = 2, objective = "binary:logistic") max_depth = 2, eta = 1, nthread = 2, nrounds = 2,
objective = "binary:logistic")
# Save as a stand-alone file; load it with xgb.load() # Save as a stand-alone file; load it with xgb.load()
xgb.save(bst, 'xgb.model') xgb.save(bst, 'xgb.model')

View File

@ -136,9 +136,8 @@ data.table::setDTthreads(nthread)
train <- agaricus.train train <- agaricus.train
test <- agaricus.test test <- agaricus.test
bst <- xgboost( bst <- xgb.train(
data = train$data, data = xgb.DMatrix(train$data, label = train$label),
label = train$label,
max_depth = 2, max_depth = 2,
eta = 0.5, eta = 0.5,
nthread = nthread, nthread = nthread,
@ -180,9 +179,8 @@ num_class <- 3
set.seed(11) set.seed(11)
bst <- xgboost( bst <- xgb.train(
data = as.matrix(iris[, -5]), data = xgb.DMatrix(as.matrix(iris[, -5]), label = lb),
label = lb,
max_depth = 4, max_depth = 4,
eta = 0.5, eta = 0.5,
nthread = 2, nthread = 2,
@ -205,9 +203,8 @@ sum(pred_labels != lb) / length(lb)
# compare with predictions from softmax: # compare with predictions from softmax:
set.seed(11) set.seed(11)
bst <- xgboost( bst <- xgb.train(
data = as.matrix(iris[, -5]), data = xgb.DMatrix(as.matrix(iris[, -5]), label = lb),
label = lb,
max_depth = 4, max_depth = 4,
eta = 0.5, eta = 0.5,
nthread = 2, nthread = 2,

View File

@ -34,8 +34,10 @@ data.table::setDTthreads(nthread)
train <- agaricus.train train <- agaricus.train
test <- agaricus.test test <- agaricus.test
bst <- xgboost( bst <- xgb.train(
data = train$data, label = train$label, max_depth = 2, eta = 1, data = xgb.DMatrix(train$data, label = train$label),
max_depth = 2,
eta = 1,
nthread = nthread, nthread = nthread,
nrounds = 2, nrounds = 2,
objective = "binary:logistic" objective = "binary:logistic"

View File

@ -38,8 +38,10 @@ data.table::setDTthreads(nthread)
train <- agaricus.train train <- agaricus.train
test <- agaricus.test test <- agaricus.test
bst <- xgboost( bst <- xgb.train(
data = train$data, label = train$label, max_depth = 2, eta = 1, data = xgb.DMatrix(train$data, label = train$label),
max_depth = 2,
eta = 1,
nthread = nthread, nthread = nthread,
nrounds = 2, nrounds = 2,
objective = "binary:logistic" objective = "binary:logistic"

View File

@ -32,8 +32,8 @@ data.table::setDTthreads(nthread)
train <- agaricus.train train <- agaricus.train
test <- agaricus.test test <- agaricus.test
bst <- xgboost(data = train$data, label = train$label, max_depth = 2, bst <- xgb.train(data = xgb.DMatrix(train$data, label = train$label), max_depth = 2,
eta = 1, nthread = nthread, nrounds = 2,objective = "binary:logistic") eta = 1, nthread = nthread, nrounds = 2,objective = "binary:logistic")
raw <- xgb.save.raw(bst) raw <- xgb.save.raw(bst)
bst <- xgb.load.raw(raw) bst <- xgb.load.raw(raw)

View File

@ -21,8 +21,8 @@ data(agaricus.train, package='xgboost')
data(agaricus.test, package='xgboost') data(agaricus.test, package='xgboost')
train <- agaricus.train train <- agaricus.train
test <- agaricus.test test <- agaricus.test
bst <- xgboost(data = train$data, label = train$label, max_depth = 2, bst <- xgb.train(data = xgb.DMatrix(train$data, label = train$label), max_depth = 2,
eta = 1, nthread = 2, nrounds = 2,objective = "binary:logistic") eta = 1, nthread = 2, nrounds = 2,objective = "binary:logistic")
raw <- xgb.serialize(bst) raw <- xgb.serialize(bst)
bst <- xgb.unserialize(raw) bst <- xgb.unserialize(raw)

View File

@ -16,10 +16,11 @@ n_threads <- 1
test_that("train and predict binary classification", { test_that("train and predict binary classification", {
nrounds <- 2 nrounds <- 2
expect_output( expect_output(
bst <- xgboost( bst <- xgb.train(
data = train$data, label = train$label, max_depth = 2, data = xgb.DMatrix(train$data, label = train$label), max_depth = 2,
eta = 1, nthread = n_threads, nrounds = nrounds, eta = 1, nthread = n_threads, nrounds = nrounds,
objective = "binary:logistic", eval_metric = "error" objective = "binary:logistic", eval_metric = "error",
watchlist = list(train = xgb.DMatrix(train$data, label = train$label))
), ),
"train-error" "train-error"
) )
@ -104,9 +105,8 @@ test_that("dart prediction works", {
rnorm(100) rnorm(100)
set.seed(1994) set.seed(1994)
booster_by_xgboost <- xgboost( booster_by_xgboost <- xgb.train(
data = d, data = xgb.DMatrix(d, label = y),
label = y,
max_depth = 2, max_depth = 2,
booster = "dart", booster = "dart",
rate_drop = 0.5, rate_drop = 0.5,
@ -151,10 +151,11 @@ test_that("train and predict softprob", {
lb <- as.numeric(iris$Species) - 1 lb <- as.numeric(iris$Species) - 1
set.seed(11) set.seed(11)
expect_output( expect_output(
bst <- xgboost( bst <- xgb.train(
data = as.matrix(iris[, -5]), label = lb, data = xgb.DMatrix(as.matrix(iris[, -5]), label = lb),
max_depth = 3, eta = 0.5, nthread = n_threads, nrounds = 5, max_depth = 3, eta = 0.5, nthread = n_threads, nrounds = 5,
objective = "multi:softprob", num_class = 3, eval_metric = "merror" objective = "multi:softprob", num_class = 3, eval_metric = "merror",
watchlist = list(train = xgb.DMatrix(as.matrix(iris[, -5]), label = lb))
), ),
"train-merror" "train-merror"
) )
@ -201,10 +202,11 @@ test_that("train and predict softmax", {
lb <- as.numeric(iris$Species) - 1 lb <- as.numeric(iris$Species) - 1
set.seed(11) set.seed(11)
expect_output( expect_output(
bst <- xgboost( bst <- xgb.train(
data = as.matrix(iris[, -5]), label = lb, data = xgb.DMatrix(as.matrix(iris[, -5]), label = lb),
max_depth = 3, eta = 0.5, nthread = n_threads, nrounds = 5, max_depth = 3, eta = 0.5, nthread = n_threads, nrounds = 5,
objective = "multi:softmax", num_class = 3, eval_metric = "merror" objective = "multi:softmax", num_class = 3, eval_metric = "merror",
watchlist = list(train = xgb.DMatrix(as.matrix(iris[, -5]), label = lb))
), ),
"train-merror" "train-merror"
) )
@ -222,11 +224,12 @@ test_that("train and predict RF", {
set.seed(11) set.seed(11)
lb <- train$label lb <- train$label
# single iteration # single iteration
bst <- xgboost( bst <- xgb.train(
data = train$data, label = lb, max_depth = 5, data = xgb.DMatrix(train$data, label = lb), max_depth = 5,
nthread = n_threads, nthread = n_threads,
nrounds = 1, objective = "binary:logistic", eval_metric = "error", nrounds = 1, objective = "binary:logistic", eval_metric = "error",
num_parallel_tree = 20, subsample = 0.6, colsample_bytree = 0.1 num_parallel_tree = 20, subsample = 0.6, colsample_bytree = 0.1,
watchlist = list(train = xgb.DMatrix(train$data, label = lb))
) )
expect_equal(bst$niter, 1) expect_equal(bst$niter, 1)
expect_equal(xgb.ntree(bst), 20) expect_equal(xgb.ntree(bst), 20)
@ -248,12 +251,13 @@ test_that("train and predict RF with softprob", {
lb <- as.numeric(iris$Species) - 1 lb <- as.numeric(iris$Species) - 1
nrounds <- 15 nrounds <- 15
set.seed(11) set.seed(11)
bst <- xgboost( bst <- xgb.train(
data = as.matrix(iris[, -5]), label = lb, data = xgb.DMatrix(as.matrix(iris[, -5]), label = lb),
max_depth = 3, eta = 0.9, nthread = n_threads, nrounds = nrounds, max_depth = 3, eta = 0.9, nthread = n_threads, nrounds = nrounds,
objective = "multi:softprob", eval_metric = "merror", objective = "multi:softprob", eval_metric = "merror",
num_class = 3, verbose = 0, num_class = 3, verbose = 0,
num_parallel_tree = 4, subsample = 0.5, colsample_bytree = 0.5 num_parallel_tree = 4, subsample = 0.5, colsample_bytree = 0.5,
watchlist = list(train = xgb.DMatrix(as.matrix(iris[, -5]), label = lb))
) )
expect_equal(bst$niter, 15) expect_equal(bst$niter, 15)
expect_equal(xgb.ntree(bst), 15 * 3 * 4) expect_equal(xgb.ntree(bst), 15 * 3 * 4)
@ -271,10 +275,11 @@ test_that("train and predict RF with softprob", {
test_that("use of multiple eval metrics works", { test_that("use of multiple eval metrics works", {
expect_output( expect_output(
bst <- xgboost( bst <- xgb.train(
data = train$data, label = train$label, max_depth = 2, data = xgb.DMatrix(train$data, label = train$label), max_depth = 2,
eta = 1, nthread = n_threads, nrounds = 2, objective = "binary:logistic", eta = 1, nthread = n_threads, nrounds = 2, objective = "binary:logistic",
eval_metric = "error", eval_metric = "auc", eval_metric = "logloss" eval_metric = "error", eval_metric = "auc", eval_metric = "logloss",
watchlist = list(train = xgb.DMatrix(train$data, label = train$label))
), ),
"train-error.*train-auc.*train-logloss" "train-error.*train-auc.*train-logloss"
) )
@ -282,10 +287,11 @@ test_that("use of multiple eval metrics works", {
expect_equal(dim(bst$evaluation_log), c(2, 4)) expect_equal(dim(bst$evaluation_log), c(2, 4))
expect_equal(colnames(bst$evaluation_log), c("iter", "train_error", "train_auc", "train_logloss")) expect_equal(colnames(bst$evaluation_log), c("iter", "train_error", "train_auc", "train_logloss"))
expect_output( expect_output(
bst2 <- xgboost( bst2 <- xgb.train(
data = train$data, label = train$label, max_depth = 2, data = xgb.DMatrix(train$data, label = train$label), max_depth = 2,
eta = 1, nthread = n_threads, nrounds = 2, objective = "binary:logistic", eta = 1, nthread = n_threads, nrounds = 2, objective = "binary:logistic",
eval_metric = list("error", "auc", "logloss") eval_metric = list("error", "auc", "logloss"),
watchlist = list(train = xgb.DMatrix(train$data, label = train$label))
), ),
"train-error.*train-auc.*train-logloss" "train-error.*train-auc.*train-logloss"
) )
@ -361,7 +367,7 @@ test_that("xgb.cv works", {
expect_is(cv, "xgb.cv.synchronous") expect_is(cv, "xgb.cv.synchronous")
expect_false(is.null(cv$evaluation_log)) expect_false(is.null(cv$evaluation_log))
expect_lt(cv$evaluation_log[, min(test_error_mean)], 0.03) expect_lt(cv$evaluation_log[, min(test_error_mean)], 0.03)
expect_lt(cv$evaluation_log[, min(test_error_std)], 0.008) expect_lt(cv$evaluation_log[, min(test_error_std)], 0.0085)
expect_equal(cv$niter, 2) expect_equal(cv$niter, 2)
expect_false(is.null(cv$folds) && is.list(cv$folds)) expect_false(is.null(cv$folds) && is.list(cv$folds))
expect_length(cv$folds, 5) expect_length(cv$folds, 5)
@ -391,8 +397,8 @@ test_that("xgb.cv works with stratified folds", {
test_that("train and predict with non-strict classes", { test_that("train and predict with non-strict classes", {
# standard dense matrix input # standard dense matrix input
train_dense <- as.matrix(train$data) train_dense <- as.matrix(train$data)
bst <- xgboost( bst <- xgb.train(
data = train_dense, label = train$label, max_depth = 2, data = xgb.DMatrix(train_dense, label = train$label), max_depth = 2,
eta = 1, nthread = n_threads, nrounds = 2, objective = "binary:logistic", eta = 1, nthread = n_threads, nrounds = 2, objective = "binary:logistic",
verbose = 0 verbose = 0
) )
@ -402,8 +408,8 @@ test_that("train and predict with non-strict classes", {
class(train_dense) <- "shmatrix" class(train_dense) <- "shmatrix"
expect_true(is.matrix(train_dense)) expect_true(is.matrix(train_dense))
expect_error( expect_error(
bst <- xgboost( bst <- xgb.train(
data = train_dense, label = train$label, max_depth = 2, data = xgb.DMatrix(train_dense, label = train$label), max_depth = 2,
eta = 1, nthread = n_threads, nrounds = 2, objective = "binary:logistic", eta = 1, nthread = n_threads, nrounds = 2, objective = "binary:logistic",
verbose = 0 verbose = 0
), ),
@ -416,8 +422,8 @@ test_that("train and predict with non-strict classes", {
class(train_dense) <- c("pphmatrix", "shmatrix") class(train_dense) <- c("pphmatrix", "shmatrix")
expect_true(is.matrix(train_dense)) expect_true(is.matrix(train_dense))
expect_error( expect_error(
bst <- xgboost( bst <- xgb.train(
data = train_dense, label = train$label, max_depth = 2, data = xgb.DMatrix(train_dense, label = train$label), max_depth = 2,
eta = 1, nthread = n_threads, nrounds = 2, objective = "binary:logistic", eta = 1, nthread = n_threads, nrounds = 2, objective = "binary:logistic",
verbose = 0 verbose = 0
), ),
@ -480,8 +486,8 @@ test_that("colsample_bytree works", {
}) })
test_that("Configuration works", { test_that("Configuration works", {
bst <- xgboost( bst <- xgb.train(
data = train$data, label = train$label, max_depth = 2, data = xgb.DMatrix(train$data, label = train$label), max_depth = 2,
eta = 1, nthread = n_threads, nrounds = 2, objective = "binary:logistic", eta = 1, nthread = n_threads, nrounds = 2, objective = "binary:logistic",
eval_metric = "error", eval_metric = "auc", eval_metric = "logloss" eval_metric = "error", eval_metric = "auc", eval_metric = "logloss"
) )
@ -521,8 +527,8 @@ test_that("strict_shape works", {
y <- as.numeric(iris$Species) - 1 y <- as.numeric(iris$Species) - 1
X <- as.matrix(iris[, -5]) X <- as.matrix(iris[, -5])
bst <- xgboost( bst <- xgb.train(
data = X, label = y, data = xgb.DMatrix(X, label = y),
max_depth = 2, nrounds = n_rounds, nthread = n_threads, max_depth = 2, nrounds = n_rounds, nthread = n_threads,
objective = "multi:softprob", num_class = 3, eval_metric = "merror" objective = "multi:softprob", num_class = 3, eval_metric = "merror"
) )
@ -536,8 +542,8 @@ test_that("strict_shape works", {
X <- agaricus.train$data X <- agaricus.train$data
y <- agaricus.train$label y <- agaricus.train$label
bst <- xgboost( bst <- xgb.train(
data = X, label = y, max_depth = 2, nthread = n_threads, data = xgb.DMatrix(X, label = y), max_depth = 2, nthread = n_threads,
nrounds = n_rounds, objective = "binary:logistic", nrounds = n_rounds, objective = "binary:logistic",
eval_metric = "error", eval_metric = "auc", eval_metric = "logloss" eval_metric = "error", eval_metric = "auc", eval_metric = "logloss"
) )
@ -555,8 +561,8 @@ test_that("'predict' accepts CSR data", {
x_csc <- as(X[1L, , drop = FALSE], "CsparseMatrix") x_csc <- as(X[1L, , drop = FALSE], "CsparseMatrix")
x_csr <- as(x_csc, "RsparseMatrix") x_csr <- as(x_csc, "RsparseMatrix")
x_spv <- as(x_csc, "sparseVector") x_spv <- as(x_csc, "sparseVector")
bst <- xgboost( bst <- xgb.train(
data = X, label = y, objective = "binary:logistic", data = xgb.DMatrix(X, label = y), objective = "binary:logistic",
nrounds = 5L, verbose = FALSE, nthread = n_threads, nrounds = 5L, verbose = FALSE, nthread = n_threads,
) )
p_csc <- predict(bst, x_csc) p_csc <- predict(bst, x_csc)

View File

@ -265,14 +265,14 @@ test_that("early stopping works with titanic", {
dtx <- model.matrix(~ 0 + ., data = titanic[, c("Pclass", "Sex")]) dtx <- model.matrix(~ 0 + ., data = titanic[, c("Pclass", "Sex")])
dty <- titanic$Survived dty <- titanic$Survived
xgboost::xgboost( xgboost::xgb.train(
data = dtx, data = xgb.DMatrix(dtx, label = dty),
label = dty,
objective = "binary:logistic", objective = "binary:logistic",
eval_metric = "auc", eval_metric = "auc",
nrounds = 100, nrounds = 100,
early_stopping_rounds = 3, early_stopping_rounds = 3,
nthread = n_threads nthread = n_threads,
watchlist = list(train = xgb.DMatrix(dtx, label = dty))
) )
expect_true(TRUE) # should not crash expect_true(TRUE) # should not crash

View File

@ -6,8 +6,8 @@ test_that("train and prediction when gctorture is on", {
train <- agaricus.train train <- agaricus.train
test <- agaricus.test test <- agaricus.test
gctorture(TRUE) gctorture(TRUE)
bst <- xgboost(data = train$data, label = train$label, max.depth = 2, bst <- xgb.train(data = xgb.DMatrix(train$data, label = train$label), max.depth = 2,
eta = 1, nthread = 2, nrounds = 2, objective = "binary:logistic") eta = 1, nthread = 2, nrounds = 2, objective = "binary:logistic")
pred <- predict(bst, test$data) pred <- predict(bst, test$data)
gctorture(FALSE) gctorture(FALSE)
expect_length(pred, length(test$label)) expect_length(pred, length(test$label))

View File

@ -25,15 +25,15 @@ if (isTRUE(VCD_AVAILABLE)) {
label <- df[, ifelse(Improved == "Marked", 1, 0)] label <- df[, ifelse(Improved == "Marked", 1, 0)]
# binary # binary
bst.Tree <- xgboost(data = sparse_matrix, label = label, max_depth = 9, bst.Tree <- xgb.train(data = xgb.DMatrix(sparse_matrix, label = label), max_depth = 9,
eta = 1, nthread = 2, nrounds = nrounds, verbose = 0, eta = 1, nthread = 2, nrounds = nrounds, verbose = 0,
objective = "binary:logistic", booster = "gbtree", objective = "binary:logistic", booster = "gbtree",
base_score = 0.5) base_score = 0.5)
bst.GLM <- xgboost(data = sparse_matrix, label = label, bst.GLM <- xgb.train(data = xgb.DMatrix(sparse_matrix, label = label),
eta = 1, nthread = 1, nrounds = nrounds, verbose = 0, eta = 1, nthread = 1, nrounds = nrounds, verbose = 0,
objective = "binary:logistic", booster = "gblinear", objective = "binary:logistic", booster = "gblinear",
base_score = 0.5) base_score = 0.5)
feature.names <- colnames(sparse_matrix) feature.names <- colnames(sparse_matrix)
} }
@ -41,13 +41,13 @@ if (isTRUE(VCD_AVAILABLE)) {
# multiclass # multiclass
mlabel <- as.numeric(iris$Species) - 1 mlabel <- as.numeric(iris$Species) - 1
nclass <- 3 nclass <- 3
mbst.Tree <- xgboost(data = as.matrix(iris[, -5]), label = mlabel, verbose = 0, mbst.Tree <- xgb.train(data = xgb.DMatrix(as.matrix(iris[, -5]), label = mlabel), verbose = 0,
max_depth = 3, eta = 0.5, nthread = 2, nrounds = nrounds, max_depth = 3, eta = 0.5, nthread = 2, nrounds = nrounds,
objective = "multi:softprob", num_class = nclass, base_score = 0) objective = "multi:softprob", num_class = nclass, base_score = 0)
mbst.GLM <- xgboost(data = as.matrix(iris[, -5]), label = mlabel, verbose = 0, mbst.GLM <- xgb.train(data = xgb.DMatrix(as.matrix(iris[, -5]), label = mlabel), verbose = 0,
booster = "gblinear", eta = 0.1, nthread = 1, nrounds = nrounds, booster = "gblinear", eta = 0.1, nthread = 1, nrounds = nrounds,
objective = "multi:softprob", num_class = nclass, base_score = 0) objective = "multi:softprob", num_class = nclass, base_score = 0)
test_that("xgb.dump works", { test_that("xgb.dump works", {
@ -71,8 +71,9 @@ test_that("xgb.dump works for gblinear", {
expect_length(xgb.dump(bst.GLM), 14) expect_length(xgb.dump(bst.GLM), 14)
# also make sure that it works properly for a sparse model where some coefficients # also make sure that it works properly for a sparse model where some coefficients
# are 0 from setting large L1 regularization: # are 0 from setting large L1 regularization:
bst.GLM.sp <- xgboost(data = sparse_matrix, label = label, eta = 1, nthread = 2, nrounds = 1, bst.GLM.sp <- xgb.train(data = xgb.DMatrix(sparse_matrix, label = label), eta = 1,
alpha = 2, objective = "binary:logistic", booster = "gblinear") nthread = 2, nrounds = 1,
alpha = 2, objective = "binary:logistic", booster = "gblinear")
d.sp <- xgb.dump(bst.GLM.sp) d.sp <- xgb.dump(bst.GLM.sp)
expect_length(d.sp, 14) expect_length(d.sp, 14)
expect_gt(sum(d.sp == "0"), 0) expect_gt(sum(d.sp == "0"), 0)
@ -168,7 +169,7 @@ test_that("SHAPs sum to predictions, with or without DART", {
nrounds <- 30 nrounds <- 30
for (booster in list("gbtree", "dart")) { for (booster in list("gbtree", "dart")) {
fit <- xgboost( fit <- xgb.train(
params = c( params = c(
list( list(
nthread = 2, nthread = 2,
@ -177,8 +178,7 @@ test_that("SHAPs sum to predictions, with or without DART", {
eval_metric = "rmse"), eval_metric = "rmse"),
if (booster == "dart") if (booster == "dart")
list(rate_drop = .01, one_drop = TRUE)), list(rate_drop = .01, one_drop = TRUE)),
data = d, data = xgb.DMatrix(d, label = y),
label = y,
nrounds = nrounds) nrounds = nrounds)
pr <- function(...) { pr <- function(...) {
@ -360,9 +360,8 @@ test_that("xgb.importance works with and without feature names", {
expect_equal(importance_from_dump(), importance, tolerance = 1e-6) expect_equal(importance_from_dump(), importance, tolerance = 1e-6)
## decision stump ## decision stump
m <- xgboost::xgboost( m <- xgboost::xgb.train(
data = as.matrix(data.frame(x = c(0, 1))), data = xgb.DMatrix(as.matrix(data.frame(x = c(0, 1))), label = c(1, 2)),
label = c(1, 2),
nrounds = 1, nrounds = 1,
base_score = 0.5, base_score = 0.5,
nthread = 2 nthread = 2
@ -393,9 +392,9 @@ test_that("xgb.importance works with GLM model", {
test_that("xgb.model.dt.tree and xgb.importance work with a single split model", { test_that("xgb.model.dt.tree and xgb.importance work with a single split model", {
.skip_if_vcd_not_available() .skip_if_vcd_not_available()
bst1 <- xgboost(data = sparse_matrix, label = label, max_depth = 1, bst1 <- xgb.train(data = xgb.DMatrix(sparse_matrix, label = label), max_depth = 1,
eta = 1, nthread = 2, nrounds = 1, verbose = 0, eta = 1, nthread = 2, nrounds = 1, verbose = 0,
objective = "binary:logistic") objective = "binary:logistic")
expect_error(dt <- xgb.model.dt.tree(model = bst1), regexp = NA) # no error expect_error(dt <- xgb.model.dt.tree(model = bst1), regexp = NA) # no error
expect_equal(nrow(dt), 3) expect_equal(nrow(dt), 3)
expect_error(imp <- xgb.importance(model = bst1), regexp = NA) # no error expect_error(imp <- xgb.importance(model = bst1), regexp = NA) # no error

View File

@ -13,9 +13,9 @@ train <- matrix(c(x1, x2, x3), ncol = 3)
test_that("interaction constraints for regression", { test_that("interaction constraints for regression", {
# Fit a model that only allows interaction between x1 and x2 # Fit a model that only allows interaction between x1 and x2
bst <- xgboost(data = train, label = y, max_depth = 3, bst <- xgb.train(data = xgb.DMatrix(train, label = y), max_depth = 3,
eta = 0.1, nthread = 2, nrounds = 100, verbose = 0, eta = 0.1, nthread = 2, nrounds = 100, verbose = 0,
interaction_constraints = list(c(0, 1))) interaction_constraints = list(c(0, 1)))
# Set all observations to have the same x3 values then increment # Set all observations to have the same x3 values then increment
# by the same amount # by the same amount

View File

@ -98,15 +98,14 @@ test_that("SHAP contribution values are not NAN", {
ivs <- c("x1", "x2") ivs <- c("x1", "x2")
fit <- xgboost( fit <- xgb.train(
verbose = 0, verbose = 0,
params = list( params = list(
objective = "reg:squarederror", objective = "reg:squarederror",
eval_metric = "rmse", eval_metric = "rmse",
nthread = n_threads nthread = n_threads
), ),
data = as.matrix(subset(d, fold == 2)[, ivs]), data = xgb.DMatrix(as.matrix(subset(d, fold == 2)[, ivs]), label = subset(d, fold == 2)$y),
label = subset(d, fold == 2)$y,
nrounds = 3 nrounds = 3
) )
@ -169,9 +168,8 @@ test_that("multiclass feature interactions work", {
test_that("SHAP single sample works", { test_that("SHAP single sample works", {
train <- agaricus.train train <- agaricus.train
test <- agaricus.test test <- agaricus.test
booster <- xgboost( booster <- xgb.train(
data = train$data, data = xgb.DMatrix(train$data, label = train$label),
label = train$label,
max_depth = 2, max_depth = 2,
nrounds = 4, nrounds = 4,
objective = "binary:logistic", objective = "binary:logistic",

View File

@ -7,8 +7,8 @@ test <- agaricus.test
test_that("load/save raw works", { test_that("load/save raw works", {
nrounds <- 8 nrounds <- 8
booster <- xgboost( booster <- xgb.train(
data = train$data, label = train$label, data = xgb.DMatrix(train$data, label = train$label),
nrounds = nrounds, objective = "binary:logistic", nrounds = nrounds, objective = "binary:logistic",
nthread = 2 nthread = 2
) )

View File

@ -7,9 +7,9 @@ train <- matrix(x, ncol = 1)
test_that("monotone constraints for regression", { test_that("monotone constraints for regression", {
bst <- xgboost(data = train, label = y, max_depth = 2, bst <- xgb.train(data = xgb.DMatrix(train, label = y), max_depth = 2,
eta = 0.1, nthread = 2, nrounds = 100, verbose = 0, eta = 0.1, nthread = 2, nrounds = 100, verbose = 0,
monotone_constraints = -1) monotone_constraints = -1)
pred <- predict(bst, train) pred <- predict(bst, train)

View File

@ -10,13 +10,13 @@ dtest <- xgb.DMatrix(
agaricus.test$data, label = agaricus.test$label, nthread = 2 agaricus.test$data, label = agaricus.test$label, nthread = 2
) )
bst <- xgboost(data = dtrain, bst <- xgb.train(data = dtrain,
max_depth = 2, max_depth = 2,
eta = 1, eta = 1,
nrounds = 10, nrounds = 10,
nthread = 1, nthread = 1,
verbose = 0, verbose = 0,
objective = "binary:logistic") objective = "binary:logistic")
test_that("call is exposed to R", { test_that("call is exposed to R", {
expect_false(is.null(bst$call)) expect_false(is.null(bst$call))

View File

@ -4,8 +4,8 @@ set.seed(1994)
test_that("Poisson regression works", { test_that("Poisson regression works", {
data(mtcars) data(mtcars)
bst <- xgboost( bst <- xgb.train(
data = as.matrix(mtcars[, -11]), label = mtcars[, 11], data = xgb.DMatrix(as.matrix(mtcars[, -11]), label = mtcars[, 11]),
objective = 'count:poisson', nrounds = 10, verbose = 0, nthread = 2 objective = 'count:poisson', nrounds = 10, verbose = 0, nthread = 2
) )
expect_equal(class(bst), "xgb.Booster") expect_equal(class(bst), "xgb.Booster")

View File

@ -8,9 +8,9 @@ set.seed(1994)
test_that("Can save and load models with Unicode paths", { test_that("Can save and load models with Unicode paths", {
nrounds <- 2 nrounds <- 2
bst <- xgboost(data = train$data, label = train$label, max_depth = 2, bst <- xgb.train(data = xgb.DMatrix(train$data, label = train$label), max_depth = 2,
eta = 1, nthread = 2, nrounds = nrounds, objective = "binary:logistic", eta = 1, nthread = 2, nrounds = nrounds, objective = "binary:logistic",
eval_metric = "error") eval_metric = "error")
tmpdir <- tempdir() tmpdir <- tempdir()
lapply(c("모델.json", "がうる・ぐら.json", "类继承.ubj"), function(x) { lapply(c("모델.json", "がうる・ぐら.json", "类继承.ubj"), function(x) {
path <- file.path(tmpdir, x) path <- file.path(tmpdir, x)

View File

@ -52,9 +52,8 @@ labels <- c(1, 1, 1,
data <- data.frame(dates = dates, labels = labels) data <- data.frame(dates = dates, labels = labels)
bst <- xgboost( bst <- xgb.train(
data = as.matrix(data$dates), data = xgb.DMatrix(as.matrix(data$dates), label = labels),
label = labels,
nthread = 2, nthread = 2,
nrounds = 1, nrounds = 1,
objective = "binary:logistic", objective = "binary:logistic",