【发布时间】:2017-04-27 15:40:49
【问题描述】:
对不起,如果已经问过了,但我在半小时内找不到它,所以如果你能指出一些方向,我将不胜感激。
我在模型中缺少对象时遇到了麻烦,而我在构建模型时实际上并没有使用这个对象,它只是存在于数据集中。 (如下例所示)。
这是一个问题,因为我已经训练了一些射频模型,我正在将模型加载到环境中,并且我正在按原样重复使用它们。测试数据集不包含构建模型的数据集中存在的一些变量,但它们并未在模型本身中使用!
library(randomForest)
data(iris)
smp_size <- floor(0.75*nrow(iris))
set.seed(123)
train_ind <- sample(seq_len(nrow(iris)), size = smp_size)
train <- iris[train_ind, ]
test <- iris[-train_ind, ]
test$Sepal.Length <- NULL # for the sake of example I drop this column
rf_model <- randomForest(Species ~ . - Sepal.Length, # I don't use the column in training model
data = train)
rf_prediction <- predict(rf_model, newdata = test)
当我尝试对测试数据集进行预测时,出现错误:
Error in eval(expr, envir, enclos) : object 'Sepal.Length' not found
我希望实现的是使用我已经构建的模型,因为在不丢失变量的情况下重做它们会很昂贵。
感谢您的建议!
【问题讨论】:
-
我想到的最佳解决方案是通过这种方式从模型中的输入数据集中删除列:
rf_model <- randomForest(Species ~ . - Sepal.Length, data = train[,-which(colnames(data) %in% c('Sepal.Length'))]但这需要在训练模型时而不是之后指定:/
标签: r random-forest