[R] Move all DMatrix fields to function arguments (#9862)

This commit is contained in:
david-cortes
2023-12-09 19:45:28 +01:00
committed by GitHub
parent 1094d6015d
commit 562352101d
10 changed files with 236 additions and 68 deletions

View File

@@ -56,7 +56,7 @@ test_that("parameter validation works", {
y <- d[, "x1"] + d[, "x2"]^2 +
ifelse(d[, "x3"] > .5, d[, "x3"]^2, 2^d[, "x3"]) +
rnorm(10)
dtrain <- xgb.DMatrix(data = d, info = list(label = y), nthread = n_threads)
dtrain <- xgb.DMatrix(data = d, label = y, nthread = n_threads)
correct <- function() {
params <- list(
@@ -124,7 +124,7 @@ test_that("dart prediction works", {
expect_false(all(matrix(pred_by_xgboost_0, byrow = TRUE) == matrix(pred_by_xgboost_2, byrow = TRUE)))
set.seed(1994)
dtrain <- xgb.DMatrix(data = d, info = list(label = y), nthread = n_threads)
dtrain <- xgb.DMatrix(data = d, label = y, nthread = n_threads)
booster_by_train <- xgb.train(
params = list(
booster = "dart",
@@ -186,7 +186,7 @@ test_that("train and predict softprob", {
x3 = rnorm(100)
)
y <- sample.int(10, 100, replace = TRUE) - 1
dtrain <- xgb.DMatrix(data = d, info = list(label = y), nthread = n_threads)
dtrain <- xgb.DMatrix(data = d, label = y, nthread = n_threads)
booster <- xgb.train(
params = list(tree_method = "hist", nthread = n_threads),
data = dtrain, nrounds = 4, num_class = 10,
@@ -643,3 +643,28 @@ test_that("Can use multi-output labels with custom objectives", {
expect_true(cor(y, pred[, 1]) > 0.9)
expect_true(cor(y, pred[, 2]) < -0.9)
})
test_that("Can use ranking objectives with either 'qid' or 'group'", {
set.seed(123)
x <- matrix(rnorm(100 * 10), nrow = 100)
y <- sample(2, size = 100, replace = TRUE) - 1
qid <- c(rep(1, 20), rep(2, 20), rep(3, 60))
gr <- c(20, 20, 60)
dmat_qid <- xgb.DMatrix(x, label = y, qid = qid)
dmat_gr <- xgb.DMatrix(x, label = y, group = gr)
params <- list(tree_method = "hist",
lambdarank_num_pair_per_sample = 8,
objective = "rank:ndcg",
lambdarank_pair_method = "topk",
nthread = n_threads)
set.seed(123)
model_qid <- xgb.train(params, dmat_qid, nrounds = 5)
set.seed(123)
model_gr <- xgb.train(params, dmat_gr, nrounds = 5)
pred_qid <- predict(model_qid, x)
pred_gr <- predict(model_gr, x)
expect_equal(pred_qid, pred_gr)
})

View File

@@ -305,3 +305,20 @@ test_that("xgb.DMatrix: error on three-dimensional array", {
dim(y) <- c(50, 4, 2)
expect_error(xgb.DMatrix(data = x, label = y))
})
test_that("xgb.DMatrix: can get group for both 'qid' and 'group' constructors", {
set.seed(123)
x <- matrix(rnorm(1000), nrow = 100)
group <- c(20, 20, 60)
qid <- c(rep(1, 20), rep(2, 20), rep(3, 60))
gr_mat <- xgb.DMatrix(x, group = group)
qid_mat <- xgb.DMatrix(x, qid = qid)
info_gr <- getinfo(gr_mat, "group")
info_qid <- getinfo(qid_mat, "group")
expect_equal(info_gr, info_qid)
expected_gr <- c(0, 20, 40, 100)
expect_equal(info_gr, expected_gr)
})

View File

@@ -47,7 +47,7 @@ test_that("interaction constraints scientific representation", {
d <- matrix(rexp(rows, rate = .1), nrow = rows, ncol = cols)
y <- rnorm(rows)
dtrain <- xgb.DMatrix(data = d, info = list(label = y), nthread = n_threads)
dtrain <- xgb.DMatrix(data = d, label = y, nthread = n_threads)
inc <- list(c(seq.int(from = 0, to = cols, by = 1)))
with_inc <- xgb.train(