基于重要性的变量缩减
我在过滤模型中最不重要的变量时遇到了困难。我收到了一组包含 4,000 多个变量的数据,我被要求减少进入模型的变量数量。
我确实尝试过两种方法,但我失败了两次。
我尝试的第一件事是在建模后手动检查变量重要性,并在此基础上删除不重要的变量。
# reproducible example
data <- iris
# artificial class imbalancing
data <- iris %>%
mutate(Species = as.factor(ifelse(Species == "virginica", "1", "0")))
使用 simple 时一切正常Learner:
# creating Task
task <- TaskClassif$new(id = "score", backend = data, target = "Species", positive = "1")
# creating Learner
lrn <- lrn("classif.xgboost")
# setting scoring as prediction type
lrn$predict_type = "prob"
lrn$train(task)
lrn$importance()
Petal.Width Petal.Length
0.90606304 0.09393696
问题是数据高度不平衡,所以我决定使用GraphLearnerwithPipeOp运算符来对多数组进行欠采样,然后将其传递给AutoTuner:
我确实跳过了我认为对这种情况不重要的代码的某些部分,例如搜索空间、终止符、调谐器等。
# undersampling
po_under <- po("classbalancing",
id = "undersample", adjust = "major",
reference = "major", shuffle = FALSE, ratio = 1 / 2)
# combine learner with pipeline graph
lrn_under <- GraphLearner$new(po_under %>>% lrn)
# setting the autoTuner
at <- AutoTuner$new(
learner = lrn_under,
resampling = resample,
measure = measure,
search_space = ps_under,
terminator = terminator,
tuner = tuner
)
at$train(task)
该问题的权利知道的是,尽管意义的财产是内仍visableat的$importance()中不可用。
> at
<AutoTuner:undersample.classif.xgboost.tuned>
* Model: list
* Parameters: list()
* Packages: -
* Predict Type: prob
* Feature types: logical, integer, numeric, character, factor, ordered, POSIXct
* Properties: featureless, importance, missings, multiclass, oob_error, selected_features, twoclass, weights
所以我决定改变我的方法并尝试将过滤添加到Learner. 这就是我更失败的地方。我首先查看了这个 mlr3book 博客 - https://mlr3book.mlr-org.com/fs.html。我试图importance = "impurity"像在博客中一样添加到 Learner 中,但 id 确实产生了错误。
> lrn <- lrn("classif.xgboost", importance = "impurity")
B??d w poleceniu 'instance[[nn]] <- dots[[i]]':
nie mo?na zmieni? warto?ci zablokowanego po??czenia dla 'importance'
这基本上意味着这样的事情:
Error in 'instance[[nn]] <- dots[[i]]': can't change value of blocked connection for 'importance'
我也尝试通过PipeOp过滤解决问题,但它也失败了。我相信没有importance = "impurity".
所以我的问题是,有没有办法实现我的目标?
此外,我将非常感谢解释为什么在建模之前可以按重要性进行过滤?不应该基于模型结果吗?
回答
您无法访问$importance该at变量的原因是它是 an AutoTuner,它不直接提供变量重要性,而仅“包裹”在实际Learner被调整的周围。
受过训练GraphLearner的保存在您的AutoTuner下$learner:
# get the trained GraphLearner, with tuned hyperparameters
graphlearner <- at$learner
这个对象也没有$importance()。(理论上,aGraphLearner可以包含多个Learner,然后它甚至不知道给予哪个重要性!)。
获取实际LearnerClassifXgboost对象有点乏味,不幸的是,因为mlr3 使用的“R6”对象系统存在缺陷:
- 获取未经训练的
Learner对象 - 获得训练有素的状态的
Learner,并把它变成对象
# get the untrained Learner
xgboostlearner <- graphlearner$graph$pipeops$classif.xgboost$learner
# put the trained model into the Learner
xgboostlearner$state <- graphlearner$model$classif.xgboost
现在可以查询重要性
xgboostlearner$importance()
您链接到的书中的示例在您的案例中不起作用,因为该书使用rangerLearner,而使用xgboost. importance = "impurity"特定于ranger.