我mlr
在 R 中使用包在二进制分类任务中比较两个学习器,即随机森林和套索分类器。我想提取特征对最佳分类器的重要性,在这种情况下是随机森林,类似于caret::varImp()
. 我遇到了getBMRFeatSelResults()
, getFeatureImportance()
,generateFeatureImportanceData()
但似乎没有一个可以解决问题。这是我使用嵌套重采样执行基准实验的代码。理想情况下,我希望基尼系数平均下降。谢谢你。
library(easypackages)
libraries("mlr","purrr","glmnet","parallelMap","parallel")
data = read.table("data_past.txt", h = T)
set.seed(123)
task = makeClassifTask(id = "past_history", data = data, target = "DIAG", positive = "BD")
ps_rf = makeParamSet(makeIntegerParam("mtry", lower = 4, upper = 16),makeDiscreteParam("ntree", values = 1000))
ps_lasso = makeParamSet(makeNumericParam("s", lower = .01, upper = 1),makeDiscreteParam("alpha", values = 1))
ctrl_rf = makeTuneControlRandom(maxit = 10L)
ctrl_lasso = makeTuneControlRandom(maxit = 100L)
inner = makeResampleDesc("RepCV", fold = 10, reps = 3, stratify = TRUE)
lrn_rf = makeLearner("classif.randomForest", predict.type = "prob", fix.factors.prediction = TRUE)
lrn_rf = makeTuneWrapper(lrn_rf, resampling = inner, par.set = ps_rf, control = ctrl_rf, measures = auc, show.info = FALSE)
lrn_lasso = makeLearner("classif.glmnet", predict.type = "prob", fix.factors.prediction = TRUE)
lrn_lasso = makeTuneWrapper(learner = lrn_lasso, resampling = inner, control = ctrl_lasso, par.set = ps_lasso, measures = auc, show.info = FALSE)
outer = makeResampleDesc("CV", iters = 10, stratify = TRUE)
lrns = list(lrn_rf, lrn_lasso)
parallelStartMulticore(36)
res = benchmark(lrns, task, outer, measures = list(auc, ppv, npv, fpr, tpr, mmce), show.info = FALSE, model = T)
saveRDS(res, file = "res.rds")
parallelStop()
models <- getBMRModels(res, drop = TRUE)