【问题标题】:How to get around randomForest Error in R (Predictors in new data do not match)如何解决 R 中的 randomForest 错误(新数据中的预测变量不匹配)
【发布时间】:2017-02-03 22:06:24
【问题描述】:

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

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

【问题讨论】:

    标签: r random-forest


    【解决方案1】:

    这可能是因为test1 中的一个预测变量是一个因子变量,其值在原始数据集中不存在。例如,如果titanic 有一个名为group 的列可以有AB 的值,但test1$group 可以有C 的值,那么您会收到该错误。

    例如:

    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)
    

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

    【讨论】:

    • 或者如果训练集和测试集的水平不同。假设titanic$group 有“A”和“B”级,但test1$group 是只有“A”级的因素。也就是说,除了导致这种情况的额外关卡之外,还可能缺少关卡。
    猜你喜欢
    • 2014-09-09
    • 2015-10-31
    • 2014-07-20
    • 2018-07-12
    • 2020-01-29
    • 2013-04-16
    • 2021-05-04
    • 1970-01-01
    • 2019-08-08
    相关资源
    最近更新 更多