Skip to content

Commit 29b7389

Browse files
Add new tests for helper functions
1 parent 13a341b commit 29b7389

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

R-package/tests/testthat/test_helpers.R

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require(data.table)
55
require(Matrix)
66
require(vcd)
77

8-
set.seed(1994)
8+
set.seed(1982)
99
data(Arthritis)
1010
data(agaricus.train, package='xgboost')
1111
df <- data.table(Arthritis, keep.rownames = F)
@@ -17,25 +17,38 @@ output_vector <- df[,Y := 0][Improved == "Marked",Y := 1][,Y]
1717
bst <- xgboost(data = sparse_matrix, label = output_vector, max.depth = 9,
1818
eta = 1, nthread = 2, nround = 10,objective = "binary:logistic")
1919

20+
feature.names <- agaricus.train$data@Dimnames[[2]]
21+
2022
test_that("xgb.dump works", {
2123
capture.output(print(xgb.dump(bst)))
2224
expect_true(xgb.dump(bst, 'xgb.model.dump', with.stats = T))
2325
})
2426

25-
test_that("xgb.importance works", {
27+
test_that("xgb.model.dt.tree works with and without feature names", {
28+
names.dt.trees <- c("ID", "Feature", "Split", "Yes", "No", "Missing", "Quality", "Cover",
29+
"Tree", "Yes.Feature", "Yes.Cover", "Yes.Quality", "No.Feature", "No.Cover", "No.Quality")
30+
dt.tree <- xgb.model.dt.tree(feature_names = feature.names, model = bst)
31+
expect_equal(names.dt.trees, names(dt.tree))
32+
expect_equal(dim(dt.tree), c(162, 15))
33+
xgb.model.dt.tree(model = bst)
34+
})
35+
36+
test_that("xgb.importance works with and without feature names", {
2637
importance <- xgb.importance(feature_names = sparse_matrix@Dimnames[[2]], model = bst)
2738
expect_equal(dim(importance), c(7, 4))
2839
expect_equal(colnames(importance), c("Feature", "Gain", "Cover", "Frequency"))
40+
xgb.importance(model = bst)
2941
})
3042

31-
test_that("xgb.plot.tree works", {
32-
xgb.plot.tree(feature_names = agaricus.train$data@Dimnames[[2]], model = bst)
43+
test_that("xgb.plot.tree works with and without feature names", {
44+
xgb.plot.tree(feature_names = feature.names, model = bst)
45+
xgb.plot.tree(model = bst)
3346
})
3447

48+
test_that("xgb.plot.multi.trees works with and without feature names", {
49+
xgb.plot.multi.trees(model = bst, feature_names = feature.names, features.keep = 3)
50+
xgb.plot.multi.trees(model = bst, features.keep = 3)
51+
})
3552
test_that("xgb.plot.deepness works", {
3653
xgb.plot.deepness(model = bst)
3754
})
38-
39-
test_that("xgb.plot.multi.trees works", {
40-
xgb.plot.multi.trees(model = bst, feature_names = agaricus.train$data@Dimnames[[2]], 3)
41-
})

0 commit comments

Comments
 (0)