[R] Cannot plot trees with categorical splits · Issue #9925 · dmlc/xgboost (original) (raw)

ref #9810

Currently, attempting to plot trees that have categorical splits in R will result in an error:

library(xgboost) set.seed(123) y <- rnorm(100) x <- sample(3, size=100*3, replace=TRUE) |> matrix(nrow=100) x <- x - 1 dm <- xgb.DMatrix(data=x, label=y) setinfo(dm, "feature_type", c("c", "c", "c")) model <- xgb.train( data=dm, params=list( tree_method="hist", max_depth=3 ), nrounds=2 ) xgb.plot.tree(model=model)

Error in do.call(rbind, matches)[, c(2, 3, 5, 6, 7, 8, 10), drop = FALSE] :
subscript out of bounds

This is due to the regexes used to parse the dumps not having been updated for the format used in categorical splits:

branch_rx <- paste0("f(\\d+)<(", anynumber_regex, ")\\] yes=(\\d+),no=(\\d+),missing=(\\d+),",