我在模型中遇到了筛选掉最不重要变量的困难。我收到了一个有超过4000个变量的数据集,并被要求减少进入模型的变量数量。
我已经尝试了两种方法,但都失败了。
我尝试的第一件事是在建模后手动检查变量重要性,并基于此删除不显著的变量。
问题在于数据高度不平衡,因此我决定使用
目前的问题是尽管重要属性仍然在
# 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。我尝试像博客中一样向Learner添加importance = "impurity"
,但它产生了一个错误。
> 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"
的话,我将无法做到。
所以我的问题是,有没有办法实现我想要的目标?
此外,如果可以在建模之前根据重要性进行过滤,为什么会出现这种情况呢?难道不应该基于模型结果吗?
library(caret);names(dataset)[nearZeroVar(dataset)]
),然后尝试删除高度相关的特征(例如,如果您有“年龄”(例如31岁)和“年龄段”(例如25-29岁,30-34岁),可能安全删除“年龄段”)。您可以以多种方式调查特征之间的相关性 - 我使用的一种方法是使用例如hclust和/或大型热图对所有样本的所有特征的分数进行聚类(我使用python进行此操作,因为R可能难以绘制> 100百万数据点),然后进行建模并检查特征重要性。 - jared_mamrot$importance()
函数不可用。 - Radbys