在测试数据集上进行预测时随机森林模型中缺少对象
Missing object in randomForest model when predicting on test dataset
抱歉,如果已经有人问过,但我找了半个小时没找到,所以如果你能给我指明方向,我将不胜感激。
我遇到了模型中缺少对象的问题,虽然我在构建模型时实际上并没有使用这个对象,它只是存在于数据集中。 (如下例所示)。
这是个问题,因为我已经训练了一些射频模型,我正在将模型加载到环境中,然后按原样重用它们。测试数据集不包含构建模型的数据集中存在的一些变量,但它们未在模型本身中使用!
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
我希望实现的是使用我已经构建的模型,因为在不丢失变量的情况下重做它们会很昂贵。
感谢指教!
因为你的模型已经建好了。您需要在 运行 模型之前将缺失的列添加回测试集。只需添加值为 0 的缺失列,如下例所示。
library(randomForest)
library(dplyr)
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
rf_model <- randomForest(Species ~ . - Sepal.Length,
data = train)
# adding the missing column to your test set.
missingColumns <- setdiff(colnames(train),colnames(test))
test[,missingColumns] <- 0
rf_prediction <- predict(rf_model, newdata = test)
rf_prediction
#showing this produce the same results
train2 <- iris[train_ind, ]
test2 <- iris[-train_ind, ]
test2$Sepal.Length <- NULL
train2$Sepal.Length <- NULL
rf_model2 <- randomForest(Species ~ .,
data = train2)
rf_prediction2 <- predict(rf_model2, newdata = test2)
rf_prediction2 == rf_prediction
抱歉,如果已经有人问过,但我找了半个小时没找到,所以如果你能给我指明方向,我将不胜感激。
我遇到了模型中缺少对象的问题,虽然我在构建模型时实际上并没有使用这个对象,它只是存在于数据集中。 (如下例所示)。
这是个问题,因为我已经训练了一些射频模型,我正在将模型加载到环境中,然后按原样重用它们。测试数据集不包含构建模型的数据集中存在的一些变量,但它们未在模型本身中使用!
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
我希望实现的是使用我已经构建的模型,因为在不丢失变量的情况下重做它们会很昂贵。
感谢指教!
因为你的模型已经建好了。您需要在 运行 模型之前将缺失的列添加回测试集。只需添加值为 0 的缺失列,如下例所示。
library(randomForest)
library(dplyr)
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
rf_model <- randomForest(Species ~ . - Sepal.Length,
data = train)
# adding the missing column to your test set.
missingColumns <- setdiff(colnames(train),colnames(test))
test[,missingColumns] <- 0
rf_prediction <- predict(rf_model, newdata = test)
rf_prediction
#showing this produce the same results
train2 <- iris[train_ind, ]
test2 <- iris[-train_ind, ]
test2$Sepal.Length <- NULL
train2$Sepal.Length <- NULL
rf_model2 <- randomForest(Species ~ .,
data = train2)
rf_prediction2 <- predict(rf_model2, newdata = test2)
rf_prediction2 == rf_prediction