Visualizes distributions related to depth of tree leafs. xgb.plot.deepness uses base R graphics, while xgb.ggplot.deepness uses the ggplot backend.

xgb.ggplot.deepness(model = NULL, which = c("2x1", "max.depth", "med.depth",
  "med.weight"))

xgb.plot.deepness(model = NULL, which = c("2x1", "max.depth", "med.depth",
  "med.weight"), plot = TRUE, ...)

Arguments

model

either an xgb.Booster model generated by the xgb.train function or a data.table result of the xgb.model.dt.tree function.

which

which distribution to plot (see details).

plot

(base R barplot) whether a barplot should be produced. If FALSE, only a data.table is returned.

...

other parameters passed to barplot or plot.

Value

Other than producing plots (when plot=TRUE), the xgb.plot.deepness function silently returns a processed data.table where each row corresponds to a terminal leaf in a tree model, and contains information about leaf's depth, cover, and weight (which is used in calculating predictions).

The xgb.ggplot.deepness silently returns either a list of two ggplot graphs when which="2x1" or a single ggplot graph for the other which options.

Details

When which="2x1", two distributions with respect to the leaf depth are plotted on top of each other:

  • the distribution of the number of leafs in a tree model at a certain depth;

  • the distribution of average weighted number of observations ("cover") ending up in leafs at certain depth.

Those could be helpful in determining sensible ranges of the max_depth and min_child_weight parameters.

When which="max.depth" or which="med.depth", plots of either maximum or median depth per tree with respect to tree number are created. And which="med.weight" allows to see how a tree's median absolute leaf weight changes through the iterations.

This function was inspired by the blog post https://github.com/aysent/random-forest-leaf-visualization.

See also

Examples

data(agaricus.train, package='xgboost') # Change max_depth to a higher number to get a more significant result bst <- xgboost(data = agaricus.train$data, label = agaricus.train$label, max_depth = 6, eta = 0.1, nthread = 2, nrounds = 50, objective = "binary:logistic", subsample = 0.5, min_child_weight = 2)
#> [1] train-error:0.001996 #> [2] train-error:0.002764 #> [3] train-error:0.002764 #> [4] train-error:0.003071 #> [5] train-error:0.003071 #> [6] train-error:0.001996 #> [7] train-error:0.001228 #> [8] train-error:0.001228 #> [9] train-error:0.001228 #> [10] train-error:0.001228 #> [11] train-error:0.001228 #> [12] train-error:0.001228 #> [13] train-error:0.001228 #> [14] train-error:0.001228 #> [15] train-error:0.001228 #> [16] train-error:0.001228 #> [17] train-error:0.001228 #> [18] train-error:0.001228 #> [19] train-error:0.001228 #> [20] train-error:0.001228 #> [21] train-error:0.001228 #> [22] train-error:0.001228 #> [23] train-error:0.001228 #> [24] train-error:0.001228 #> [25] train-error:0.001228 #> [26] train-error:0.001228 #> [27] train-error:0.001228 #> [28] train-error:0.001228 #> [29] train-error:0.001228 #> [30] train-error:0.001228 #> [31] train-error:0.001228 #> [32] train-error:0.001228 #> [33] train-error:0.001228 #> [34] train-error:0.001228 #> [35] train-error:0.001228 #> [36] train-error:0.001228 #> [37] train-error:0.001228 #> [38] train-error:0.001228 #> [39] train-error:0.001228 #> [40] train-error:0.000614 #> [41] train-error:0.000000 #> [42] train-error:0.000000 #> [43] train-error:0.000000 #> [44] train-error:0.000000 #> [45] train-error:0.000000 #> [46] train-error:0.000000 #> [47] train-error:0.000000 #> [48] train-error:0.000000 #> [49] train-error:0.000000 #> [50] train-error:0.000000
xgb.plot.deepness(bst)
#> Error: igraph package is required for plotting the graph deepness.
xgb.ggplot.deepness(bst)
#> Error: igraph package is required for plotting the graph deepness.
xgb.plot.deepness(bst, which='max.depth', pch=16, col=rgb(0,0,1,0.3), cex=2)
#> Error: igraph package is required for plotting the graph deepness.
xgb.plot.deepness(bst, which='med.weight', pch=16, col=rgb(0,0,1,0.3), cex=2)
#> Error: igraph package is required for plotting the graph deepness.