我在过滤模型中最不重要的变量时遇到了困难。我收到了一组包含 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"
我就做不到.所以我的问题是,有没有办法实现我的目标?
此外,我将非常感谢解释为什么在建模之前可以按重要性进行过滤?不应该基于模型结果吗?
最佳答案
无法访问的原因$importance
的at
变量是它是一个 AutoTuner
,它不直接提供变量重要性,仅“包裹”实际 Learner
正在调整。
受过训练的 GraphLearner
保存在您的 AutoTuner
中下 $learner
:
# get the trained GraphLearner, with tuned hyperparameters
graphlearner <- at$learner
这个对象也没有$importance()
. (理论上,一个 GraphLearner
可以包含多个 Learner
,然后它甚至不知道该赋予哪个重要性!)。获取实际
LearnerClassifXgboost
object 有点乏味,不幸的是,因为 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
学习者,虽然正在使用 xgboost
. importance = "impurity"
特定于 ranger
.
关于r - 基于重要性的变量缩减,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66267945/