4

我试图通过使用-package in来获取调整后的支持向量机 (svm)模型的所有预测变量(或变量或特征)的变量重要性。但我不确定,如果我做的评估是正确的。好吧,起初的想法是:e1071::svmmlrR

为了得到一个诚实的调谐 svm 模型,我遵循嵌套重采样教程SpRepCV,在外循环中使用空间 n 折交叉验证( SpCV),在内循环中使用空间交叉验证 ()。作为调整参数gammacost在随机网格搜索中调整。作为所有预测变量的变量重要性评估,我想使用permutation.importance描述相关的,基本上是特征置换和未置换预测之间的聚合差异。

mlr中,有一些过滤函数可以获得变量的重要性,但同时在模型拟合之前根据用户特定的选择输入(阈值或变量数量)创建一个子集。- 但是,我想检索每个拟合模型的所有变量的变量重要性。(我知道学习者random forest具有“包容性”的重要评估)

现在,我在重采样中使用mlr::generateFeatureImportanceDataextract-argument,这看起来很尴尬。所以我问我,如果没有更简单的方法吗?

这是一个使用mlr-development 版本的示例:

## initialize libraries
# devtools::install_github("mlr-org/mlr) # using developper version of mlr
if(!require("pacman")) install.packages("pacman")
pacman::p_load("mlr", "ParamHelpers", "e1071", "parallelMap")


## create tuning setting
svm.ps <- ParamHelpers::makeParamSet(
  ParamHelpers::makeNumericParam("cost", lower = -12, 
                                 upper = 15, trafo = function(x) 2^x),
  ParamHelpers::makeNumericParam("gamma", lower = -15, 
                                 upper = 6, trafo = function(x) 2^x)
)

## create random search grid, small iteration number for example
ctrl.tune <- mlr::makeTuneControlRandom(maxit = 8) 

# inner resampling loop, "
inner <- mlr::makeResampleDesc("SpCV", iters = 3, predict = "both")

# outer loop, "
outer <- mlr::makeResampleDesc("SpRepCV", folds = 5, reps = 2, predict = "both")


## create learner - Support Vector Machine of the e1071-package
lrn.svm <- mlr::makeLearner("classif.svm", predict.type = "prob")

# ... tuning in inner resampling
lrn.svm.tune <- mlr::makeTuneWrapper(learner = lrn.svm, resampling = inner, 
                                     measures = list(auc),
                                     par.set = svm.ps, control = ctrl.tune, 
                                     show.info = FALSE) 


## create function that calculate variable importance based on permutation 
extractVarImpFunction <- function(x)
{
  list(mlr::generateFeatureImportanceData(task = mlr::makeClassifTask(
                          id = x$task.desc$id, 
                          data = mlr::getTaskData(mlr::spatial.task, subset = x$subset), 
                          target = x$task.desc$target,
                          positive = x$task.desc$positive, 
                          coordinates = mlr::spatial.task$coordinates[x$subset,]),
                        method = "permutation.importance", 
                        learner = mlr::makeLearner(cl = "classif.svm", 
                                                     predict.type = "prob", 
                          cost = x$learner.model$opt.result$x$cost,
                          gamma = x$learner.model$opt.result$x$gamma),
                        measure = list(mlr::auc), nmc = 10
                          )$res
      )
}



## start resampling for getting variable importance of tuned models (outer)

# parallelize tuning
parallelMap::parallelStart(mode = "multicore", level = "mlr.tuneParams", cpus = 8)

res.VarImpTuned <- mlr::resample(learner = lrn.svm.tune, task = mlr::spatial.task, 
                                 extract = extractVarImpFunction,
                                 resampling = outer, measures = list(auc), 
                                 models = TRUE, show.info = TRUE)

parallelMap::parallelStop() # stop parallelization

## get mean auroc decrease
var.imp <- do.call(rbind, lapply(res.VarImpTuned$extract, FUN = function(x){x[[1]]}))
var.imp <- data.frame(AUC_DECR = colMeans(var.imp), Variable = names(colMeans(var.imp))) 
4

0 回答 0