| 1 |
library(Matrix) |
| 2 |
context("testing xgb.DMatrix functionality") |
| 3 |
|
| 4 |
data(agaricus.test, package = "xgboost") |
| 5 |
test_data <- agaricus.test$data[1:100, ] |
| 6 |
test_label <- agaricus.test$label[1:100] |
| 7 |
|
| 8 |
n_threads <- 2 |
| 9 |
|
| 10 |
test_that("xgb.DMatrix: basic construction", { |
| 11 |
# from sparse matrix |
| 12 |
dtest1 <- xgb.DMatrix(test_data, label = test_label, nthread = n_threads) |
| 13 |
|
| 14 |
# from dense matrix |
| 15 |
dtest2 <- xgb.DMatrix(as.matrix(test_data), label = test_label, nthread = n_threads) |
| 16 |
expect_equal(getinfo(dtest1, "label"), getinfo(dtest2, "label")) |
| 17 |
expect_equal(dim(dtest1), dim(dtest2)) |
| 18 |
|
| 19 |
# from dense integer matrix |
| 20 |
int_data <- as.matrix(test_data) |
| 21 |
storage.mode(int_data) <- "integer" |
| 22 |
dtest3 <- xgb.DMatrix(int_data, label = test_label, nthread = n_threads) |
| 23 |
expect_equal(dim(dtest1), dim(dtest3)) |
| 24 |
|
| 25 |
n_samples <- 100 |
| 26 |
X <- cbind( |
| 27 |
x1 = sample(x = 4, size = n_samples, replace = TRUE), |
| 28 |
x2 = sample(x = 4, size = n_samples, replace = TRUE), |
| 29 |
x3 = sample(x = 4, size = n_samples, replace = TRUE) |
| 30 |
) |
| 31 |
X <- matrix(X, nrow = n_samples) |
| 32 |
y <- rbinom(n = n_samples, size = 1, prob = 1 / 2) |
| 33 |
|
| 34 |
fd <- xgb.DMatrix(X, label = y, missing = 1, nthread = n_threads) |
| 35 |
|
| 36 |
dgc <- as(X, "dgCMatrix") |
| 37 |
fdgc <- xgb.DMatrix(dgc, label = y, missing = 1.0, nthread = n_threads) |
| 38 |
|
| 39 |
dgr <- as(X, "dgRMatrix") |
| 40 |
fdgr <- xgb.DMatrix(dgr, label = y, missing = 1, nthread = n_threads) |
| 41 |
|
| 42 |
params <- list(tree_method = "hist", nthread = n_threads) |
| 43 |
bst_fd <- xgb.train( |
| 44 |
params, nrounds = 8, fd, evals = list(train = fd), verbose = 0 |
| 45 |
) |
| 46 |
bst_dgr <- xgb.train( |
| 47 |
params, nrounds = 8, fdgr, evals = list(train = fdgr), verbose = 0 |
| 48 |
) |
| 49 |
bst_dgc <- xgb.train( |
| 50 |
params, nrounds = 8, fdgc, evals = list(train = fdgc), verbose = 0 |
| 51 |
) |
| 52 |
|
| 53 |
raw_fd <- xgb.save.raw(bst_fd, raw_format = "ubj") |
| 54 |
raw_dgr <- xgb.save.raw(bst_dgr, raw_format = "ubj") |
| 55 |
raw_dgc <- xgb.save.raw(bst_dgc, raw_format = "ubj") |
| 56 |
|
| 57 |
expect_equal(raw_fd, raw_dgr) |
| 58 |
expect_equal(raw_fd, raw_dgc) |
| 59 |
}) |
| 60 |
|
| 61 |
test_that("xgb.DMatrix: NA", { |
| 62 |
n_samples <- 3 |
| 63 |
x <- cbind( |
| 64 |
x1 = sample(x = 4, size = n_samples, replace = TRUE), |
| 65 |
x2 = sample(x = 4, size = n_samples, replace = TRUE) |
| 66 |
) |
| 67 |
x[1, "x1"] <- NA |
| 68 |
|
| 69 |
m <- xgb.DMatrix(x, nthread = n_threads) |
| 70 |
fname_int <- file.path(tempdir(), "int.dmatrix") |
| 71 |
xgb.DMatrix.save(m, fname_int) |
| 72 |
|
| 73 |
x <- matrix(as.numeric(x), nrow = n_samples, ncol = 2) |
| 74 |
colnames(x) <- c("x1", "x2") |
| 75 |
m <- xgb.DMatrix(x, nthread = n_threads) |
| 76 |
|
| 77 |
fname_float <- file.path(tempdir(), "float.dmatrix") |
| 78 |
xgb.DMatrix.save(m, fname_float) |
| 79 |
|
| 80 |
iconn <- file(fname_int, "rb") |
| 81 |
fconn <- file(fname_float, "rb") |
| 82 |
|
| 83 |
expect_equal(file.size(fname_int), file.size(fname_float)) |
| 84 |
|
| 85 |
bytes <- file.size(fname_int) |
| 86 |
idmatrix <- readBin(iconn, "raw", n = bytes) |
| 87 |
fdmatrix <- readBin(fconn, "raw", n = bytes) |
| 88 |
|
| 89 |
expect_equal(length(idmatrix), length(fdmatrix)) |
| 90 |
expect_equal(idmatrix, fdmatrix) |
| 91 |
|
| 92 |
close(iconn) |
| 93 |
close(fconn) |
| 94 |
|
| 95 |
file.remove(fname_int) |
| 96 |
file.remove(fname_float) |
| 97 |
}) |
| 98 |
|
| 99 |
test_that("xgb.DMatrix: saving, loading", { |
| 100 |
# save to a local file |
| 101 |
dtest1 <- xgb.DMatrix(test_data, label = test_label, nthread = n_threads) |
| 102 |
tmp_file <- tempfile('xgb.DMatrix_') |
| 103 |
on.exit(unlink(tmp_file)) |
| 104 |
expect_true(xgb.DMatrix.save(dtest1, tmp_file)) |
| 105 |
# read from a local file |
| 106 |
xgb.set.config(verbosity = 2) |
| 107 |
expect_output(dtest3 <- xgb.DMatrix(tmp_file, nthread = 1), "entries loaded from") |
| 108 |
xgb.set.config(verbosity = 1) |
| 109 |
expect_output(dtest3 <- xgb.DMatrix(tmp_file, nthread = 1), NA) |
| 110 |
unlink(tmp_file) |
| 111 |
expect_equal(getinfo(dtest1, 'label'), getinfo(dtest3, 'label')) |
| 112 |
|
| 113 |
# from a libsvm text file |
| 114 |
tmp <- c("0 1:1 2:1", "1 3:1", "0 1:1") |
| 115 |
tmp_file <- tempfile(fileext = ".libsvm") |
| 116 |
writeLines(tmp, tmp_file) |
| 117 |
expect_true(file.exists(tmp_file)) |
| 118 |
dtest4 <- xgb.DMatrix( |
| 119 |
paste(tmp_file, "?format=libsvm", sep = ""), silent = TRUE, nthread = n_threads |
| 120 |
) |
| 121 |
expect_equal(dim(dtest4), c(3, 4)) |
| 122 |
expect_equal(getinfo(dtest4, 'label'), c(0, 1, 0)) |
| 123 |
|
| 124 |
# check that feature info is saved |
| 125 |
data(agaricus.train, package = 'xgboost') |
| 126 |
dtrain <- xgb.DMatrix( |
| 127 |
data = agaricus.train$data, label = agaricus.train$label, nthread = n_threads |
| 128 |
) |
| 129 |
cnames <- colnames(dtrain) |
| 130 |
expect_equal(length(cnames), 126) |
| 131 |
tmp_file <- tempfile('xgb.DMatrix_') |
| 132 |
xgb.DMatrix.save(dtrain, tmp_file) |
| 133 |
xgb.set.config(verbosity = 0) |
| 134 |
dtrain <- xgb.DMatrix(tmp_file, nthread = 1) |
| 135 |
expect_equal(colnames(dtrain), cnames) |
| 136 |
|
| 137 |
ft <- rep(c("c", "q"), each = length(cnames) / 2) |
| 138 |
setinfo(dtrain, "feature_type", ft) |
| 139 |
expect_equal(ft, getinfo(dtrain, "feature_type")) |
| 140 |
}) |
| 141 |
|
| 142 |
test_that("xgb.DMatrix: getinfo & setinfo", { |
| 143 |
dtest <- xgb.DMatrix(test_data, nthread = n_threads) |
| 144 |
expect_true(setinfo(dtest, 'label', test_label)) |
| 145 |
labels <- getinfo(dtest, 'label') |
| 146 |
expect_equal(test_label, getinfo(dtest, 'label')) |
| 147 |
|
| 148 |
expect_true(setinfo(dtest, 'label_lower_bound', test_label)) |
| 149 |
expect_equal(test_label, getinfo(dtest, 'label_lower_bound')) |
| 150 |
|
| 151 |
expect_true(setinfo(dtest, 'label_upper_bound', test_label)) |
| 152 |
expect_equal(test_label, getinfo(dtest, 'label_upper_bound')) |
| 153 |
|
| 154 |
expect_true(length(getinfo(dtest, 'weight')) == 0) |
| 155 |
expect_true(length(getinfo(dtest, 'base_margin')) == 0) |
| 156 |
|
| 157 |
expect_true(setinfo(dtest, 'weight', test_label)) |
| 158 |
expect_true(setinfo(dtest, 'base_margin', test_label)) |
| 159 |
expect_true(setinfo(dtest, 'group', c(50, 50))) |
| 160 |
expect_error(setinfo(dtest, 'group', test_label)) |
| 161 |
|
| 162 |
# providing character values will give an error |
| 163 |
expect_error(setinfo(dtest, 'weight', rep('a', nrow(test_data)))) |
| 164 |
|
| 165 |
# any other label should error |
| 166 |
expect_error(setinfo(dtest, 'asdf', test_label)) |
| 167 |
}) |
| 168 |
|
| 169 |
test_that("xgb.DMatrix: slice, dim", { |
| 170 |
dtest <- xgb.DMatrix(test_data, label = test_label, nthread = n_threads) |
| 171 |
expect_equal(dim(dtest), dim(test_data)) |
| 172 |
dsub1 <- xgb.slice.DMatrix(dtest, 1:42) |
| 173 |
expect_equal(nrow(dsub1), 42) |
| 174 |
expect_equal(ncol(dsub1), ncol(test_data)) |
| 175 |
|
| 176 |
dsub2 <- dtest[1:42, ] |
| 177 |
expect_equal(dim(dtest), dim(test_data)) |
| 178 |
expect_equal(getinfo(dsub1, 'label'), getinfo(dsub2, 'label')) |
| 179 |
}) |
| 180 |
|
| 181 |
test_that("xgb.DMatrix: slice, trailing empty rows", { |
| 182 |
data(agaricus.train, package = 'xgboost') |
| 183 |
train_data <- agaricus.train$data |
| 184 |
train_label <- agaricus.train$label |
| 185 |
dtrain <- xgb.DMatrix( |
| 186 |
data = train_data, label = train_label, nthread = n_threads |
| 187 |
) |
| 188 |
xgb.slice.DMatrix(dtrain, 6513L) |
| 189 |
train_data[6513, ] <- 0 |
| 190 |
dtrain <- xgb.DMatrix( |
| 191 |
data = train_data, label = train_label, nthread = n_threads |
| 192 |
) |
| 193 |
xgb.slice.DMatrix(dtrain, 6513L) |
| 194 |
expect_equal(nrow(dtrain), 6513) |
| 195 |
}) |
| 196 |
|
| 197 |
test_that("xgb.DMatrix: colnames", { |
| 198 |
dtest <- xgb.DMatrix(test_data, label = test_label, nthread = n_threads) |
| 199 |
expect_equal(colnames(dtest), colnames(test_data)) |
| 200 |
expect_error(colnames(dtest) <- 'asdf') |
| 201 |
new_names <- make.names(seq_len(ncol(test_data))) |
| 202 |
expect_silent(colnames(dtest) <- new_names) |
| 203 |
expect_equal(colnames(dtest), new_names) |
| 204 |
expect_silent(colnames(dtest) <- NULL) |
| 205 |
expect_null(colnames(dtest)) |
| 206 |
}) |
| 207 |
|
| 208 |
test_that("xgb.DMatrix: nrow is correct for a very sparse matrix", { |
| 209 |
set.seed(123) |
| 210 |
nr <- 1000 |
| 211 |
x <- Matrix::rsparsematrix(nr, 100, density = 0.0005) |
| 212 |
# we want it very sparse, so that last rows are empty |
| 213 |
expect_lt(max(x@i), nr) |
| 214 |
dtest <- xgb.DMatrix(x, nthread = n_threads) |
| 215 |
expect_equal(dim(dtest), dim(x)) |
| 216 |
}) |
| 217 |
|
| 218 |
test_that("xgb.DMatrix: print", { |
| 219 |
data(agaricus.train, package = 'xgboost') |
| 220 |
|
| 221 |
# core DMatrix with just data and labels |
| 222 |
dtrain <- xgb.DMatrix( |
| 223 |
data = agaricus.train$data, label = agaricus.train$label, |
| 224 |
nthread = n_threads |
| 225 |
) |
| 226 |
txt <- capture.output({ |
| 227 |
print(dtrain) |
| 228 |
}) |
| 229 |
expect_equal(txt, "xgb.DMatrix dim: 6513 x 126 info: label colnames: yes") |
| 230 |
|
| 231 |
# verbose=TRUE prints feature names |
| 232 |
txt <- capture.output({ |
| 233 |
print(dtrain, verbose = TRUE) |
| 234 |
}) |
| 235 |
expect_equal(txt[[1L]], "xgb.DMatrix dim: 6513 x 126 info: label colnames:") |
| 236 |
expect_equal(txt[[2L]], sprintf("'%s'", paste(colnames(dtrain), collapse = "','"))) |
| 237 |
|
| 238 |
# DMatrix with weights and base_margin |
| 239 |
dtrain <- xgb.DMatrix( |
| 240 |
data = agaricus.train$data, |
| 241 |
label = agaricus.train$label, |
| 242 |
weight = seq_along(agaricus.train$label), |
| 243 |
base_margin = agaricus.train$label, |
| 244 |
nthread = n_threads |
| 245 |
) |
| 246 |
txt <- capture.output({ |
| 247 |
print(dtrain) |
| 248 |
}) |
| 249 |
expect_equal(txt, "xgb.DMatrix dim: 6513 x 126 info: base_margin, label, weight colnames: yes") |
| 250 |
|
| 251 |
# DMatrix with just features |
| 252 |
dtrain <- xgb.DMatrix( |
| 253 |
data = agaricus.train$data, |
| 254 |
nthread = n_threads |
| 255 |
) |
| 256 |
txt <- capture.output({ |
| 257 |
print(dtrain) |
| 258 |
}) |
| 259 |
expect_equal(txt, "xgb.DMatrix dim: 6513 x 126 info: NA colnames: yes") |
| 260 |
|
| 261 |
# DMatrix with no column names |
| 262 |
data_no_colnames <- agaricus.train$data |
| 263 |
colnames(data_no_colnames) <- NULL |
| 264 |
dtrain <- xgb.DMatrix( |
| 265 |
data = data_no_colnames, |
| 266 |
nthread = n_threads |
| 267 |
) |
| 268 |
txt <- capture.output({ |
| 269 |
print(dtrain) |
| 270 |
}) |
| 271 |
expect_equal(txt, "xgb.DMatrix dim: 6513 x 126 info: NA colnames: no") |
| 272 |
}) |
| 273 |
|
| 274 |
test_that("xgb.DMatrix: Inf as missing", { |
| 275 |
x_inf <- matrix(as.numeric(1:10), nrow = 5) |
| 276 |
x_inf[2, 1] <- Inf |
| 277 |
|
| 278 |
x_nan <- x_inf |
| 279 |
x_nan[2, 1] <- NA_real_ |
| 280 |
|
| 281 |
m_inf <- xgb.DMatrix(x_inf, nthread = n_threads, missing = Inf) |
| 282 |
fname_inf <- file.path(tempdir(), "inf.dmatrix") |
| 283 |
xgb.DMatrix.save(m_inf, fname_inf) |
| 284 |
|
| 285 |
m_nan <- xgb.DMatrix(x_nan, nthread = n_threads, missing = NA_real_) |
| 286 |
fname_nan <- file.path(tempdir(), "nan.dmatrix") |
| 287 |
xgb.DMatrix.save(m_nan, fname_nan) |
| 288 |
|
| 289 |
infconn <- file(fname_inf, "rb") |
| 290 |
nanconn <- file(fname_nan, "rb") |
| 291 |
|
| 292 |
expect_equal(file.size(fname_inf), file.size(fname_nan)) |
| 293 |
|
| 294 |
bytes <- file.size(fname_inf) |
| 295 |
infdmatrix <- readBin(infconn, "raw", n = bytes) |
| 296 |
nandmatrix <- readBin(nanconn, "raw", n = bytes) |
| 297 |
|
| 298 |
expect_equal(length(infdmatrix), length(nandmatrix)) |
| 299 |
expect_equal(infdmatrix, nandmatrix) |
| 300 |
|
| 301 |
close(infconn) |
| 302 |
close(nanconn) |
| 303 |
|
| 304 |
file.remove(fname_inf) |
| 305 |
file.remove(fname_nan) |
| 306 |
}) |
| 307 |
|
| 308 |
test_that("xgb.DMatrix: missing in CSR", { |
| 309 |
x_dense <- matrix(as.numeric(1:10), nrow = 5) |
| 310 |
x_dense[2, 1] <- NA_real_ |
| 311 |
|
| 312 |
x_csr <- as(x_dense, "RsparseMatrix") |
| 313 |
|
| 314 |
m_dense <- xgb.DMatrix(x_dense, nthread = n_threads, missing = NA_real_) |
| 315 |
xgb.DMatrix.save(m_dense, "dense.dmatrix") |
| 316 |
|
| 317 |
m_csr <- xgb.DMatrix(x_csr, nthread = n_threads, missing = NA) |
| 318 |
xgb.DMatrix.save(m_csr, "csr.dmatrix") |
| 319 |
|
| 320 |
denseconn <- file("dense.dmatrix", "rb") |
| 321 |
csrconn <- file("csr.dmatrix", "rb") |
| 322 |
|
| 323 |
expect_equal(file.size("dense.dmatrix"), file.size("csr.dmatrix")) |
| 324 |
|
| 325 |
bytes <- file.size("dense.dmatrix") |
| 326 |
densedmatrix <- readBin(denseconn, "raw", n = bytes) |
| 327 |
csrmatrix <- readBin(csrconn, "raw", n = bytes) |
| 328 |
|
| 329 |
expect_equal(length(densedmatrix), length(csrmatrix)) |
| 330 |
expect_equal(densedmatrix, csrmatrix) |
| 331 |
|
| 332 |
close(denseconn) |
| 333 |
close(csrconn) |
| 334 |
|
| 335 |
file.remove("dense.dmatrix") |
| 336 |
file.remove("csr.dmatrix") |
| 337 |
}) |
| 338 |
|
| 339 |
test_that("xgb.DMatrix: error on three-dimensional array", { |
| 340 |
set.seed(123) |
| 341 |
x <- matrix(rnorm(500), nrow = 50) |
| 342 |
y <- rnorm(400) |
| 343 |
dim(y) <- c(50, 4, 2) |
| 344 |
expect_error(xgb.DMatrix(data = x, label = y)) |
| 345 |
}) |
| 346 |
|
| 347 |
test_that("xgb.DMatrix: can get group for both 'qid' and 'group' constructors", { |
| 348 |
set.seed(123) |
| 349 |
x <- matrix(rnorm(1000), nrow = 100) |
| 350 |
group <- c(20, 20, 60) |
| 351 |
qid <- c(rep(1, 20), rep(2, 20), rep(3, 60)) |
| 352 |
|
| 353 |
gr_mat <- xgb.DMatrix(x, group = group, nthread = 1) |
| 354 |
qid_mat <- xgb.DMatrix(x, qid = qid, nthread = 1) |
| 355 |
|
| 356 |
info_gr <- getinfo(gr_mat, "group") |
| 357 |
info_qid <- getinfo(qid_mat, "group") |
| 358 |
expect_equal(info_gr, info_qid) |
| 359 |
|
| 360 |
expected_gr <- c(0, 20, 40, 100) |
| 361 |
expect_equal(info_gr, expected_gr) |
| 362 |
}) |
| 363 |
|
| 364 |
test_that("xgb.DMatrix: data.frame", { |
| 365 |
df <- data.frame( |
| 366 |
a = (1:4) / 10, |
| 367 |
num = c(1, NA, 3, 4), |
| 368 |
as.int = as.integer(c(1, 2, 3, 4)), |
| 369 |
lo = c(TRUE, FALSE, NA, TRUE), |
| 370 |
str.fac = c("a", "b", "d", "c"), |
| 371 |
as.fac = as.factor(c(3, 5, 8, 11)), |
| 372 |
stringsAsFactors = TRUE |
| 373 |
) |
| 374 |
|
| 375 |
m <- xgb.DMatrix(df, nthread = 1) |
| 376 |
expect_equal(colnames(m), colnames(df)) |
| 377 |
expect_equal( |
| 378 |
getinfo(m, "feature_type"), c("float", "float", "int", "i", "c", "c") |
| 379 |
) |
| 380 |
|
| 381 |
df <- data.frame( |
| 382 |
missing = c("a", "b", "d", NA), |
| 383 |
valid = c("a", "b", "d", "c"), |
| 384 |
stringsAsFactors = TRUE |
| 385 |
) |
| 386 |
m <- xgb.DMatrix(df, nthread = 1) |
| 387 |
expect_equal(getinfo(m, "feature_type"), c("c", "c")) |
| 388 |
}) |
| 389 |
|
| 390 |
test_that("xgb.DMatrix: can take multi-dimensional 'base_margin'", { |
| 391 |
set.seed(123) |
| 392 |
x <- matrix(rnorm(100 * 10), nrow = 100) |
| 393 |
y <- matrix(rnorm(100 * 2), nrow = 100) |
| 394 |
b <- matrix(rnorm(100 * 2), nrow = 100) |
| 395 |
model <- xgb.train( |
| 396 |
data = xgb.DMatrix(data = x, label = y, nthread = n_threads), |
| 397 |
params = list( |
| 398 |
objective = "reg:squarederror", |
| 399 |
tree_method = "hist", |
| 400 |
multi_strategy = "multi_output_tree", |
| 401 |
base_score = 0, |
| 402 |
nthread = n_threads |
| 403 |
), |
| 404 |
nround = 1 |
| 405 |
) |
| 406 |
pred_only_x <- predict(model, x) |
| 407 |
pred_w_base <- predict( |
| 408 |
model, |
| 409 |
xgb.DMatrix(data = x, base_margin = b, nthread = 1) |
| 410 |
) |
| 411 |
expect_equal(pred_only_x, pred_w_base - b, tolerance = 1e-5) |
| 412 |
}) |
| 413 |
|
| 414 |
test_that("xgb.DMatrix: QuantileDMatrix produces same result as DMatrix", { |
| 415 |
data(mtcars) |
| 416 |
y <- mtcars[, 1] |
| 417 |
x <- mtcars[, -1] |
| 418 |
|
| 419 |
cast_matrix <- function(x) as.matrix(x) |
| 420 |
cast_df <- function(x) as.data.frame(x) |
| 421 |
cast_csr <- function(x) as(as.matrix(x), "RsparseMatrix") |
| 422 |
casting_funs <- list(cast_matrix, cast_df, cast_csr) |
| 423 |
|
| 424 |
for (casting_fun in casting_funs) { |
| 425 |
|
| 426 |
qdm <- xgb.QuantileDMatrix( |
| 427 |
data = casting_fun(x), |
| 428 |
label = y, |
| 429 |
nthread = n_threads, |
| 430 |
max_bin = 5 |
| 431 |
) |
| 432 |
params <- list( |
| 433 |
tree_method = "hist", |
| 434 |
objective = "reg:squarederror", |
| 435 |
nthread = n_threads, |
| 436 |
max_bin = 5 |
| 437 |
) |
| 438 |
model_qdm <- xgb.train( |
| 439 |
params = params, |
| 440 |
data = qdm, |
| 441 |
nrounds = 2 |
| 442 |
) |
| 443 |
pred_qdm <- predict(model_qdm, x) |
| 444 |
|
| 445 |
dm <- xgb.DMatrix( |
| 446 |
data = x, |
| 447 |
label = y, |
| 448 |
nthread = n_threads |
| 449 |
) |
| 450 |
model_dm <- xgb.train( |
| 451 |
params = params, |
| 452 |
data = dm, |
| 453 |
nrounds = 2 |
| 454 |
) |
| 455 |
pred_dm <- predict(model_dm, x) |
| 456 |
|
| 457 |
expect_equal(pred_qdm, pred_dm) |
| 458 |
} |
| 459 |
}) |
| 460 |
|
| 461 |
test_that("xgb.DMatrix: QuantileDMatrix is not accepted by exact method", { |
| 462 |
data(mtcars) |
| 463 |
y <- mtcars[, 1] |
| 464 |
x <- as.matrix(mtcars[, -1]) |
| 465 |
qdm <- xgb.QuantileDMatrix( |
| 466 |
data = x, |
| 467 |
label = y, |
| 468 |
nthread = n_threads |
| 469 |
) |
| 470 |
params <- list( |
| 471 |
tree_method = "exact", |
| 472 |
objective = "reg:squarederror", |
| 473 |
nthread = n_threads |
| 474 |
) |
| 475 |
expect_error({ |
| 476 |
xgb.train( |
| 477 |
params = params, |
| 478 |
data = qdm, |
| 479 |
nrounds = 2 |
| 480 |
) |
| 481 |
}) |
| 482 |
}) |
| 483 |
|
| 484 |
test_that("xgb.DMatrix: ExtMemDMatrix produces the same results as regular DMatrix", { |
| 485 |
data(mtcars) |
| 486 |
y <- mtcars[, 1] |
| 487 |
x <- as.matrix(mtcars[, -1]) |
| 488 |
set.seed(123) |
| 489 |
params <- list( |
| 490 |
objective = "reg:squarederror", |
| 491 |
nthread = n_threads |
| 492 |
) |
| 493 |
model <- xgb.train( |
| 494 |
data = xgb.DMatrix(x, label = y, nthread = 1), |
| 495 |
params = params, |
| 496 |
nrounds = 5 |
| 497 |
) |
| 498 |
pred <- predict(model, x) |
| 499 |
pred <- unname(pred) |
| 500 |
|
| 501 |
iterator_env <- as.environment( |
| 502 |
list( |
| 503 |
iter = 0, |
| 504 |
x = mtcars[, -1], |
| 505 |
y = mtcars[, 1] |
| 506 |
) |
| 507 |
) |
| 508 |
iterator_next <- function(iterator_env) { |
| 509 |
curr_iter <- iterator_env[["iter"]] |
| 510 |
if (curr_iter >= 2) { |
| 511 |
return(NULL) |
| 512 |
} |
| 513 |
if (curr_iter == 0) { |
| 514 |
x_batch <- iterator_env[["x"]][1:16, ] |
| 515 |
y_batch <- iterator_env[["y"]][1:16] |
| 516 |
} else { |
| 517 |
x_batch <- iterator_env[["x"]][17:32, ] |
| 518 |
y_batch <- iterator_env[["y"]][17:32] |
| 519 |
} |
| 520 |
on.exit({ |
| 521 |
iterator_env[["iter"]] <- curr_iter + 1 |
| 522 |
}) |
| 523 |
return(xgb.DataBatch(data = x_batch, label = y_batch)) |
| 524 |
} |
| 525 |
iterator_reset <- function(iterator_env) { |
| 526 |
iterator_env[["iter"]] <- 0 |
| 527 |
} |
| 528 |
data_iterator <- xgb.DataIter( |
| 529 |
env = iterator_env, |
| 530 |
f_next = iterator_next, |
| 531 |
f_reset = iterator_reset |
| 532 |
) |
| 533 |
cache_prefix <- tempdir() |
| 534 |
edm <- xgb.ExtMemDMatrix(data_iterator, cache_prefix, nthread = 1) |
| 535 |
expect_true(inherits(edm, "xgb.ExtMemDMatrix")) |
| 536 |
expect_true(inherits(edm, "xgb.DMatrix")) |
| 537 |
set.seed(123) |
| 538 |
model_ext <- xgb.train( |
| 539 |
data = edm, |
| 540 |
params = params, |
| 541 |
nrounds = 5 |
| 542 |
) |
| 543 |
|
| 544 |
pred_model1_edm <- predict(model, edm) |
| 545 |
pred_model2_mat <- predict(model_ext, x) |> unname() |
| 546 |
pred_model2_edm <- predict(model_ext, edm) |
| 547 |
|
| 548 |
expect_equal(pred_model1_edm, pred) |
| 549 |
expect_equal(pred_model2_mat, pred) |
| 550 |
expect_equal(pred_model2_edm, pred) |
| 551 |
}) |
| 552 |
|
| 553 |
test_that("xgb.DMatrix: External QDM produces same results as regular QDM", { |
| 554 |
data(mtcars) |
| 555 |
y <- mtcars[, 1] |
| 556 |
x <- as.matrix(mtcars[, -1]) |
| 557 |
set.seed(123) |
| 558 |
params <- list( |
| 559 |
objective = "reg:squarederror", |
| 560 |
nthread = n_threads, |
| 561 |
max_bin = 3 |
| 562 |
) |
| 563 |
model <- xgb.train( |
| 564 |
data = xgb.QuantileDMatrix( |
| 565 |
x, |
| 566 |
label = y, |
| 567 |
nthread = 1, |
| 568 |
max_bin = 3 |
| 569 |
), |
| 570 |
params = params, |
| 571 |
nrounds = 5 |
| 572 |
) |
| 573 |
pred <- predict(model, x) |
| 574 |
pred <- unname(pred) |
| 575 |
|
| 576 |
iterator_env <- as.environment( |
| 577 |
list( |
| 578 |
iter = 0, |
| 579 |
x = mtcars[, -1], |
| 580 |
y = mtcars[, 1] |
| 581 |
) |
| 582 |
) |
| 583 |
iterator_next <- function(iterator_env) { |
| 584 |
curr_iter <- iterator_env[["iter"]] |
| 585 |
if (curr_iter >= 2) { |
| 586 |
return(NULL) |
| 587 |
} |
| 588 |
if (curr_iter == 0) { |
| 589 |
x_batch <- iterator_env[["x"]][1:16, ] |
| 590 |
y_batch <- iterator_env[["y"]][1:16] |
| 591 |
} else { |
| 592 |
x_batch <- iterator_env[["x"]][17:32, ] |
| 593 |
y_batch <- iterator_env[["y"]][17:32] |
| 594 |
} |
| 595 |
on.exit({ |
| 596 |
iterator_env[["iter"]] <- curr_iter + 1 |
| 597 |
}) |
| 598 |
return(xgb.DataBatch(data = x_batch, label = y_batch)) |
| 599 |
} |
| 600 |
iterator_reset <- function(iterator_env) { |
| 601 |
iterator_env[["iter"]] <- 0 |
| 602 |
} |
| 603 |
data_iterator <- xgb.DataIter( |
| 604 |
env = iterator_env, |
| 605 |
f_next = iterator_next, |
| 606 |
f_reset = iterator_reset |
| 607 |
) |
| 608 |
cache_prefix <- tempdir() |
| 609 |
qdm <- xgb.QuantileDMatrix.from_iterator( |
| 610 |
data_iterator, |
| 611 |
max_bin = 3, |
| 612 |
nthread = 1 |
| 613 |
) |
| 614 |
expect_true(inherits(qdm, "xgb.QuantileDMatrix")) |
| 615 |
expect_true(inherits(qdm, "xgb.DMatrix")) |
| 616 |
set.seed(123) |
| 617 |
model_ext <- xgb.train( |
| 618 |
data = qdm, |
| 619 |
params = params, |
| 620 |
nrounds = 5 |
| 621 |
) |
| 622 |
|
| 623 |
pred_model1_qdm <- predict(model, qdm) |
| 624 |
pred_model2_mat <- predict(model_ext, x) |> unname() |
| 625 |
pred_model2_qdm <- predict(model_ext, qdm) |
| 626 |
|
| 627 |
expect_equal(pred_model1_qdm, pred) |
| 628 |
expect_equal(pred_model2_mat, pred) |
| 629 |
expect_equal(pred_model2_qdm, pred) |
| 630 |
}) |
| 631 |
|
| 632 |
test_that("xgb.DMatrix: R errors thrown on DataIterator are thrown back to the user", { |
| 633 |
data(mtcars) |
| 634 |
iterator_env <- as.environment( |
| 635 |
list( |
| 636 |
iter = 0, |
| 637 |
x = mtcars[, -1], |
| 638 |
y = mtcars[, 1] |
| 639 |
) |
| 640 |
) |
| 641 |
iterator_next <- function(iterator_env) { |
| 642 |
curr_iter <- iterator_env[["iter"]] |
| 643 |
if (curr_iter >= 2) { |
| 644 |
return(0) |
| 645 |
} |
| 646 |
if (curr_iter == 0) { |
| 647 |
x_batch <- iterator_env[["x"]][1:16, ] |
| 648 |
y_batch <- iterator_env[["y"]][1:16] |
| 649 |
} else { |
| 650 |
stop("custom error") |
| 651 |
} |
| 652 |
on.exit({ |
| 653 |
iterator_env[["iter"]] <- curr_iter + 1 |
| 654 |
}) |
| 655 |
return(xgb.DataBatch(data = x_batch, label = y_batch)) |
| 656 |
} |
| 657 |
iterator_reset <- function(iterator_env) { |
| 658 |
iterator_env[["iter"]] <- 0 |
| 659 |
} |
| 660 |
data_iterator <- xgb.DataIter( |
| 661 |
env = iterator_env, |
| 662 |
f_next = iterator_next, |
| 663 |
f_reset = iterator_reset |
| 664 |
) |
| 665 |
expect_error( |
| 666 |
{xgb.ExtMemDMatrix(data_iterator, nthread = 1)}, |
| 667 |
"custom error" |
| 668 |
) |
| 669 |
}) |
| 670 |
|
| 671 |
test_that("xgb.DMatrix: number of non-missing matches data", { |
| 672 |
x <- matrix(1:10, nrow = 5) |
| 673 |
dm1 <- xgb.DMatrix(x, nthread = 1) |
| 674 |
expect_equal(xgb.get.DMatrix.num.non.missing(dm1), 10) |
| 675 |
|
| 676 |
x[2, 2] <- NA |
| 677 |
x[4, 1] <- NA |
| 678 |
dm2 <- xgb.DMatrix(x, nthread = 1) |
| 679 |
expect_equal(xgb.get.DMatrix.num.non.missing(dm2), 8) |
| 680 |
}) |
| 681 |
|
| 682 |
test_that("xgb.DMatrix: retrieving data as CSR", { |
| 683 |
data(mtcars) |
| 684 |
dm <- xgb.DMatrix(as.matrix(mtcars), nthread = 1) |
| 685 |
csr <- xgb.get.DMatrix.data(dm) |
| 686 |
expect_equal(dim(csr), dim(mtcars)) |
| 687 |
expect_equal(colnames(csr), colnames(mtcars)) |
| 688 |
expect_equal(unname(as.matrix(csr)), unname(as.matrix(mtcars)), tolerance = 1e-6) |
| 689 |
}) |
| 690 |
|
| 691 |
test_that("xgb.DMatrix: quantile cuts look correct", { |
| 692 |
data(mtcars) |
| 693 |
y <- mtcars$mpg |
| 694 |
x <- as.matrix(mtcars[, -1]) |
| 695 |
dm <- xgb.DMatrix(x, label = y, nthread = 1) |
| 696 |
model <- xgb.train( |
| 697 |
data = dm, |
| 698 |
params = list( |
| 699 |
tree_method = "hist", |
| 700 |
max_bin = 8, |
| 701 |
nthread = 1 |
| 702 |
), |
| 703 |
nrounds = 3 |
| 704 |
) |
| 705 |
qcut_list <- xgb.get.DMatrix.qcut(dm, "list") |
| 706 |
qcut_arrays <- xgb.get.DMatrix.qcut(dm, "arrays") |
| 707 |
|
| 708 |
expect_equal(length(qcut_arrays), 2) |
| 709 |
expect_equal(names(qcut_arrays), c("indptr", "data")) |
| 710 |
expect_equal(length(qcut_arrays$indptr), ncol(x) + 1) |
| 711 |
expect_true(min(diff(qcut_arrays$indptr)) > 0) |
| 712 |
|
| 713 |
col_min <- apply(x, 2, min) |
| 714 |
col_max <- apply(x, 2, max) |
| 715 |
|
| 716 |
expect_equal(length(qcut_list), ncol(x)) |
| 717 |
expect_equal(names(qcut_list), colnames(x)) |
| 718 |
lapply( |
| 719 |
seq(1, ncol(x)), |
| 720 |
function(col) { |
| 721 |
cuts <- qcut_list[[col]] |
| 722 |
expect_true(min(diff(cuts)) > 0) |
| 723 |
expect_true(col_min[col] > cuts[1]) |
| 724 |
expect_true(col_max[col] < cuts[length(cuts)]) |
| 725 |
expect_true(length(cuts) <= 9) |
| 726 |
} |
| 727 |
) |
| 728 |
}) |
| 729 |
|
| 730 |
test_that("xgb.DMatrix: slicing keeps field indicators", { |
| 731 |
data(mtcars) |
| 732 |
x <- as.matrix(mtcars[, -1]) |
| 733 |
y <- mtcars[, 1] |
| 734 |
dm <- xgb.DMatrix( |
| 735 |
data = x, |
| 736 |
label_lower_bound = -y, |
| 737 |
label_upper_bound = y, |
| 738 |
nthread = 1 |
| 739 |
) |
| 740 |
idx_take <- seq(1, 5) |
| 741 |
dm_slice <- xgb.slice.DMatrix(dm, idx_take) |
| 742 |
|
| 743 |
expect_true(xgb.DMatrix.hasinfo(dm_slice, "label_lower_bound")) |
| 744 |
expect_true(xgb.DMatrix.hasinfo(dm_slice, "label_upper_bound")) |
| 745 |
expect_false(xgb.DMatrix.hasinfo(dm_slice, "label")) |
| 746 |
|
| 747 |
expect_equal(getinfo(dm_slice, "label_lower_bound"), -y[idx_take], tolerance = 1e-6) |
| 748 |
expect_equal(getinfo(dm_slice, "label_upper_bound"), y[idx_take], tolerance = 1e-6) |
| 749 |
}) |
| 750 |
|
| 751 |
test_that("xgb.DMatrix: can slice with groups", { |
| 752 |
data(iris) |
| 753 |
x <- as.matrix(iris[, -5]) |
| 754 |
set.seed(123) |
| 755 |
y <- sample(3, size = nrow(x), replace = TRUE) |
| 756 |
group <- c(50, 50, 50) |
| 757 |
dm <- xgb.DMatrix(x, label = y, group = group, nthread = 1) |
| 758 |
idx_take <- seq(1, 50) |
| 759 |
dm_slice <- xgb.slice.DMatrix(dm, idx_take, allow_groups = TRUE) |
| 760 |
|
| 761 |
expect_true(xgb.DMatrix.hasinfo(dm_slice, "label")) |
| 762 |
expect_false(xgb.DMatrix.hasinfo(dm_slice, "group")) |
| 763 |
expect_false(xgb.DMatrix.hasinfo(dm_slice, "qid")) |
| 764 |
expect_null(getinfo(dm_slice, "group")) |
| 765 |
expect_equal(getinfo(dm_slice, "label"), y[idx_take], tolerance = 1e-6) |
| 766 |
}) |
| 767 |
|
| 768 |
test_that("xgb.DMatrix: can read CSV", { |
| 769 |
txt <- paste( |
| 770 |
"1,2,3", |
| 771 |
"-1,3,2", |
| 772 |
sep = "\n" |
| 773 |
) |
| 774 |
fname <- file.path(tempdir(), "data.csv") |
| 775 |
writeChar(txt, fname) |
| 776 |
uri <- paste0(fname, "?format=csv&label_column=0") |
| 777 |
dm <- xgb.DMatrix(uri, silent = TRUE, nthread = 1) |
| 778 |
expect_equal(getinfo(dm, "label"), c(1, -1)) |
| 779 |
expect_equal( |
| 780 |
as.matrix(xgb.get.DMatrix.data(dm)), |
| 781 |
matrix(c(2, 3, 3, 2), nrow = 2, byrow = TRUE) |
| 782 |
) |
| 783 |
}) |