R随机森林预测不起作用

时间:2014-06-26 23:46:38

标签: r prediction random-forest

我是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版本存在问题。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

使用预测功能时,您试图预测测试集的结果或标签。

rf_predict <- predict(RF_model, test_set)

您可以使用表函数

创建混淆矩阵以比较随机林的准确性
table(observed, rf_predict)

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

相关问题