Plot a boosted tree model
Read a tree model text dump. Plotting only works for boosted tree model (not linear model).
xgb.plot.tree(feature_names = NULL, filename_dump = NULL, model = NULL, n_first_tree = NULL, CSSstyle = NULL, width = NULL, height = NULL)
- names of each feature as a character vector. Can be extracted from a sparse matrix (see example). If model dump already contains feature names, this argument should be
- the path to the text file storing the model. Model dump must include the gain per feature and per tree (parameter
with.stats = Tin function
xgb.dump). Possible to provide a model directly (see
- generated by the
xgb.trainfunction. Avoid the creation of a dump file.
- limit the plot to the n first trees. If
NULL, all trees of the model are plotted. Performance can be low for huge models.
charactervector storing a css style to customize the appearance of nodes. Look at the Mermaid wiki for more information.
- the width of the diagram in pixels.
- the height of the diagram in pixels.
The content of each node is organised that way:
cover: the sum of second order gradient of training data classified to the leaf, if it is square loss, this simply corresponds to the number of instances in that branch. Deeper in the tree a node is, lower this metric will be ;
gain: metric the importance of the node in the model.
Each branch finishes with a leaf. For each leaf, only the
cover is indicated.
It uses Mermaid library for that purpose.
DiagrammeRof the model.
data(agaricus.train, package='xgboost') #Both dataset are list with two items, a sparse matrix and labels #(labels = outcome column which will be learned). #Each column of the sparse Matrix is a feature in one hot encoding format. train <- agaricus.train bst <- xgboost(data = train$data, label = train$label, max.depth = 2, eta = 1, nthread = 2, nround = 2,objective = "binary:logistic") #agaricus.test$data@Dimnames[] represents the column names of the sparse matrix. xgb.plot.tree(agaricus.train$data@Dimnames[], model = bst)