| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/xgb.model.dt.tree.R |
| 3 |
\name{xgb.model.dt.tree} |
| 4 |
\alias{xgb.model.dt.tree} |
| 5 |
\title{Parse model text dump} |
| 6 |
\usage{ |
| 7 |
xgb.model.dt.tree(model, trees = NULL, use_int_id = FALSE, ...) |
| 8 |
} |
| 9 |
\arguments{ |
| 10 |
\item{model}{Object of class \code{xgb.Booster}. If it contains feature names (they can |
| 11 |
be set through \code{\link[=setinfo]{setinfo()}}), they will be used in the output from this function. |
| 12 |
|
| 13 |
If the model contains categorical features, an error will be thrown.} |
| 14 |
|
| 15 |
\item{trees}{An integer vector of (base-1) tree indices that should be used. The default |
| 16 |
(\code{NULL}) uses all trees. Useful, e.g., in multiclass classification to get only |
| 17 |
the trees of one class.} |
| 18 |
|
| 19 |
\item{use_int_id}{A logical flag indicating whether nodes in columns "Yes", "No", and |
| 20 |
"Missing" should be represented as integers (when \code{TRUE}) or as "Tree-Node" |
| 21 |
character strings (when \code{FALSE}, default).} |
| 22 |
|
| 23 |
\item{...}{Not used. |
| 24 |
|
| 25 |
Some arguments that were part of this function in previous XGBoost versions are currently |
| 26 |
deprecated or have been renamed. If a deprecated or renamed argument is passed, will throw |
| 27 |
a warning (by default) and use its current equivalent instead. This warning will become an |
| 28 |
error if using the \link[=xgboost-options]{'strict mode' option}. |
| 29 |
|
| 30 |
If some additional argument is passed that is neither a current function argument nor |
| 31 |
a deprecated or renamed argument, a warning or error will be thrown depending on the |
| 32 |
'strict mode' option. |
| 33 |
|
| 34 |
\bold{Important:} \code{...} will be removed in a future version, and all the current |
| 35 |
deprecation warnings will become errors. Please use only arguments that form part of |
| 36 |
the function signature.} |
| 37 |
} |
| 38 |
\value{ |
| 39 |
A \code{data.table} with detailed information about tree nodes. It has the following columns: |
| 40 |
\itemize{ |
| 41 |
\item \code{Tree}: integer ID of a tree in a model (zero-based index). |
| 42 |
\item \code{Node}: integer ID of a node in a tree (zero-based index). |
| 43 |
\item \code{ID}: character identifier of a node in a model (only when \code{use_int_id = FALSE}). |
| 44 |
\item \code{Feature}: for a branch node, a feature ID or name (when available); |
| 45 |
for a leaf node, it simply labels it as \code{"Leaf"}. |
| 46 |
\item \code{Split}: location of the split for a branch node (split condition is always "less than"). |
| 47 |
\item \code{Yes}: ID of the next node when the split condition is met. |
| 48 |
\item \code{No}: ID of the next node when the split condition is not met. |
| 49 |
\item \code{Missing}: ID of the next node when the branch value is missing. |
| 50 |
\item \code{Gain}: either the split gain (change in loss) or the leaf value. |
| 51 |
\item \code{Cover}: metric related to the number of observations either seen by a split |
| 52 |
or collected by a leaf during training. |
| 53 |
} |
| 54 |
|
| 55 |
When \code{use_int_id = FALSE}, columns "Yes", "No", and "Missing" point to model-wide node identifiers |
| 56 |
in the "ID" column. When \code{use_int_id = TRUE}, those columns point to node identifiers from |
| 57 |
the corresponding trees in the "Node" column. |
| 58 |
} |
| 59 |
\description{ |
| 60 |
Parse a boosted tree model text dump into a \code{data.table} structure. |
| 61 |
} |
| 62 |
\details{ |
| 63 |
Note that this function does not work with models that were fitted to |
| 64 |
categorical data, and is only applicable to tree-based boosters (not \code{gblinear}). |
| 65 |
} |
| 66 |
\examples{ |
| 67 |
# Basic use: |
| 68 |
|
| 69 |
data(agaricus.train, package = "xgboost") |
| 70 |
## Keep the number of threads to 1 for examples |
| 71 |
nthread <- 1 |
| 72 |
data.table::setDTthreads(nthread) |
| 73 |
|
| 74 |
bst <- xgb.train( |
| 75 |
data = xgb.DMatrix(agaricus.train$data, label = agaricus.train$label, nthread = 1), |
| 76 |
nrounds = 2, |
| 77 |
params = xgb.params( |
| 78 |
max_depth = 2, |
| 79 |
nthread = nthread, |
| 80 |
objective = "binary:logistic" |
| 81 |
) |
| 82 |
) |
| 83 |
|
| 84 |
# This bst model already has feature_names stored with it, so those would be used when |
| 85 |
# feature_names is not set: |
| 86 |
dt <- xgb.model.dt.tree(bst) |
| 87 |
|
| 88 |
# How to match feature names of splits that are following a current 'Yes' branch: |
| 89 |
merge( |
| 90 |
dt, |
| 91 |
dt[, .(ID, Y.Feature = Feature)], by.x = "Yes", by.y = "ID", all.x = TRUE |
| 92 |
)[ |
| 93 |
order(Tree, Node) |
| 94 |
] |
| 95 |
|
| 96 |
} |