【发布时间】:2020-09-02 17:05:24
【问题描述】:
我们以鸢尾花数据集为例。
library(randomForest)
data(iris)
smp_size <- floor(0.75 * nrow(iris))
train_ind <- sample(seq_len(nrow(iris)), size = smp_size)
train <- iris[train_ind, ]
test <- iris[-train_ind, ]
model <- randomForest(Species~., data = train, ntree=10)
如果我使用 randomForest 包中的 getTree() 函数,我可以毫无问题地提取例如第三棵树。
treefit <- getTree(model, 3)
但是,例如,我如何使用它(即 treefit)对测试集进行预测?像“predict()”,有没有一个函数可以直接做到这一点?
提前谢谢你
【问题讨论】:
-
如果你真的想使用那棵树,你将不得不使用底层的 c 代码,github.com/cran/randomForest/blob/master/R/… .. 我认为使用 predict.all 的答案是一个很好的解决方法..跨度>
标签: r machine-learning classification random-forest decision-tree