如果我想使用 mlr 包对新数据进行预测,我该如何预处理新数据,以便使用原始数据预处理所需的信息。EG 如果我合并小因子水平并且新数据集中的频率与第一个数据集不同,则生成的因子水平可能不同并且无法进行预测。注意:我在这里假设在训练模型时新数据尚不可用,这不是关于测试数据,而是关于预测新数据。那么在 mlr 中应该如何对新数据进行预处理呢?这是一个示例,我创建了一个新任务来预处理导致错误的新数据集:
library(mlr)
a <- data.frame(y=factor(c(1,1,1,1,1,1,1,1,0,0,1,0)),
x1=rep(c("a","b", "c"), times=c(10,1,1)))
# most frequent x1 factor is "a"
aTask <- makeClassifTask(data = a, target = "y", positive="1")
aTask <- mergeSmallFactorLevels(aTask, cols=c("x1"), min.perc=0.1)
# combines "b" and "c" into factor ".merged"
getTaskData(aTask)
aLearner <- makeLearner("classif.rpart", predict.type = "prob")
model <- train(aLearner, aTask)
b <- data.frame(y=factor(c(1,0,1,1,1,1,1,1,0,0,1,0)),
x1=rep(c("a","b", "c"), times=c(1,10,1)))
# most frequent x1 factor is "b"
# target would be made up, because at this stage there would be now target
# variable availabel
newdataTask <- makeClassifTask(data = b, target = "y", positive="1")
newdataTask <- mergeSmallFactorLevels(newdataTask, cols="x1",
min.perc = 0.1)
# combines "a" and "c" into factor ".merged"
getTaskData(newdataTask)
pred <- predict(model, newdataTask)
#Error in model.frame.default(Terms, newdata, na.action = na.action,
# xlev = attr(object, :
#Faktor 'x1' hat neue Stufen b (= factor 'x1' has new level b)
我的解决方案的另一个问题是,一项新任务似乎需要一个目标变量,该目标变量不适用于新数据集。