Often, data sets include a large number of features. The technique of extracting a subset of relevant features is called feature selection. Feature selection can enhance the interpretability of the model, speed up the learning process and improve the learner performance. There exist different approaches to identify the relevant features. mlr supports filter and wrapper methods.

Filter methods

Filter methods assign an importance value to each feature. Based on these values the features can be ranked and a feature subset can be selected.

Calculating the feature importance

Different methods for calculating the feature importance are built into mlr’s function generateFilterValuesData() (getFilterValues() has been deprecated in favor of generateFilterValuesData().). Currently, classification, regression and survival analysis tasks are supported. A table showing all available methods can be found in article filter methods.

Function generateFilterValuesData() requires the Task() and a character string specifying the filter method.

fv is a FilterValues() object and fv$data contains a data.frame that gives the importance values for all features. Optionally, a vector of filter methods can be passed.

A bar plot of importance values for the individual features can be obtained using function plotFilterValues().

By default plotFilterValues() will create facetted subplots if multiple filter methods are passed as input to generateFilterValuesData().

There is also an experimental ggvis plotting function, plotFilterValuesGGVIS(). This takes the same arguments as plotFilterValues() and produces a shiny application that allows the interactive selection of the displayed filter method, the number of features selected, and the sorting method (e.g., ascending or descending).

According to the "information.gain" measure, Petal.Width and Petal.Length contain the most information about the target variable Species.

Selecting a feature subset

With mlr’s function filterFeatures() you can create a new Task() by leaving out features of lower importance.

There are several ways to select a feature subset based on feature importance values:

  • Keep a certain absolute number (abs) of features with highest importance.
  • Keep a certain percentage (perc) of features with highest importance.
  • Keep all features whose importance exceeds a certain threshold value (threshold).

Function filterFeatures() supports these three methods as shown in the following example. Moreover, you can either specify the method for calculating the feature importance or you can use previously computed importance values via argument fval.

Fuse a learner with a filter method

Often feature selection based on a filter method is part of the data preprocessing and in a subsequent step a learning method is applied to the filtered data. In a proper experimental setup you might want to automate the selection of the features so that it can be part of the validation method of your choice. A Learner (makeLearner()) can be fused with a filter method by function makeFilterWrapper(). The resulting Learner (makeLearner()) has the additional class attribute FilterWrapper().

In the following example we calculate the 10-fold cross-validated error rate mmce of the k-nearest neighbor classifier (FNN::fnn()) with preceding feature selection on the iris (datasets::iris()) data set. We use "information.gain" as importance measure and select the 2 features with highest importance. In each resampling iteration feature selection is carried out on the corresponding training data set before fitting the learner.

lrn = makeFilterWrapper(learner = "classif.fnn", fw.method = "information.gain", fw.abs = 2)
rdesc = makeResampleDesc("CV", iters = 10)
r = resample(learner = lrn, task = iris.task, resampling = rdesc, show.info = FALSE, models = TRUE)
r$aggr
## mmce.test.mean 
##     0.03333333

You may want to know which features have been used. Luckily, we have called resample() with the argument models = TRUE, which means that r$models contains a list of models (makeWrappedModel()) fitted in the individual resampling iterations. In order to access the selected feature subsets we can call getFilteredFeatures() on each model.

The selection of features seems to be very stable. The features Sepal.Length and Sepal.Width did not make it into a single fold.

Tuning the size of the feature subset

In the above examples the number/percentage of features to select or the threshold value have been arbitrarily chosen. If filtering is a preprocessing step before applying a learning method optimal values with regard to the learner performance can be found by tuning.

In the following regression example we consider the BostonHousing (mlbench::BostonHousing()) data set. We use a linear regression model and determine the optimal percentage value for feature selection such that the 3-fold cross-validated mean squared error (mse()) of the learner is minimal. As search strategy for tuning a grid search is used.

The performance of all percentage values visited during tuning is:

The optimal percentage and the corresponding performance can be accessed as follows:

After tuning we can generate a new wrapped learner with the optimal percentage value for further use.

