如何解决 R 中的随机森林错误(新数据中的预测器不匹配) [英] How to get around randomForest Error in R (Predictors in new data do not match)

查看:217
本文介绍了如何解决 R 中的随机森林错误(新数据中的预测器不匹配)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难对下面的错误消息进行故障排除.我正在尝试在 titanic 数据集上做一个随机森林模型.有没有办法解决这个错误?有没有代码可以检查树中的级别?

 predict.randomForest(my_rf_model, test1) 中的错误:新数据中的预测变量类型与训练数据不匹配.

解决方案

发生这种情况的原因可能是 test1 中的预测变量之一是一个因子变量,该变量的值不存在于原始数据中放.例如,如果 titanic 有一个名为 group 的列,它的值可以是 AB,但是 test1$group 可以有 C 的值,那么你会得到那个错误.

例如:

数据(虹膜)iris$group = factor(sample(c("A","B"),nrow(iris),replace=TRUE))rf <-随机森林(物种〜.,数据=鸢尾花)newdat = 虹膜newdat$group = "C"预测(射频,新数据=新数据)

<块引用>

predict.randomForest(rf, newdata = newdat) 中的错误:类型新数据中的预测变量与训练数据中的预测变量不匹配.

I am having a hard time troubleshooting the error message below. I am trying to do a random forest model on a titanic data set. Is there a way to get around this error? Is there a code to check the levels in the tree?

Error in predict.randomForest(my_rf_model, test1) : Type of predictors in new data
    do not match that of the training data.

解决方案

This is probably occurring because one of the predictor variables in test1 is a factor variable that has a value not present in the original data set. For example, if titanic has a column called group that can have values A or B, but test1$group can have a value of C, then you would get that error.

For example:

data(iris)
iris$group = factor(sample(c("A","B"), nrow(iris), replace=TRUE))
rf <- randomForest(Species ~ ., data=iris)

newdat = iris
newdat$group = "C"

predict(rf, newdata=newdat)

Error in predict.randomForest(rf, newdata = newdat) : Type of predictors in new data do not match that of the training data.

这篇关于如何解决 R 中的随机森林错误(新数据中的预测器不匹配)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