| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/xgb.plot.multi.trees.R |
| 3 |
\name{xgb.plot.multi.trees} |
| 4 |
\alias{xgb.plot.multi.trees} |
| 5 |
\title{Project all trees on one tree} |
| 6 |
\usage{ |
| 7 |
xgb.plot.multi.trees( |
| 8 |
model, |
| 9 |
features_keep = 5, |
| 10 |
plot_width = NULL, |
| 11 |
plot_height = NULL, |
| 12 |
render = TRUE, |
| 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{features_keep}{Number of features to keep in each position of the multi trees, |
| 22 |
by default 5.} |
| 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{render}{Should the graph be rendered or not? The default is \code{TRUE}.} |
| 28 |
|
| 29 |
\item{...}{Not used. |
| 30 |
|
| 31 |
Some arguments that were part of this function in previous XGBoost versions are currently |
| 32 |
deprecated or have been renamed. If a deprecated or renamed argument is passed, will throw |
| 33 |
a warning (by default) and use its current equivalent instead. This warning will become an |
| 34 |
error if using the \link[=xgboost-options]{'strict mode' option}. |
| 35 |
|
| 36 |
If some additional argument is passed that is neither a current function argument nor |
| 37 |
a deprecated or renamed argument, a warning or error will be thrown depending on the |
| 38 |
'strict mode' option. |
| 39 |
|
| 40 |
\bold{Important:} \code{...} will be removed in a future version, and all the current |
| 41 |
deprecation warnings will become errors. Please use only arguments that form part of |
| 42 |
the function signature.} |
| 43 |
} |
| 44 |
\value{ |
| 45 |
Rendered graph object which is an htmlwidget of ' class \code{grViz}. Similar to |
| 46 |
"ggplot" objects, it needs to be printed when not running from the command |
| 47 |
line. |
| 48 |
} |
| 49 |
\description{ |
| 50 |
Visualization of the ensemble of trees as a single collective unit. |
| 51 |
} |
| 52 |
\details{ |
| 53 |
Note that this function does not work with models that were fitted to |
| 54 |
categorical data. |
| 55 |
|
| 56 |
This function tries to capture the complexity of a gradient boosted tree model |
| 57 |
in a cohesive way by compressing an ensemble of trees into a single tree-graph representation. |
| 58 |
The goal is to improve the interpretability of a model generally seen as black box. |
| 59 |
|
| 60 |
Note: this function is applicable to tree booster-based models only. |
| 61 |
|
| 62 |
It takes advantage of the fact that the shape of a binary tree is only defined by |
| 63 |
its depth (therefore, in a boosting model, all trees have similar shape). |
| 64 |
|
| 65 |
Moreover, the trees tend to reuse the same features. |
| 66 |
|
| 67 |
The function projects each tree onto one, and keeps for each position the |
| 68 |
\code{features_keep} first features (based on the Gain per feature measure). |
| 69 |
|
| 70 |
This function is inspired by this blog post: |
| 71 |
\url{https://wellecks.wordpress.com/2015/02/21/peering-into-the-black-box-visualizing-lambdamart/} |
| 72 |
} |
| 73 |
\examples{ |
| 74 |
|
| 75 |
data(agaricus.train, package = "xgboost") |
| 76 |
|
| 77 |
## Keep the number of threads to 2 for examples |
| 78 |
nthread <- 2 |
| 79 |
data.table::setDTthreads(nthread) |
| 80 |
|
| 81 |
model <- xgboost( |
| 82 |
agaricus.train$data, factor(agaricus.train$label), |
| 83 |
nrounds = 30, |
| 84 |
verbosity = 0L, |
| 85 |
nthreads = nthread, |
| 86 |
max_depth = 15, |
| 87 |
learning_rate = 1, |
| 88 |
min_child_weight = 50 |
| 89 |
) |
| 90 |
|
| 91 |
p <- xgb.plot.multi.trees(model, features_keep = 3) |
| 92 |
print(p) |
| 93 |
|
| 94 |
# Below is an example of how to save this plot to a file. |
| 95 |
if (require("DiagrammeR") && require("DiagrammeRsvg") && require("rsvg")) { |
| 96 |
fname <- file.path(tempdir(), "tree.pdf") |
| 97 |
gr <- xgb.plot.multi.trees(model, features_keep = 3, render = FALSE) |
| 98 |
export_graph(gr, fname, width = 1500, height = 600) |
| 99 |
} |
| 100 |
} |