gpt4 book ai didi

r - r 中的 xgb.plot.tree 布局

转载 作者:行者123 更新时间:2023-12-02 04:18:16 25 4
gpt4 key购买 nike

我正在读 xgb notebook示例中的 xgb.plot.tree 命令会生成如下图片: enter image description here

但是,当我做同样的事情时,我得到了这样的图片,它是两个单独的图表,而且颜色也不同。

enter image description here

这正常吗?这两个图是两棵树吗?

最佳答案

我也有同样的问题。根据 xgboost github 存储库上的问题案例,这可能是由于 xgboost 用于渲染树的 DiametermeR 库发生了更改。 https://github.com/dmlc/xgboost/issues/2640

而不是修改 dgr_graph带有diagrammeR命令的对象,我选择创建函数的新版本xgb.plot.tree直接定义节点字体的颜色。添加参数 fontcolor="black" 就足够了在 nodes <- DiagrammeR::create_node_df线

    xgb.plot.tree  <- function (feature_names = NULL, model = NULL, n_first_tree = NULL, 
plot_width = NULL, plot_height = NULL, ...)
{

if (class(model) != "xgb.Booster") {
stop("model: Has to be an object of class xgb.Booster model generaged by the xgb.train function.")
}
if (!requireNamespace("DiagrammeR", quietly = TRUE)) {
stop("DiagrammeR package is required for xgb.plot.tree",
call. = FALSE)
}
allTrees <- xgb.model.dt.tree(feature_names = feature_names,
model = model, n_first_tree = n_first_tree)
allTrees[, `:=`(label, paste0(Feature, "\\nCover: ", Cover,
"\\nGain: ", Quality))]
allTrees[, `:=`(shape, "rectangle")][Feature == "Leaf", `:=`(shape,
"oval")]
allTrees[, `:=`(filledcolor, "Beige")][Feature == "Leaf",
`:=`(filledcolor, "Khaki")]
nodes <- DiagrammeR::create_node_df(n = length(allTrees[,
ID] %>% rev), label = allTrees[, label] %>% rev, style = "filled",
color = "DimGray", fillcolor = allTrees[, filledcolor] %>%
rev, shape = allTrees[, shape] %>% rev, data = allTrees[,
Feature] %>% rev, fontname = "Helvetica", fontcolor="black")
edges <- DiagrammeR::create_edge_df(from = match(allTrees[Feature !=
"Leaf", c(ID)] %>% rep(2), allTrees[, ID] %>% rev), to = match(allTrees[Feature !=
"Leaf", c(Yes, No)], allTrees[, ID] %>% rev), label = allTrees[Feature !=
"Leaf", paste("<", Split)] %>% c(rep("", nrow(allTrees[Feature !=
"Leaf"]))), color = "DimGray", arrowsize = "1.5", arrowhead = "vee",
fontname = "Helvetica", rel = "leading_to")
graph <- DiagrammeR::create_graph(nodes_df = nodes, edges_df = edges)
DiagrammeR::render_graph(graph, width = plot_width, height = plot_height)
}

然后,仍然需要更改一些参数以提高图表的可读性。下面我添加了一个代码示例,用于显示 xgboost 模型的第一棵树。

    xgb.plot.tree  <- function (feature_names = NULL, model = NULL, n_first_tree = NULL, 
plot_width = NULL, plot_height = NULL, ...)
{

if (class(model) != "xgb.Booster") {
stop("model: Has to be an object of class xgb.Booster model generaged by the xgb.train function.")
}
if (!requireNamespace("DiagrammeR", quietly = TRUE)) {
stop("DiagrammeR package is required for xgb.plot.tree",
call. = FALSE)
}
allTrees <- xgb.model.dt.tree(feature_names = feature_names,
model = model, n_first_tree = n_first_tree)

allTrees$Quality <- round(allTrees$Quality, 3)
allTrees$Cover <- round(allTrees$Cover, 3)


allTrees[, `:=`(label, paste0(Feature, "\\nCover: ", Cover,
"\\nGain: ", Quality))]
allTrees[, `:=`(shape, "rectangle")][Feature == "Leaf", `:=`(shape,
"egg")]
allTrees[, `:=`(filledcolor, "Beige")][Feature == "Leaf",
`:=`(filledcolor, "Khaki")]

nodes <- DiagrammeR::create_node_df(n = length(allTrees[,
ID] %>% rev), label = allTrees[, label] %>% rev, style = "filled", width=1.5,
color = "DimGray", fillcolor = allTrees[, filledcolor] %>%
rev, shape = allTrees[, shape] %>% rev, data = allTrees[,
Feature] %>% rev, fontname = "Helvetica", fontcolor="black")

edges <- DiagrammeR::create_edge_df(from = match(allTrees[Feature !=
"Leaf", c(ID)] %>% rep(2), allTrees[, ID] %>% rev), to = match(allTrees[Feature !=
"Leaf", c(Yes, No)], allTrees[, ID] %>% rev), label = allTrees[Feature !=
"Leaf", paste("<", Split)] %>% c(rep("", nrow(allTrees[Feature !=
"Leaf"]))), color = "DimGray", arrowsize = 1, arrowhead = "vee", minlen="5",
fontname = "Helvetica", rel = "leading_to", fontsize="15")

graph <- DiagrammeR::create_graph(nodes_df = nodes, edges_df = edges, attr_theme=NULL)
DiagrammeR::render_graph(graph, width = plot_width, height = plot_height)
return(graph)
}

关于r - r 中的 xgb.plot.tree 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45530189/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com