如果我想使用mlr包对新数据进行预测,我该如何预处理新数据,以便使用原始数据预处理所需的信息。例如。如果我合并小因子水平并且新数据集中的频率与第一数据集不同,则得到的因子水平可能不同并且不可能进行预测。注意:我在这里假设在训练模型时新数据尚不可用,这不是关于测试数据,而是关于预测新数据。那么如何预处理新数据应该在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)
我的解决方案的另一个问题是新任务似乎需要一个目标变量,该变量不适用于新数据集。
答案 0 :(得分:2)
mlr
并不提供任何自动执行此操作的功能,但您可以轻松检查已替换的因子级别,并在新数据中重新命名:
library(plyr)
to.replace = setdiff(levels(b$x1), levels(getTaskData(aTask)$x1))
b$x1 = mapvalues(b$x1, from = to.replace, to = rep(".merged", times = length(to.replace)))
完整示例:
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)))
aTask = makeClassifTask(data = a, target = "y", positive="1")
aTask = mergeSmallFactorLevels(aTask, cols=c("x1"), min.perc=0.1)
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)))
library(plyr)
to.replace = setdiff(levels(b$x1), levels(getTaskData(aTask)$x1))
b$x1 = mapvalues(b$x1, from = to.replace, to = rep(".merged", times = length(to.replace)))
newdataTask = makeClassifTask(data = b, target = "y", positive="1")
pred = predict(model, newdataTask)
对于这样的事情,将学习者与预处理融合起来往往更好,以便在训练和预测时透明且自动地发生。在这种情况下,一个完整的例子看起来像这样:
lrn = makeLearner("classif.rpart", predict.type = "prob")
trainfun = function(data, target, args) {
task = makeClassifTask(data = data, target = target, positive = "1")
new.task = mergeSmallFactorLevels(task, cols = c("x1"), min.perc = 0.1)
return(list(data = getTaskData(new.task), control = list(levels(getTaskData(new.task)$x1))))
}
predictfun = function(data, target, args, control) {
library(plyr)
to.replace = setdiff(levels(data$x1), control[[1]])
data$x1 = mapvalues(data$x1, from = to.replace, to = rep(".merged", times = length(to.replace)))
return(data)
}
lrn = makePreprocWrapper(lrn, train = trainfun, predict = predictfun)
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)))
aTask = makeClassifTask(data = a, target = "y", positive="1")
model = train(lrn, 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)))
newdataTask = makeClassifTask(data = b, target = "y", positive = "1")
pred = predict(model, newdataTask)
这只是一个概念证明 - 您可能想要指定应该处理哪些功能以及阈值应该是什么的参数,并调整predictfun
代码来处理任意数字已处理的功能。