[英]R Random Forest prediction not working
I'm new to Random Forests in R, and I'm trying to make a prediction. 我是R语言中的Random Forests的新手,所以我试图做出一个预测。 I have built a Random Forest model using the following code, which works fine 我使用以下代码构建了一个随机森林模型,效果很好
library(randomForest)
RF_model = randomForest(trainrows[,col_truth]~.
,data = trainrows[,cols_to_use]
,ntree=100
,do.trace=T)
If I print out RF_model, I get the following output 如果我打印出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
Then, when I try and make a prediction with the model, I get the following error 然后,当我尝试对模型进行预测时,出现以下错误
> predict(RF_model)
Error in 1:dim(data)[1] : argument of length 0
I have tried supplying data to the predict method, but I get the same error. 我尝试将数据提供给预测方法,但出现相同的错误。 Does anyone know what's going on and how to fix it? 有谁知道发生了什么事以及如何解决它?
EDIT 编辑
In order to provide some more data, I have tried using Random Forests with the iris dataset. 为了提供更多数据,我尝试将虹膜数据集与随机森林配合使用。
rf = randomForest(iris[,1]~., data=iris[,c(1, 2)], ntree=100)
predict(rf)
Error in 1:dim(data)[1] : argument of length 0
This is not related to my data, but a problem with my version of R, I think. 我认为这与我的数据无关,但与我的R版本有关。 Any ideas? 有任何想法吗?
When you use the predict function, you are trying to predict the outcome or labels for your test set. 当您使用预测功能时,您正在尝试预测测试集的结果或标签。
rf_predict <- predict(RF_model, test_set)
You can create a confusion matrix to compare the accuracy of your random forest by using the table function 您可以使用表函数创建混淆矩阵以比较随机森林的准确性
table(observed, rf_predict)
Note: The observed will be the correct labels for the test set 注意:观察到的将是测试集的正确标签
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.