MLR:我如何在学习者周围包装指定功能的选择?

时间:2018-03-28 13:06:50

标签: r feature-selection mlr

我想比较简单的逻辑回归模型,其中每个模型仅考虑一组指定的特征。我想在数据的重新采样上对这些回归模型进行比较。

R包mlr允许我使用dropFeatures在任务级别选择列。代码类似于:

full_task = makeClassifTask(id = "full task", data = my_data, target = "target")
reduced_task = dropFeatures(full_task, setdiff( getTaskFeatureNames(full_task), list_feat_keep))

然后我可以进行基准测试,我有一个任务列表。

lrn = makeLearner("classif.logreg", predict.type = "prob") 
rdesc = makeResampleDesc(method = "Bootstrap", iters = 50, stratify = TRUE)
bmr = benchmark(lrn, list(full_task, reduced_task), rdesc, measures = auc, show.info = FALSE)

如何生成仅考虑一组指定功能的学习者。 据我所知,过滤器或选择方法总是应用一些统计 过程但不允许直接选择功能。谢谢!

1 个答案:

答案 0 :(得分:4)

第一个解决方案是懒惰的,也不是最佳解决方案,因为仍然执行过滤器计算:

library(mlr)
task = sonar.task
sel.feats = c("V1", "V10")
lrn = makeLearner("classif.logreg", predict.type = "prob")
lrn.reduced = makeFilterWrapper(learner = lrn, fw.method = "variance", fw.abs = 2, fw.mandatory.feat = sel.feats)
bmr = benchmark(list(lrn, lrn.reduced), task, cv3, measures = auc, show.info = FALSE)

第二个使用预处理包装器来过滤数据,应该是最快的解决方案,也更灵活:

lrn.reduced.2 = makePreprocWrapper(
  learner = lrn, 
  train = function(data, target, args) list(data = data[, c(sel.feats, target)], control = list()),
  predict = function(data, target, args, control) data[, sel.feats]
)
bmr = benchmark(list(lrn, lrn.reduced.2), task, cv3, measures = auc, show.info = FALSE)