首页 > 解决方案 > Predict() 仅返回带有随机森林的 NA

问题描述

我使用随机森林构建了一个模型,并尝试使用 predict() 在另一个数据库上对其进行测试。但是,它只返回 NA。

RF=randomForest(intention~., data=train,ntree=1000,na.action=na.roughfix) 
#no NA in the train nor the test dataset

# Predicting
pred <-predict(RF, newdata=test,type="response")
#pred vector is only set to NA

我检查了这个页面并检查了我的数据集没有 NA。但是,我继续获得相同的回报。 https://www.kaggle.com/c/the-analytics-edge-mit-15-071x/discussion/7808

我也检查了这个页面,但对于随机森林来说似乎并不准确(或者我不明白)。 r - loess 预测返回 NA

感谢您的帮助 !

标签: rrandom-forestnapredict

解决方案


正如@Allan Cameron 猜测的那样,问题来自数据集的不对称性。在运行 RF 算法时遇到问题,我在这个论坛上找到了一个建议,使用以下代码删除值太少的变量。

index <- c()
 for (j in (1 : 41))   {
   if (is.numeric(train[ ,j])  &  length(unique(as.numeric(train[ ,j]))) == 1 )
     {index <- append(index,j)}
train <- train[ ,-index]
#ran on test dataset too

但是,我没有看到它在火车上删除了 5 列,在测试中删除了 9 列。函数 predict() 尝试将使用 51 个变量构建的模型应用于具有 47 个变量的数据集返回 NA 但没有错误。


推荐阅读