# Feature Selection with Recursive Feature Elimination with Cross Validation

Source:`R/FSelectorRFECV.R`

`mlr_fselectors_rfecv.Rd`

Feature selection using the Recursive Feature Elimination with Cross-Validation (RFE-CV) algorithm. See FSelectorRFE for a description of the base algorithm. RFE-CV runs a recursive feature elimination in each iteration of a cross-validation to determine the optimal number of features. Then a recursive feature elimination is run again on the complete dataset with the optimal number of features as the final feature set size. The performance of the optimal feature set is calculated on the complete data set and should not be reported as the performance of the final model. Only works with mlr3::Learners that can calculate importance scores (see the section on optional extractors in mlr3::Learner).

## Details

The resampling strategy is changed during the feature selection.
The resampling strategy passed to the instance (`resampling`

) is used to determine the optimal number of features.
Usually, a cross-validation strategy is used and a recursive feature elimination is run in each iteration of the cross-validation.
Internally, mlr3::ResamplingCustom is used to emulate this part of the algorithm.
In the final recursive feature elimination run the resampling strategy is changed to mlr3::ResamplingInsample i.e. the complete data set is used for training and testing.

The feature selection terminates itself when the optimal number of features is reached. It is not necessary to set a termination criterion.

## Archive

The ArchiveFSelect holds the following additional columns:

`"iteration"`

(`integer(1)`

)

The resampling iteration in which the feature subset was evaluated.`"importance"`

(`numeric()`

)

The importance score vector of the feature subset.

## Resources

The gallery features a collection of case studies and demos about optimization.

Utilize the built-in feature importance of models with Recursive Feature Elimination.

## Control Parameters

`n_features`

`integer(1)`

The number of features to select. By default half of the features are selected.`feature_fraction`

`double(1)`

Fraction of features to retain in each iteration. The default 0.5 retrains half of the features.`feature_number`

`integer(1)`

Number of features to remove in each iteration.`subset_sizes`

`integer()`

Vector of number of features to retain in each iteration. Must be sorted in decreasing order.`recursive`

`logical(1)`

If`TRUE`

(default), the feature importance is calculated in each iteration.

The parameter `feature_fraction`

, `feature_number`

and `subset_sizes`

are mutually exclusive.

## Super class

`mlr3fselect::FSelector`

-> `FSelectorRFECV`

## Examples

```
# Feature Selection
# \donttest{
# retrieve task and load learner
task = tsk("penguins")
learner = lrn("classif.rpart")
# run feature selection on the Palmer Penguins data set
instance = fselect(
fselector = fs("rfecv"),
task = task,
learner = learner,
resampling = rsmp("cv", folds = 3),
measure = msr("classif.ce"),
store_models = TRUE
)
# best performing feature subset
instance$result
#> bill_depth bill_length body_mass flipper_length island sex year
#> 1: TRUE TRUE FALSE TRUE FALSE FALSE FALSE
#> features classif.ce
#> 1: bill_depth,bill_length,flipper_length 0.0377907
# all evaluated feature subsets
as.data.table(instance$archive)
#> bill_depth bill_length body_mass flipper_length island sex year
#> 1: TRUE TRUE TRUE TRUE TRUE TRUE TRUE
#> 2: TRUE TRUE TRUE TRUE TRUE TRUE TRUE
#> 3: TRUE TRUE TRUE TRUE TRUE TRUE TRUE
#> 4: TRUE TRUE FALSE TRUE FALSE FALSE FALSE
#> 5: TRUE TRUE FALSE TRUE FALSE FALSE FALSE
#> 6: FALSE TRUE TRUE TRUE FALSE FALSE FALSE
#> 7: TRUE TRUE TRUE TRUE TRUE TRUE TRUE
#> 8: TRUE TRUE FALSE TRUE FALSE FALSE FALSE
#> classif.ce runtime_learners timestamp batch_nr warnings errors
#> 1: 0.03478261 0.005 2023-11-17 12:02:38 1 0 0
#> 2: 0.08695652 0.006 2023-11-17 12:02:38 1 0 0
#> 3: 0.08771930 0.005 2023-11-17 12:02:38 1 0 0
#> 4: 0.06956522 0.005 2023-11-17 12:02:38 2 0 0
#> 5: 0.08695652 0.005 2023-11-17 12:02:38 2 0 0
#> 6: 0.12280702 0.019 2023-11-17 12:02:38 2 0 0
#> 7: 0.03488372 0.006 2023-11-17 12:02:38 3 0 0
#> 8: 0.03779070 0.005 2023-11-17 12:02:39 4 0 0
#> importance iteration
#> 1: 85.37539,75.63432,63.96837,62.19122,57.78177, 0.00000,... 1
#> 2: 90.55369,90.47703,68.48238,62.56415,58.35917, 0.00000,... 2
#> 3: 94.166121,81.953864,80.389962,80.104217,76.983562, 2.812381,... 3
#> 4: 85.37539,75.63432,63.96837 1
#> 5: 90.55369,90.47703,68.48238 2
#> 6: 83.41044,80.41734,80.08980 3
#> 7: 124.20793,121.52400,102.74919, 87.26186, 78.61700, 0.00000,... NA
#> 8: 124.2079,121.5240,104.2507 NA
#> features
#> 1: bill_depth,bill_length,body_mass,flipper_length,island,sex,...
#> 2: bill_depth,bill_length,body_mass,flipper_length,island,sex,...
#> 3: bill_depth,bill_length,body_mass,flipper_length,island,sex,...
#> 4: bill_depth,bill_length,flipper_length
#> 5: bill_depth,bill_length,flipper_length
#> 6: bill_length,body_mass,flipper_length
#> 7: bill_depth,bill_length,body_mass,flipper_length,island,sex,...
#> 8: bill_depth,bill_length,flipper_length
#> resample_result
#> 1: <ResampleResult[21]>
#> 2: <ResampleResult[21]>
#> 3: <ResampleResult[21]>
#> 4: <ResampleResult[21]>
#> 5: <ResampleResult[21]>
#> 6: <ResampleResult[21]>
#> 7: <ResampleResult[21]>
#> 8: <ResampleResult[21]>
# subset the task and fit the final model
task$select(instance$result_feature_set)
learner$train(task)
# }
```