# 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: TRUE TRUE FALSE 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.06086957 0.008 2023-03-02 12:42:38 1 0 0
#> 2: 0.09565217 0.008 2023-03-02 12:42:38 1 0 0
#> 3: 0.03508772 0.008 2023-03-02 12:42:38 1 0 0
#> 4: 0.06956522 0.008 2023-03-02 12:42:38 2 0 0
#> 5: 0.09565217 0.009 2023-03-02 12:42:38 2 0 0
#> 6: 0.03508772 0.007 2023-03-02 12:42:38 2 0 0
#> 7: 0.03488372 0.010 2023-03-02 12:42:38 3 0 0
#> 8: 0.03779070 0.009 2023-03-02 12:42:38 4 0 0
#> importance iteration
#> 1: 79.62073,79.02241,58.47072,56.67162,44.97748, 0.00000,... 1
#> 2: 92.75216,87.35120,65.51831,61.72015,48.42658, 0.00000,... 2
#> 3: 85.02369,79.84248,71.75478,59.51483,56.43610, 0.00000,... 3
#> 4: 79.62073,79.02241,58.47072 1
#> 5: 92.75216,87.35120,65.51831 2
#> 6: 85.02369,79.84248,71.75478 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_depth,bill_length,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)
# }
```