R 在随机森林包中的500棵树中画一棵
在R中,R 在随机森林包中的500棵树中画一棵,r,machine-learning,classification,random-forest,R,Machine Learning,Classification,Random Forest,在R中,randomForest函数的输出中如何以相同的名称绘制树?例如,我使用iris数据,并希望绘制500个输出树中的第一棵树。我的代码是 model <-randomForest(Species~.,data=iris,ntree=500) model您可以使用randomForest包中的getTree()函数(官方指南:) 在iris数据集中: require(randomForest) data(iris) ## we have a look at the k-th tre
randomForest
函数的输出中如何以相同的名称绘制树?例如,我使用iris
数据,并希望绘制500个输出树中的第一棵树。我的代码是
model <-randomForest(Species~.,data=iris,ntree=500)
model您可以使用randomForest
包中的getTree()
函数(官方指南:)
在iris
数据集中:
require(randomForest)
data(iris)
## we have a look at the k-th tree in the forest
k <- 10
getTree(randomForest(iris[, -5], iris[, 5], ntree = 10), k, labelVar = TRUE)
require(随机林)
数据(iris)
##我们来看看森林里的第k棵树
k您可以使用cforest
进行如下绘图,我已将值硬编码为5,您可以根据需要进行更改
ntree <- 5
library("party")
cf <- cforest(Species~., data=iris,controls=cforest_control(ntree=ntree))
for(i in 1:ntree){
pt <- prettytree(cf@ensemble[[i]], names(cf@data@get("input")))
nt <- new("Random Forest BinaryTree")
nt@tree <- pt
nt@data <- cf@data
nt@responses <- cf@responses
pdf(file=paste0("filex",i,".pdf"))
plot(nt, type="simple")
dev.off()
}
ntree下面的答案有帮助吗@阿维图斯:谢谢你的回答,但我无法解开。你能给我一个在randomForest函数输出中绘制第一棵树的简单代码吗?我在下面为randomForest
包添加了一个答案;其他软件包,如party
-允许类似功能如何绘制此树?请将绘图代码添加到上述代码中。thanks@Amin上面的代码为您提供了所选的树(k谢谢您的回答。我想使用randomForest
,我可以使用cforest
而不是randomForest
?