繁体   English   中英

R随机森林预测无效

[英]R Random Forest prediction not working

我是R语言中的Random Forests的新手,所以我试图做出一个预测。 我使用以下代码构建了一个随机森林模型,效果很好

library(randomForest)
RF_model = randomForest(trainrows[,col_truth]~.
                    ,data = trainrows[,cols_to_use]
                    ,ntree=100
                    ,do.trace=T)

如果我打印出RF_model,则会得到以下输出

Call:
 randomForest(formula = trainrows[, col_truth] ~ ., data = trainrows[,      cols_to_use], ntree = 100, do.trace = T) 
               Type of random forest: classification
                     Number of trees: 100
No. of variables tried at each split: 4

        OOB estimate of  error rate: 19.23%
Confusion matrix:
     0    1 class.error
0 7116 1640   0.1873001
1 1725 7015   0.1973684

然后,当我尝试对模型进行预测时,出现以下错误

> predict(RF_model)
Error in 1:dim(data)[1] : argument of length 0

我尝试将数据提供给预测方法,但出现相同的错误。 有谁知道发生了什么事以及如何解决它?

编辑

为了提供更多数据,我尝试将虹膜数据集与随机森林配合使用。

rf = randomForest(iris[,1]~., data=iris[,c(1, 2)], ntree=100)
predict(rf)
Error in 1:dim(data)[1] : argument of length 0

我认为这与我的数据无关,但与我的R版本有关。 有任何想法吗?

当您使用预测功能时,您正在尝试预测测试集的结果或标签。

rf_predict <- predict(RF_model, test_set)

您可以使用表函数创建混淆矩阵以比较随机森林的准确性

table(observed, rf_predict)

注意:观察到的将是测试集的正确标签

暂无
暂无

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

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