简体   繁体   English

从随机森林中提取一棵树,然后使用提取的树进行预测

[英]Extract a tree from a random forest and then use the extracted tree for prediction

As an example, let's use the iris data set.例如,让我们使用 iris 数据集。

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)

If I use the getTree() function from the randomForest package, I can extract, for example, the third tree without any problem.如果我使用来自 randomForest package 的 getTree() function,我可以毫无问题地提取第三棵树。

treefit <- getTree(model, 3)

But how can I use that (ie treefit) to make predictions on the test set, for instance?但是,例如,我如何使用它(即 treefit)对测试集进行预测? like "predict()", is there a function out there to do that directly?像“predict()”一样,有没有 function 可以直接做到这一点?

Thank you in advance先感谢您

You can use the predict function in the randomForest package directly by setting the predict.all argument to TRUE .您可以通过将predict.all参数设置为TRUE直接在randomForest package 中使用predict function 。

See the following reproducible code for how to use this: also see the help page for predict.randomForest here .有关如何使用它的信息,请参阅以下可重现的代码:另请参阅predict.randomForest的帮助页面。

library(randomForest)
set.seed(1212)
x <- rnorm(100)
y <- rnorm(100, x, 10)
df_train <- data.frame(x=x, y=y)
x_test <- rnorm(20)
y_test <- rnorm(20, x_test, 10)
df_test <- data.frame(x = x_test, y = y_test)
rf_fit <- randomForest(y ~ x, data = df_train, ntree = 500)
# You get a list with the overall predictions and individual tree predictions
rf_pred <- predict(rf_fit, df_test, predict.all = TRUE)
rf_pred$individual[, 3] # Obtains the 3rd tree's predictions on the test data

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM