如何使用 R 在随机森林中生成决策树图和变量重要性图?
How do I generate a Decision Tree plot and a Variable Importance plot in Random Forest using R?
我是数据科学的新手,我正在使用随机森林算法进行机器学习分析以执行分类。我的数据集中的目标变量称为 Attrition (Yes/No).
我对如何在随机森林中生成这两个图感到有点困惑`:
(1) Feature Importance Plot
(2) Decision Tree Plot
我了解到随机森林是数据集中多个决策树模型的集合。
假设我的训练数据集称为 TrainDf
,我的测试数据集称为 TestDf
,我如何在 R 中创建这两个图?
更新:从这两个帖子来看,它们似乎无法完成,或者我在这里遗漏了什么?
为什么单树随机森林比决策树分类器好得多?
How would you interpret an ensemble tree model?
要绘制变量重要性,您可以使用以下代码。
mtcars.rf <- randomForest(am ~ ., data=mtcars, ntree=1000, keep.forest=FALSE,
importance=TRUE)
varImpPlot(mtcars.rf)
特征重要性图 with ggplot2
,
library(randomForest)
library(ggplot2)
mtcars.rf <- randomForest(vs ~ ., data=mtcars)
imp <- cbind.data.frame(Feature=rownames(mtcars.rf$importance),mtcars.rf$importance)
g <- ggplot(imp, aes(x=reorder(Feature, -IncNodePurity), y=IncNodePurity))
g + geom_bar(stat = 'identity') + xlab('Feature')
带有 igraph
的决策树图(来自随机森林的树)
tree <- randomForest::getTree(mtcars.rf, k=1, labelVar=TRUE) # get the 1st decision tree with k=1
tree$`split var` <- as.character(tree$`split var`)
tree$`split point` <- as.character(tree$`split point`)
tree[is.na(tree$`split var`),]$`split var` <- ''
tree[tree$`split point` == '0',]$`split point` <- ''
library(igraph)
gdf <- data.frame(from = rep(rownames(tree), 2),
to = c(tree$`left daughter`, tree$`right daughter`))
g <- graph_from_data_frame(gdf, directed=TRUE)
V(g)$label <- paste(tree$`split var`, '\r\n(', tree$`split point`, ',', round(tree$prediction,2), ')')
g <- delete_vertices(g, '0')
print(g, e=TRUE, v=TRUE)
plot(g, layout = layout.reingold.tilford(g, root=1), vertex.size=5, vertex.color='cyan')
从下图中可以看出,决策树中每个节点的label代表了该节点选择的拆分变量名,(拆分值,[=37=]与label的比例1) 在那个节点。
同样,第 100 棵树可以通过 k=100
和 randomForest::getTree()
函数获得,如下所示
我是数据科学的新手,我正在使用随机森林算法进行机器学习分析以执行分类。我的数据集中的目标变量称为 Attrition (Yes/No).
我对如何在随机森林中生成这两个图感到有点困惑`:
(1) Feature Importance Plot
(2) Decision Tree Plot
我了解到随机森林是数据集中多个决策树模型的集合。
假设我的训练数据集称为 TrainDf
,我的测试数据集称为 TestDf
,我如何在 R 中创建这两个图?
更新:从这两个帖子来看,它们似乎无法完成,或者我在这里遗漏了什么? 为什么单树随机森林比决策树分类器好得多?
How would you interpret an ensemble tree model?
要绘制变量重要性,您可以使用以下代码。
mtcars.rf <- randomForest(am ~ ., data=mtcars, ntree=1000, keep.forest=FALSE,
importance=TRUE)
varImpPlot(mtcars.rf)
特征重要性图 with ggplot2
,
library(randomForest)
library(ggplot2)
mtcars.rf <- randomForest(vs ~ ., data=mtcars)
imp <- cbind.data.frame(Feature=rownames(mtcars.rf$importance),mtcars.rf$importance)
g <- ggplot(imp, aes(x=reorder(Feature, -IncNodePurity), y=IncNodePurity))
g + geom_bar(stat = 'identity') + xlab('Feature')
带有 igraph
的决策树图(来自随机森林的树)
tree <- randomForest::getTree(mtcars.rf, k=1, labelVar=TRUE) # get the 1st decision tree with k=1
tree$`split var` <- as.character(tree$`split var`)
tree$`split point` <- as.character(tree$`split point`)
tree[is.na(tree$`split var`),]$`split var` <- ''
tree[tree$`split point` == '0',]$`split point` <- ''
library(igraph)
gdf <- data.frame(from = rep(rownames(tree), 2),
to = c(tree$`left daughter`, tree$`right daughter`))
g <- graph_from_data_frame(gdf, directed=TRUE)
V(g)$label <- paste(tree$`split var`, '\r\n(', tree$`split point`, ',', round(tree$prediction,2), ')')
g <- delete_vertices(g, '0')
print(g, e=TRUE, v=TRUE)
plot(g, layout = layout.reingold.tilford(g, root=1), vertex.size=5, vertex.color='cyan')
从下图中可以看出,决策树中每个节点的label代表了该节点选择的拆分变量名,(拆分值,[=37=]与label的比例1) 在那个节点。
同样,第 100 棵树可以通过 k=100
和 randomForest::getTree()
函数获得,如下所示