我正在尝试使用 information_gain 和 mrmr 特征过滤,但也是 information_gain 和 mrmr 特征过滤的组合(两者的结合)。我试过在下面创建一个代表。
library("mlr3verse")
task <- tsk('sonar')
filters = list("nop" = po("nop"),
"information_gain" = po("filter", flt("information_gain")),
"mrmr" = po("filter", flt("mrmr")),
"ig_mrmr" = po("branch", c("ig2", "mrmr2"), id = "ig_mrmr") %>>%
gunion(list("ig2" = po("filter", flt("information_gain")),
"mrmr2" = po("filter", flt("mrmr")))) %>>%
po("featureunion", id = "union_igmrmr"))
pipe =
po("branch", names(filters), id = "branch1") %>>%
gunion(unname(filters)) %>>%
po("unbranch", names(filters), id = "unbranch1") %>>%
po(lrn('classif.rpart'))
pipe$plot()
到目前为止看起来不错,在这里您可以看到我正在尝试结合 ig 和 mrmr 选择的功能。
接下来我设置参数,我认为是正确的:
ps <- ParamSet$new(list(
ParamDbl$new("classif.rpart.cp", lower = 0, upper = 0.05),
ParamInt$new("information_gain.filter.nfeat", lower = 20L, upper = 60L),
ParamFct$new("information_gain.type", levels = c("infogain", "symuncert")),
ParamInt$new("ig2.filter.nfeat", lower = 20L, upper = 60L),
ParamFct$new("ig2.type", levels = c("infogain", "symuncert")),
ParamInt$new("mrmr.filter.nfeat", lower = 20L, upper = 60L),
ParamInt$new("mrmr2.filter.nfeat", lower = 20L, upper = 60L),
ParamFct$new("branch1.selection", levels = names(filters)),
ParamFct$new("ig_mrmr.selection", levels = c("ig2", "mrmr2"))
))
依赖项是我苦苦挣扎的地方。我可以在外部分支或内部分支上设置“嵌套”参数,但我不确定如何在两者上触发它们。在下面的示例中,它们设置在外部分支上。
ps$add_dep("information_gain.filter.nfeat", "branch1.selection", CondEqual$new("information_gain"))
ps$add_dep("information_gain.type", "branch1.selection", CondEqual$new("information_gain"))
ps$add_dep("mrmr.filter.nfeat", "branch1.selection", CondEqual$new("mrmr"))
ps$add_dep("ig2.filter.nfeat", "branch1.selection", CondEqual$new("ig_mrmr"))
ps$add_dep("ig2.type", "branch1.selection", CondEqual$new("ig_mrmr"))
ps$add_dep("mrmr2.filter.nfeat", "branch1.selection", CondEqual$new("ig_mrmr"))
ps
glrn <- GraphLearner$new(pipe)
glrn$predict_type <- "prob"
cv5 <- rsmp("cv", folds = 5)
task$col_roles$stratum <- task$target_names
instance <- TuningInstanceSingleCrit$new(
task = task,
learner = glrn,
resampling = cv5,
measure = msr("classif.auc"),
search_space = ps,
terminator = trm("evals", n_evals = 5)
)
tuner <- tnr("random_search")
tuner$optimize(instance)
请注意,在我尝试优化调谐器之前,我不会遇到错误。
错误信息:
Error in self$assert(xs) :
Assertion on 'xs' failed: Parameter 'ig2.filter.nfeat' not available. Did you mean 'branch1.selection' / 'information_gain.filter.nfeat' / 'information_gain.filter.frac'?.