我需要将选定数量的预选功能添加到 mlr3 管道中。我希望我可以将这些功能添加到另一个过滤器的结果中,如 mlr3 图表的这个片段所示:
这可以通过自定义过滤器或其他方法实现吗?
我浏览了 mlr3 书,没有找到任何解决这个问题的内容。
#simple dummy example of what I'm trying to achieve
library(mlr3filters)
library(mlr3fselect)
task_pen = tsk("penguins")
#feature selection using information gain
po_flt = po("filter", filter = flt("information_gain"), filter.nfeat = 3)
graph = po_flt
result = graph$train(task_pen)
result$featureunion.output$feature_names
#the output:
#"bill_depth" "bill_length" "flipper_length"
#However, I want to be able to add a list of curated features to the filter
po_custom = #a custom filter that takes a list of features,
#in this case "island" and "sex"
po_custom$id <- "curated list"
graph = gunion(list(
po_flt,
po_custom
)) %>>%
po("featureunion")
result = graph$train(task_pen)
result$featureunion.output$feature_names
#the output (obviously not runable):
#"bill_depth" "bill_length" "flipper_length" "island" "sex"
我相信我已经弄清楚了。 mlr3 中的选择器功能可以实现此功能(以及更多功能):
po_flt = po("filter", filter = flt("information_gain"), filter.nfeat = 3)
pos = po("select", selector = selector_name(c("sex", "body_mass")))
graph = gunion(list(
po_flt,
pos
)) %>>%
po("featureunion")
result = graph$train(task_pen)
result$featureunion.output$feature_names
#"bill_depth" "bill_length" "flipper_length" "body_mass" "sex"