2
我讀的例子結果XGB notebook和xgb.plot.tree命令在PIC這樣的: xgb.plot.tree佈局
然而,當我做同樣的事情我有一個像這樣的圖像,它們是兩個單獨的圖形,也是不同的顏色。
是正常嗎?兩棵樹是兩棵樹嗎?
我讀的例子結果XGB notebook和xgb.plot.tree命令在PIC這樣的: xgb.plot.tree佈局
然而,當我做同樣的事情我有一個像這樣的圖像,它們是兩個單獨的圖形,也是不同的顏色。
是正常嗎?兩棵樹是兩棵樹嗎?
我有同樣的問題。 根據xgboost github存儲庫上的問題情況,這可能是由於xgboost用於呈現樹的DiagrammeR庫的更改所致。 https://github.com/dmlc/xgboost/issues/2640
相反修改與製圖命令「dgr_graph」對象的,我選擇創建函數xgb.plot.tree直接定義節點的字體的顏色的新版本。在nodes <- DiagrammeR::create_node_df
行中添加參數fontcolor="black"
就足夠了
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)
}
Then, it remains to change some parameters to improve the readibility of the graph. Below I add an example of the code I use to display the first tree of my xgboost model.
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)
}