Skip to contents

The AutoFSelector wraps a mlr3::Learner and augments it with an automatic feature selection. The auto_fselector() function creates an AutoFSelector object.

Usage

auto_fselector(
  method,
  learner,
  resampling,
  measure = NULL,
  term_evals = NULL,
  term_time = NULL,
  terminator = NULL,
  store_fselect_instance = TRUE,
  store_benchmark_result = TRUE,
  store_models = FALSE,
  check_values = FALSE,
  callbacks = list(),
  ...
)

Arguments

method

(character(1) | FSelector)
Key to retrieve fselector from mlr_fselectors dictionary or FSelector object.

learner

(mlr3::Learner)
Learner to optimize the feature subset for.

resampling

(mlr3::Resampling)
Resampling that is used to evaluated the performance of the feature subsets. Uninstantiated resamplings are instantiated during construction so that all feature subsets are evaluated on the same data splits. Already instantiated resamplings are kept unchanged.

measure

(mlr3::Measure)
Measure to optimize. If NULL, default measure is used.

term_evals

(integer(1))
Number of allowed evaluations.

term_time

(integer(1))
Maximum allowed time in seconds.

terminator

(Terminator)
Stop criterion of the feature selection.

store_fselect_instance

(logical(1))
If TRUE (default), stores the internally created FSelectInstanceSingleCrit with all intermediate results in slot $fselect_instance. Is set to TRUE, if store_models = TRUE

store_benchmark_result

(logical(1))
Store benchmark result in archive?

store_models

(logical(1)). Store models in benchmark result?

check_values

(logical(1))
Check the parameters before the evaluation and the results for validity?

callbacks

(list of CallbackFSelect)
List of callbacks.

...

(named list())
Named arguments to be set as parameters of the fselector.

Value

AutoFSelector.

Details

The AutoFSelector is a mlr3::Learner which wraps another mlr3::Learner and performs the following steps during $train():

  1. The wrapped (inner) learner is trained on the feature subsets via resampling. The feature selection can be specified by providing a FSelector, a bbotk::Terminator, a mlr3::Resampling and a mlr3::Measure.

  2. A final model is fit on the complete training data with the best found feature subset.

During $predict() the AutoFSelector just calls the predict method of the wrapped (inner) learner.

Resources

Nested Resampling

Nested resampling can be performed by passing an AutoFSelector object to mlr3::resample() or mlr3::benchmark(). To access the inner resampling results, set store_fselect_instance = TRUE and execute mlr3::resample() or mlr3::benchmark() with store_models = TRUE (see examples). The mlr3::Resampling passed to the AutoFSelector is meant to be the inner resampling, operating on the training set of an arbitrary outer resampling. For this reason it is not feasible to pass an instantiated mlr3::Resampling here.

Examples

# Automatic Feature Selection
# \donttest{

# split to train and external set
task = tsk("penguins")
split = partition(task, ratio = 0.8)

# create auto fselector
afs = auto_fselector(
  method = fs("random_search"),
  learner = lrn("classif.rpart"),
  resampling = rsmp ("holdout"),
  measure = msr("classif.ce"),
  term_evals = 4)

# optimize feature subset and fit final model
afs$train(task, row_ids = split$train)

# predict with final model
afs$predict(task, row_ids = split$test)
#> <PredictionClassif> for 69 observations:
#>     row_ids     truth  response
#>           5    Adelie    Adelie
#>           6    Adelie    Adelie
#>          14    Adelie    Adelie
#> ---                            
#>         339 Chinstrap Chinstrap
#>         342 Chinstrap Chinstrap
#>         344 Chinstrap Chinstrap

# show result
afs$fselect_result
#>    bill_depth bill_length body_mass flipper_length island  sex year
#> 1:       TRUE        TRUE      TRUE           TRUE   TRUE TRUE TRUE
#>                                                          features classif.ce
#> 1: bill_depth,bill_length,body_mass,flipper_length,island,sex,...          0

