可视化与树叶深度相关的分布。
xgb.plot.deepness()
使用基础 R 图形功能,而xgb.ggplot.deepness()
使用 "ggplot2"。
参数
- model
要么是一个
xgb.Booster
模型,或者是xgb.model.dt.tree()
返回的 "data.table"。- which
要绘制哪个分布(参见详情)。
- plot
是否显示图?默认值为
TRUE
。- ...
传递给
graphics::barplot()
或graphics::plot()
的其他参数。
返回值
这两个函数的返回值如下
xgb.plot.deepness()
: 一个 "data.table"(不可见地)。每一行对应模型中的一个终端叶节点。它包含关于深度、覆盖度 (cover) 和权重 (weight) 的信息(用于计算预测值)。如果plot = TRUE
,也会显示图。xgb.ggplot.deepness()
: 当which = "2x1"
时,返回一个包含两个 "ggplot" 对象的列表,否则返回一个单个的 "ggplot" 对象。
详情
当 which = "2x1"
时,两个与叶节点深度相关的分布图会叠放在一起显示
模型树在特定深度上的叶节点数量分布。
最终落入特定深度叶节点的平均加权观测数量("覆盖度")的分布。
这些有助于确定 max_depth
和 min_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
)