| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/xgb.plot.tree.R |
| 3 |
\name{xgb.plot.tree} |
| 4 |
\alias{xgb.plot.tree} |
| 5 |
\title{Plot boosted trees} |
| 6 |
\usage{ |
| 7 |
xgb.plot.tree( |
| 8 |
model, |
| 9 |
tree_idx = 1, |
| 10 |
plot_width = NULL, |
| 11 |
plot_height = NULL, |
| 12 |
with_stats = FALSE, |
| 13 |
... |
| 14 |
) |
| 15 |
} |
| 16 |
\arguments{ |
| 17 |
\item{model}{Object of class \code{xgb.Booster}. If it contains feature names |
| 18 |
(they can be set through \code{\link[=setinfo]{setinfo()}}, they will be used in the |
| 19 |
output from this function.} |
| 20 |
|
| 21 |
\item{tree_idx}{An integer of the tree index that should be used. This |
| 22 |
is an 1-based index.} |
| 23 |
|
| 24 |
\item{plot_width, plot_height}{Width and height of the graph in pixels. |
| 25 |
The values are passed to \code{DiagrammeR::render_graph()}.} |
| 26 |
|
| 27 |
\item{with_stats}{Whether to dump some additional statistics about the |
| 28 |
splits. When this option is on, the model dump contains two additional |
| 29 |
values: gain is the approximate loss function gain we get in each split; |
| 30 |
cover is the sum of second order gradient in each node.} |
| 31 |
|
| 32 |
\item{...}{Not used. |
| 33 |
|
| 34 |
Some arguments that were part of this function in previous XGBoost versions are currently |
| 35 |
deprecated or have been renamed. If a deprecated or renamed argument is passed, will throw |
| 36 |
a warning (by default) and use its current equivalent instead. This warning will become an |
| 37 |
error if using the \link[=xgboost-options]{'strict mode' option}. |
| 38 |
|
| 39 |
If some additional argument is passed that is neither a current function argument nor |
| 40 |
a deprecated or renamed argument, a warning or error will be thrown depending on the |
| 41 |
'strict mode' option. |
| 42 |
|
| 43 |
\bold{Important:} \code{...} will be removed in a future version, and all the current |
| 44 |
deprecation warnings will become errors. Please use only arguments that form part of |
| 45 |
the function signature.} |
| 46 |
} |
| 47 |
\value{ |
| 48 |
Rendered graph object which is an htmlwidget of ' class \code{grViz}. Similar to |
| 49 |
"ggplot" objects, it needs to be printed when not running from the command |
| 50 |
line. |
| 51 |
} |
| 52 |
\description{ |
| 53 |
Read a tree model text dump and plot the model. |
| 54 |
} |
| 55 |
\details{ |
| 56 |
The content of each node is visualized as follows: |
| 57 |
\itemize{ |
| 58 |
\item For non-terminal nodes, it will display the split condition (number or name |
| 59 |
if available, and the condition that would decide to which node to go |
| 60 |
next). |
| 61 |
\item Those nodes will be connected to their children by arrows that indicate |
| 62 |
whether the branch corresponds to the condition being met or not being met. |
| 63 |
\item Terminal (leaf) nodes contain the margin to add when ending there. |
| 64 |
} |
| 65 |
|
| 66 |
The "Yes" branches are marked by the "< split_value" label. |
| 67 |
The branches also used for missing values are marked as bold |
| 68 |
(as in "carrying extra capacity"). |
| 69 |
|
| 70 |
This function uses \href{https://www.graphviz.org/}{GraphViz} as DiagrammeR |
| 71 |
backend. |
| 72 |
} |
| 73 |
\examples{ |
| 74 |
data("ToothGrowth") |
| 75 |
x <- ToothGrowth[, c("len", "dose")] |
| 76 |
y <- ToothGrowth$supp |
| 77 |
model <- xgboost( |
| 78 |
x, y, |
| 79 |
nthreads = 1L, |
| 80 |
nrounds = 3L, |
| 81 |
max_depth = 3L |
| 82 |
) |
| 83 |
|
| 84 |
# plot the first tree |
| 85 |
xgb.plot.tree(model, tree_idx = 1) |
| 86 |
|
| 87 |
# Below is an example of how to save this plot to a file. |
| 88 |
if (require("DiagrammeR") && require("htmlwidgets")) { |
| 89 |
fname <- file.path(tempdir(), "plot.html'") |
| 90 |
gr <- xgb.plot.tree(model, tree_idx = 1) |
| 91 |
htmlwidgets::saveWidget(gr, fname) |
| 92 |
} |
| 93 |
} |