Skip to content

Commit

Permalink
Check explicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
trivialfis committed Feb 25, 2022
1 parent 5b3e003 commit 120dd68
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
5 changes: 4 additions & 1 deletion R-package/R/xgb.model.dt.tree.R
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,10 @@ xgb.model.dt.tree <- function(feature_names = NULL, model = NULL, text = NULL,
]

# assign feature_names when available
if (!is.null(feature_names) && !is.na(td$Feature)) {
is_stump <- function() {
return(length(td$Feature) == 1 && is.na(td$Feature))
}
if (!is.null(feature_names) && !is_stump()) {
if (length(feature_names) <= max(as.numeric(td$Feature), na.rm = TRUE))
stop("feature_names has less elements than there are features used in the model")
td[isLeaf == FALSE, Feature := feature_names[as.numeric(Feature) + 1]]
Expand Down
10 changes: 10 additions & 0 deletions R-package/tests/testthat/test_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,16 @@ test_that("xgb.importance works with and without feature names", {
imp
}
expect_equal(importance_from_dump(), importance, tolerance = 1e-6)

## decision stump
m <- xgboost::xgboost(
data = as.matrix(data.frame(x = c(0, 1))),
label = c(1, 2),
nrounds = 1
)
df <- xgb.model.dt.tree(model = m)
expect_equal(df$Feature, "Leaf")
expect_equal(df$Cover, 2)
})

test_that("xgb.importance works with GLM model", {
Expand Down

0 comments on commit 120dd68

Please sign in to comment.