Skip to content

Commit

Permalink
Parse splits on inf in xgb.model.dt.tree (#3900)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen Thatcher committed Mar 2, 2021
1 parent a9b4a95 commit a2b61f9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion R-package/R/xgb.model.dt.tree.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ xgb.model.dt.tree <- function(feature_names = NULL, model = NULL, text = NULL,

add.tree.id <- function(node, tree) if (use_int_id) node else paste(tree, node, sep = "-")

anynumber_regex <- "[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?"
anynumber_regex <- "[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?|[-+]?[Ii]nf"

td <- data.table(t = text)
td[position, Tree := 1L]
Expand Down
12 changes: 12 additions & 0 deletions R-package/tests/testthat/test_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,18 @@ test_that("xgb.model.dt.tree throws error for gblinear", {
expect_error(xgb.model.dt.tree(model = bst.GLM))
})

test_that("xgb.model.dt.tree works with splits on inf", {
tree.dump <- c(
"booster[0]",
"0:[f1<inf] yes=1,no=3,missing=3,gain=0.1,cover=3",
"1:[f2<-inf] yes=2,no=3,missing=3,gain=0.1,cover=1",
"2:[f1<2] yes=4,no=3,missing=3,gain=0.3,cover=4",
"3:leaf=0.2,cover=1",
"4:leaf=0.5,cover=1"
)
expect_equal(xgb.model.dt.tree(text = tree.dump)$Split, c(Inf, -Inf, 2, NA, NA))
})

test_that("xgb.importance works with and without feature names", {
importance.Tree <- xgb.importance(feature_names = feature.names, model = bst.Tree)
if (!flag_32bit)
Expand Down

0 comments on commit a2b61f9

Please sign in to comment.