我在过滤模型中最不重要的变量时遇到了困难。我收到了一组包含 4,000 多个变量的数据,我被要求减少进入模型的变量数量。
我已经尝试过两种方法,但都失败了两次。
我尝试的第一件事是在建模后手动检查变量重要性,并在此基础上删除不重要的变量。
# reproducible example
data <- iris
# artificial class imbalancing
data <- iris %>%
mutate(Species = as.factor(ifelse(Species == "virginica", "1", "0")))
使用简单的 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
问题是数据高度不平衡,所以我决定使用 GraphLearner
和 PipeOp
运算符对多数组进行欠采样,然后将其传递给 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)
问题是,尽管重要性属性在 at
中仍然可见,但 $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"
我将无法做到。
所以我的问题是,有没有办法实现我的目标?
此外,我将非常感谢解释为什么在建模之前可以按重要性进行过滤?不应该是基于模型结果吗?
答案 0 :(得分:7)
您无法访问 $importance
变量的 at
的原因是它是一个 AutoTuner
,它不直接提供变量重要性,只是“包裹”了实际的Learner
正在调整中。
经过训练的 GraphLearner
保存在您的 AutoTuner
中的 $learner
下:
# get the trained GraphLearner, with tuned hyperparameters
graphlearner <- at$learner
这个对象也没有$importance()
。 (理论上,一个 GraphLearner
可以包含多个 Learner
,然后它甚至不知道该赋予哪个重要性!)。
获取实际的 LearnerClassifXgboost
对象有点乏味,不幸的是,因为 shortcomings in the "R6" object system used by mlr3:
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()
您链接到的书中的示例不适用于您的案例,因为该书使用了 ranger
Learner,而使用的是 xgboost
。 importance = "impurity"
特定于 ranger
。