How to get around randomForest Error in R (Predictors in new data do not match)

北战南征 提交于 2019-12-25 18:51:34

问题


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.

回答1:


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.



来源:https://stackoverflow.com/questions/42033763/how-to-get-around-randomforest-error-in-r-predictors-in-new-data-do-not-match

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!