跳到内容

可视化与树叶深度相关的分布。

  • xgb.plot.deepness() 使用基础 R 图形,而

  • xgb.ggplot.deepness() 使用 "ggplot2"。

用法

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,
  ...
)

参数

model

可以是 xgb.Booster 模型,也可以是 xgb.model.dt.tree() 返回的 "data.table"。

which

要绘制哪个分布(详见说明)。

plot

是否显示图表?默认值为 TRUE

...

传递给 graphics::barplot()graphics::plot() 的其他参数。

这两个函数的返回值如下:

  • xgb.plot.deepness(): 一个 "data.table"(不可见)。每一行对应模型中的一个终末叶子。它包含叶子的深度、覆盖度(cover)和权重(用于计算预测)信息。如果 plot = TRUE,也会显示一个图。

  • xgb.ggplot.deepness(): 当 which = "2x1" 时,返回一个包含两个 "ggplot" 对象的列表;否则返回一个 "ggplot" 对象。

详细信息

which = "2x1" 时,绘制两个关于叶子深度的分布,相互叠加:

  1. 在特定深度,树模型中叶子数量的分布。

  2. 在特定深度,落入叶子中的观测值(“覆盖度”)的平均加权数量的分布。

这些可能有助于确定 max_depthmin_child_weight 参数的合理范围。

which = "max.depth"which = "med.depth" 时,将创建每个树的最大深度或中位数深度相对于树编号的图。

最后,which = "med.weight" 允许查看树的叶子中位数绝对权重如何随迭代次数而变化。

这些函数的灵感来自博客文章 https://github.com/aysent/random-forest-leaf-visualization

另请参阅

示例


data(agaricus.train, package = "xgboost")
## Keep the number of threads to 2 for examples
nthread <- 2
data.table::setDTthreads(nthread)

## Change max_depth to a higher number to get a more significant result
model <- xgboost(
  agaricus.train$data, factor(agaricus.train$label),
  nrounds = 50,
  max_depth = 6,
  nthreads = nthread,
  subsample = 0.5,
  min_child_weight = 2
)

xgb.plot.deepness(model)
xgb.ggplot.deepness(model)

xgb.plot.deepness(
  model, which = "max.depth", pch = 16, col = rgb(0, 0, 1, 0.3), cex = 2
)

xgb.plot.deepness(
  model, which = "med.weight", pch = 16, col = rgb(0, 0, 1, 0.3), cex = 2
)