# model slot contains trained learner and fselect instance
afs$model
#> $learner
#> <LearnerClassifRpart:classif.rpart>: Classification Tree
#> * Model: rpart
#> * Parameters: xval=0
#> * Packages: mlr3, rpart
#> * Predict Types:  [response], prob
#> * Feature Types: logical, integer, numeric, factor, ordered
#> * Properties: importance, missings, multiclass, selected_features,
#>   twoclass, weights
#> 
#> $features
#> [1] "bill_depth"     "bill_length"    "body_mass"      "flipper_length"
#> [5] "island"         "sex"            "year"          
#> 
#> $fselect_instance
#> <FSelectInstanceSingleCrit>
#> * State:  Optimized
#> * Objective: <ObjectiveFSelect:classif.rpart_on_penguins>
#> * Terminator: <TerminatorEvals>
#> * Result:
#>    bill_depth bill_length body_mass flipper_length island  sex year classif.ce
#> 1:       TRUE        TRUE      TRUE           TRUE   TRUE TRUE TRUE          0
#> * Archive:
#>    bill_depth bill_length body_mass flipper_length island   sex  year
#> 1:       TRUE        TRUE      TRUE           TRUE   TRUE  TRUE  TRUE
#> 2:      FALSE        TRUE      TRUE           TRUE   TRUE  TRUE  TRUE
#> 3:       TRUE        TRUE      TRUE           TRUE   TRUE FALSE  TRUE
#> 4:      FALSE       FALSE     FALSE           TRUE   TRUE FALSE FALSE
#>    classif.ce
#> 1: 0.00000000
#> 2: 0.01086957
#> 3: 0.00000000
#> 4: 0.08695652
#> 

# shortcut trained learner
afs$learner
#> <LearnerClassifRpart:classif.rpart>: Classification Tree
#> * Model: rpart
#> * Parameters: xval=0
#> * Packages: mlr3, rpart
#> * Predict Types:  [response], prob
#> * Feature Types: logical, integer, numeric, factor, ordered
#> * Properties: importance, missings, multiclass, selected_features,
#>   twoclass, weights

# shortcut fselect instance
afs$fselect_instance
#> <FSelectInstanceSingleCrit>
#> * State:  Optimized
#> * Objective: <ObjectiveFSelect:classif.rpart_on_penguins>
#> * Terminator: <TerminatorEvals>
#> * Result:
#>    bill_depth bill_length body_mass flipper_length island  sex year classif.ce
#> 1:       TRUE        TRUE      TRUE           TRUE   TRUE TRUE TRUE          0
#> * Archive:
#>    bill_depth bill_length body_mass flipper_length island   sex  year
#> 1:       TRUE        TRUE      TRUE           TRUE   TRUE  TRUE  TRUE
#> 2:      FALSE        TRUE      TRUE           TRUE   TRUE  TRUE  TRUE
#> 3:       TRUE        TRUE      TRUE           TRUE   TRUE FALSE  TRUE
#> 4:      FALSE       FALSE     FALSE           TRUE   TRUE FALSE FALSE
#>    classif.ce
#> 1: 0.00000000
#> 2: 0.01086957
#> 3: 0.00000000
#> 4: 0.08695652


# Nested Resampling

afs = auto_fselector(
  method = fs("random_search"),
  learner = lrn("classif.rpart"),
  resampling = rsmp ("holdout"),
  measure = msr("classif.ce"),
  term_evals = 4)

resampling_outer = rsmp("cv", folds = 3)
rr = resample(task, afs, resampling_outer, store_models = TRUE)

# retrieve inner feature selection results.
extract_inner_fselect_results(rr)
#>    iteration bill_depth bill_length body_mass flipper_length island   sex year
#> 1:         1       TRUE        TRUE     FALSE           TRUE   TRUE FALSE TRUE
#> 2:         2      FALSE        TRUE      TRUE          FALSE   TRUE  TRUE TRUE
#> 3:         3      FALSE       FALSE     FALSE           TRUE  FALSE FALSE TRUE
#>    classif.ce                                          features  task_id
#> 1: 0.14473684 bill_depth,bill_length,flipper_length,island,year penguins
#> 2: 0.02631579             bill_length,body_mass,island,sex,year penguins
#> 3: 0.25974026                               flipper_length,year penguins
#>                 learner_id resampling_id
#> 1: classif.rpart.fselector            cv
#> 2: classif.rpart.fselector            cv
#> 3: classif.rpart.fselector            cv

# performance scores estimated on the outer resampling
rr$score()
#>                 task  task_id             learner              learner_id
#> 1: <TaskClassif[50]> penguins <AutoFSelector[46]> classif.rpart.fselector
#> 2: <TaskClassif[50]> penguins <AutoFSelector[46]> classif.rpart.fselector
#> 3: <TaskClassif[50]> penguins <AutoFSelector[46]> classif.rpart.fselector
#>            resampling resampling_id iteration              prediction
#> 1: <ResamplingCV[20]>            cv         1 <PredictionClassif[20]>
#> 2: <ResamplingCV[20]>            cv         2 <PredictionClassif[20]>
#> 3: <ResamplingCV[20]>            cv         3 <PredictionClassif[20]>
#>    classif.ce
#> 1: 0.06086957
#> 2: 0.05217391
#> 3: 0.20175439

# unbiased performance of the final model trained on the full data set
rr$aggregate()
#> classif.ce 
#>  0.1049326 
# }