Here is another example using multi-criteria tuning. We consider linear discriminant analysis (MASS::lda()) with precedent feature selection based on the Chi-squared statistic of independence ("chi.squared") on the Sonar (mlbench::sonar()) data set and tune the threshold value. During tuning both, the false positive and the false negative rate fpr and fnr), are minimized. As search strategy we choose a random search (see makeTuneMultiCritControlRandom (?TuneMultiCritControl()).

The results can be visualized with function plotTuneMultiCritResult(). The plot shows the false positive and false negative rates for all parameter values visited during tuning. The size of the points on the Pareto front is slightly increased.

Wrapper methods

Wrapper methods use the performance of a learning algorithm to assess the usefulness of a feature set. In order to select a feature subset a learner is trained repeatedly on different feature subsets and the subset which leads to the best learner performance is chosen.

In order to use the wrapper approach we have to decide:

  • How to assess the performance: This involves choosing a performance measure that serves as feature selection criterion and a resampling strategy.
  • Which learning method to use.
  • How to search the space of possible feature subsets.

The search strategy is defined by functions following the naming convention makeFeatSelControl<search_strategy. The following search strategies are available:

  • Exhaustive search makeFeatSelControlExhaustive (?FeatSelControl()),
  • Genetic algorithm makeFeatSelControlGA (?FeatSelControl()),
  • Random search makeFeatSelControlRandom (?FeatSelControl()),
  • Deterministic forward or backward search makeFeatSelControlSequential (?FeatSelControl()).

Select a feature subset

Feature selection can be conducted with function selectFeatures().

In the following example we perform an exhaustive search on the Wisconsin Prognostic Breast Cancer (TH.data::wpbc()) data set. As learning method we use the Cox proportional hazards model (survival::coxph()). The performance is assessed by the holdout estimate of the concordance index cindex).

ctrl is aFeatSelControl() object that contains information about the search strategy and potential parameter values.

sfeatsis a FeatSelResult (selectFeatures()) object. The selected features and the corresponding performance can be accessed as follows:

In a second example we fit a simple linear regression model to the BostonHousing (mlbench::BostonHousing()) data set and use a sequential search to find a feature set that minimizes the mean squared error mse). method = "sfs" indicates that we want to conduct a sequential forward search where features are added to the model until the performance cannot be improved anymore. See the documentation page makeFeatSelControlSequential (?FeatSelControl()) for other available sequential search methods. The search is stopped if the improvement is smaller than alpha = 0.02.

Further information about the sequential feature selection process can be obtained by function analyzeFeatSelResult().

Fuse a learner with feature selection

A Learner (makeLearner()) can be fused with a feature selection strategy (i.e., a search strategy, a performance measure and a resampling strategy) by function makeFeatSelWrapper(). During training features are selected according to the specified selection scheme. Then, the learner is trained on the selected feature subset.

The result of the feature selection can be extracted by function getFeatSelResult().

The selected features are:

The 5-fold cross-validated performance of the learner specified above can be computed as follows:

out.rdesc = makeResampleDesc("CV", iters = 5)

r = resample(learner = lrn, task = wpbc.task, resampling = out.rdesc, models = TRUE,
  show.info = FALSE)
r$aggr
## cindex.test.mean 
##        0.5553731

The selected feature sets in the individual resampling iterations can be extracted as follows:

lapply(r$models, getFeatSelResult)
## [[1]]
## FeatSel result:
## Features (13): mean_radius, mean_texture, mean_smoothness, mean_compactness, mean_concavity, mean_symmetry, SE_perimeter, SE_smoothness, SE_compactness, worst_texture, worst_area, worst_fractaldim, pnodes
## cindex.test.mean=0.6474580
## 
## [[2]]
## FeatSel result:
## Features (20): mean_radius, mean_perimeter, mean_area, mean_smoothness, mean_compactness, mean_concavity, mean_concavepoints, mean_fractaldim, SE_texture, SE_perimeter, SE_area, SE_fractaldim, worst_radius, worst_area, worst_compactness, worst_concavity, worst_concavepoints, worst_symmetry, tsize, pnodes
## cindex.test.mean=0.6354839
## 
## [[3]]
## FeatSel result:
## Features (18): mean_radius, mean_area, mean_smoothness, mean_compactness, mean_concavity, mean_concavepoints, mean_symmetry, mean_fractaldim, SE_radius, SE_texture, worst_radius, worst_texture, worst_perimeter, worst_compactness, worst_concavity, worst_fractaldim, tsize, pnodes
## cindex.test.mean=0.6956156
## 
## [[4]]
## FeatSel result:
## Features (14): mean_radius, mean_texture, mean_area, mean_smoothness, mean_concavity, mean_fractaldim, SE_concavepoints, SE_fractaldim, worst_radius, worst_perimeter, worst_area, worst_concavepoints, worst_symmetry, tsize
## cindex.test.mean=0.6654779
## 
## [[5]]
## FeatSel result:
## Features (10): mean_radius, mean_texture, mean_area, mean_fractaldim, SE_compactness, SE_symmetry, worst_area, worst_symmetry, worst_fractaldim, pnodes
## cindex.test.mean=0.6898454