How to add curated list of features to the results of a filter in an mlr3 pipeline

54 Views Asked by At

I need to add a select number of pre-selected features to an mlr3 pipeline. I'm hoping I can add those features the results of another filter, as in this snippet of an mlr3 graph:

mlr3 pipeline

Is this possible via a custom filter or other method?

I've looked through the mlr3 book and don't find anything addressing this question.

#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"
1

There are 1 best solutions below

1
Huazhong On

I believe I figured this out. The selector function in mlr3 allows for this (and more):

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"