Read a tree model text dump and plot the model.
Usage
xgb.plot.tree(
model,
tree_idx = 1,
plot_width = NULL,
plot_height = NULL,
with_stats = FALSE,
...
)
Arguments
- model
Object of class
xgb.Booster
. If it contains feature names (they can be set throughsetinfo()
, they will be used in the output from this function.- tree_idx
An integer of the tree index that should be used. This is an 1-based index.
- plot_width, plot_height
Width and height of the graph in pixels. The values are passed to
DiagrammeR::render_graph()
.- with_stats
Whether to dump some additional statistics about the splits. When this option is on, the model dump contains two additional values: gain is the approximate loss function gain we get in each split; cover is the sum of second order gradient in each node.
- ...
Not used.
Some arguments that were part of this function in previous XGBoost versions are currently deprecated or have been renamed. If a deprecated or renamed argument is passed, will throw a warning (by default) and use its current equivalent instead. This warning will become an error if using the 'strict mode' option.
If some additional argument is passed that is neither a current function argument nor a deprecated or renamed argument, a warning or error will be thrown depending on the 'strict mode' option.
Important:
...
will be removed in a future version, and all the current deprecation warnings will become errors. Please use only arguments that form part of the function signature.
Value
Rendered graph object which is an htmlwidget of ' class grViz
. Similar to
"ggplot" objects, it needs to be printed when not running from the command
line.
Details
The content of each node is visualized as follows:
For non-terminal nodes, it will display the split condition (number or name if available, and the condition that would decide to which node to go next).
Those nodes will be connected to their children by arrows that indicate whether the branch corresponds to the condition being met or not being met.
Terminal (leaf) nodes contain the margin to add when ending there.
The "Yes" branches are marked by the "< split_value" label. The branches also used for missing values are marked as bold (as in "carrying extra capacity").
This function uses GraphViz as DiagrammeR backend.
Examples
data("ToothGrowth")
x <- ToothGrowth[, c("len", "dose")]
y <- ToothGrowth$supp
model <- xgboost(
x, y,
nthreads = 1L,
nrounds = 3L,
max_depth = 3L
)
# plot the first tree
xgb.plot.tree(model, tree_idx = 1)
# Below is an example of how to save this plot to a file.
if (require("DiagrammeR") && require("htmlwidgets")) {
fname <- file.path(tempdir(), "plot.html'")
gr <- xgb.plot.tree(model, tree_idx = 1)
htmlwidgets::saveWidget(gr, fname)
}