mlr: retrieve output of generateFilterValuesData within CV loop

大城市里の小女人 提交于 2021-01-28 06:44:12

问题


If I fuse a learner with a filter method using makeFilterWrapper, then I know I can perform feature selection using that filter within a cross-validation loop. As I understand it, filterFeatures is called before each model fit and it calls generateFilterValuesData. But is it possible to retrieve the values generated by generateFilterValuesData, using that filter, within each iteration of cross validation?

For example:

library(survival)
library(mlr)

data(veteran)
set.seed(24601)
configureMlr(show.learner.output=TRUE, show.info=TRUE)

task_id = "MAS"
mas.task <- makeSurvTask(id = task_id, data = veteran, target = c("time", "status"))
mas.task <- createDummyFeatures(mas.task)

inner = makeResampleDesc("CV", iters=2, stratify=TRUE)  # Tuning
outer = makeResampleDesc("CV", iters=3, stratify=TRUE)  # Benchmarking

cox.lrn <- makeLearner(cl="surv.coxph", id = "coxph", predict.type="response")
cox.filt.uni.abs.lrn = 
  makeFilterWrapper(
    makeLearner(cl="surv.coxph", id = "cox.filt.uni.abs", predict.type="response"), 
    fw.method="univariate.model.score", 
    fw.abs=7,
    perf.learner=cox.lrn
  )

learners = list( cox.filt.uni.abs.lrn )  
bmr = benchmark(learners=learners, tasks=mas.task, resamplings=outer, measures=list(cindex), show.info = TRUE)

mods = getBMRModels(bmr, learner.ids = c('cox.filt.uni.abs.filtered'))
for (i in 1:length(mods[[task_id]]$cox.filt.uni.abs.filtered)) {
  mod = mods$MAS$cox.filt.uni.abs.filtered[[i]]$learner.model[[1]]
  print(str(mod, max.level=1))
  **#Retrieve output of generateFilterValuesData here?**
}

回答1:


You can use the extract slot within resample() in combination with getFilteredFeatures().

library(mlr)
#> Loading required package: ParamHelpers

lrn = makeFilterWrapper(learner = "classif.ksvm", fw.method = "variance",
                        fw.abs = 5)
rdesc = makeResampleDesc("CV", iters = 2)
res = resample(lrn, spam.task, rdesc, extract = getFilteredFeatures)
#> Resampling: cross-validation
#> Measures:             mmce
#> [Resample] iter 1:    0.1808696
#> [Resample] iter 2:    0.1994785
#> 
#> Aggregated Result: mmce.test.mean=0.1901740
#> 
res$extract
#> [[1]]
#> [1] "you"          "george"       "capitalAve"   "capitalLong" 
#> [5] "capitalTotal"
#> 
#> [[2]]
#> [1] "you"          "george"       "capitalAve"   "capitalLong" 
#> [5] "capitalTotal"

Created on 2019-08-07 by the reprex package (v0.3.0)



来源:https://stackoverflow.com/questions/57389203/mlr-retrieve-output-of-generatefiltervaluesdata-within-cv-loop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!