| 1 |
context("feature weights") |
| 2 |
|
| 3 |
n_threads <- 2 |
| 4 |
|
| 5 |
test_that("training with feature weights works", { |
| 6 |
nrows <- 1000 |
| 7 |
ncols <- 9 |
| 8 |
set.seed(2022) |
| 9 |
x <- matrix(rnorm(nrows * ncols), nrow = nrows) |
| 10 |
y <- rowSums(x) |
| 11 |
weights <- seq(from = 1, to = ncols) |
| 12 |
|
| 13 |
test <- function(tm) { |
| 14 |
names <- paste0("f", 1:ncols) |
| 15 |
xy <- xgb.DMatrix( |
| 16 |
data = x, label = y, feature_weights = weights, nthread = n_threads |
| 17 |
) |
| 18 |
params <- list( |
| 19 |
colsample_bynode = 0.4, tree_method = tm, nthread = n_threads |
| 20 |
) |
| 21 |
model <- xgb.train(params = params, data = xy, nrounds = 32) |
| 22 |
importance <- xgb.importance(model = model, feature_names = names) |
| 23 |
expect_equal(dim(importance), c(ncols, 4)) |
| 24 |
importance <- importance[order(importance$Feature)] |
| 25 |
expect_lt(importance[1, Frequency], importance[9, Frequency]) |
| 26 |
} |
| 27 |
|
| 28 |
for (tm in c("hist", "approx")) { |
| 29 |
test(tm) |
| 30 |
} |
| 31 |
}) |