Feature selection

Remove useless, redundant or harmful features with variance, correlation and importance filters.

Lesson 19 of 6612 minIntermediate

You will learn to

  • Apply low-variance and correlation filters
  • Use model-based importance for selection
  • Select features inside cross-validation

Before this lesson

Intuition

Imagine packing a rucksack for a day hike. A larger rucksack lets you carry more, but each extra item is weight, and some items (a second heavy jacket, a duplicate map) add nothing. Features are like that. More columns can give a model more to work with, but useless columns have costs:

  • Noise. A column unrelated to the target still gives the model chances to find accidental patterns, which is overfitting.
  • Redundancy. Two columns that say the same thing make linear models unstable and split the credit between them.
  • Cost. More columns mean more training time, more memory and a more complicated model to explain and to monitor.

Feature selection is choosing which columns to keep. Three families of methods go from cheap to more informed:

  1. Filters that ignore the target. Drop columns that never change (low variance) and columns that duplicate another (high correlation). They are fast and safe.
  2. Filters that use the target. Score each column by its relation to the target, for example with the importance reported by a tree ensemble, and keep the best.
  3. Wrapper and embedded methods. Try subsets with a model, or let the model's own penalty set weights to zero (see regularization).

Picture it

All columns1. Low variance2. High correlation3. Low importanceKeptcountrycountryageageageageincomeincomeincomeincomeincome_kincome_kincome_kvisitsvisitsvisitsvisitsflagflagtenuretenuretenuretenurerow_noiserow_noiserow_noiserow_noise
Eight columns pass through three filters. Dashed boxes mark the stage where a column is dropped; the four that remain are kept for the model.

Each stage removes a different kind of problem, in order from the cheapest check to the most informed. The first two stages never look at the target, so they cannot leak it. The last stage does, which is why it must be treated with care (see the end of this lesson).

The math

Variance filter. The variance of a column is the average squared distance from its mean:

Var(x)=1n∑i=1n(xi−xˉ)2\text{Var}(x) = \frac{1}{n}\sum_{i=1}^{n}(x_i - \bar{x})^2

The filter drops a column when Var(x)≤τ\text{Var}(x) \le \tau, where τ\tau is the threshold. A threshold of 0 removes only constant columns. Variance depends on units: a column in metres has a variance a million times smaller than the same column in millimetres. So a non-zero threshold only makes sense on columns with comparable scales.

Correlation filter. The Pearson correlation of two columns,

rxz=∑i(xi−xˉ)(zi−zˉ)∑i(xi−xˉ)2 ∑i(zi−zˉ)2r_{xz} = \frac{\sum_i (x_i - \bar{x})(z_i - \bar{z})}{\sqrt{\sum_i (x_i - \bar{x})^2}\ \sqrt{\sum_i (z_i - \bar{z})^2}}

is between -1 and 1. The filter looks at the absolute value ∣r∣|r| for every pair and, when it is above the threshold (for example 0.95), drops one column of the pair. Sign does not matter: a perfect negative relation is as redundant as a positive one.

Importance filter. A tree ensemble can report how much each column helped it to split the data, as a number for each column that adds up to 1. A simple rule keeps the columns whose importance is at or above the median importance, which keeps about half of the columns.

Worked example

Variance. A column has the values 0.9, 1.0, 1.1, 1.0, 1.0. The mean is 1.0 and the variance is (0.01 + 0 + 0.01 + 0 + 0) / 5 = 0.004. With a threshold of 0 the column is kept, because its variance is above 0. A column with five equal values has variance exactly 0 and is dropped. Be careful with scale: the income column 30, 45, 60, 75, 90 (in thousands) has variance 450, but the same values written in millions (0.03, 0.045, ...) have variance 0.00045. A threshold such as 0.01 would wrongly drop the second version, though the information is identical.

Correlation. Take x=(1,2,3,4,5)x = (1,2,3,4,5) and z=(2,4,6,8,10.5)z = (2,4,6,8,10.5). The second column is almost exactly twice the first. The correlation is 0.9989, above 0.95, so the filter keeps one of the two. The order matters: the filter checks the columns in their given order and drops a column that is highly correlated with an earlier one, so here z is dropped and x is kept.

Importance. Eight columns get these importances (illustrative numbers): 0.30, 0.22, 0.15, 0.11, 0.09, 0.07, 0.04, 0.02. They add up to 1. The median of the eight values is (0.11 + 0.09) / 2 = 0.10. The columns at or above 0.10 are the first four, so four of the eight columns are kept. Together they hold 78% of the total importance (0.30 + 0.22 + 0.15 + 0.11 = 0.78). Whether dropping the other four hurts the score is an empirical question: compare cross-validated scores with and without them.

How mlflow does it

The Across several columns section of the Columns part has three feature-selection steps, all for numeric columns:

  • Low-Variance Removal drops columns whose variance is at or below its threshold (default 0, so only constant columns).
  • Correlation Removal drops one column of each pair whose absolute correlation is above its threshold (default 0.95).
  • Feature Selection keeps the columns whose importance, from an ExtraTrees model of 100 trees, is above the median. It has no parameters. The model inside it is a classification forest, so use it on classification pipelines and check the result against a run without it.

Put them in this order: constant columns first, duplicates second, importance last. The preprocessing reference lists all the steps.

Common mistakes

  • Selecting with the target on all the data, then cross-validating. The validation rows helped choose the columns, so the score is optimistic. The effect is large when there are many columns and few rows: you can "find" columns that predict random noise. Keep selection inside the pipeline so that it is refitted on the training part of every fold.
  • Using a variance threshold on unscaled columns. The result depends on the units. Use a threshold of 0 for constants, or scale the columns first.
  • Trusting importance from one run. Importances from a random forest-style model vary between runs and are biased towards columns with many distinct values. Treat them as a hint, not a verdict.
  • Dropping a column because it looks useless alone. Some columns help only in combination with others. Test removals with cross-validation instead of deciding by eye.
  • Using selection to fix overfitting caused by too little data. Removing columns helps a little, but more rows or a simpler model usually help more. See overfitting and bias-variance.

Try it in mlflow

Add a feature selection step

  1. Open Pipelines and pick a classification pipeline with many numeric columns.
  2. In the Columns part, open Across several columns, press Use on Remove columns that hardly change, then on Remove repeated information (its threshold defaults to 0.95).
  3. Add Feature Selection after them to keep only the columns with above-median importance.
  4. Train from Review & run (Train N models) and compare the score and the training time with a run that keeps all the columns.
Open in mlflow

Read in the docs

Check your understanding

  1. 1. A numeric column has the same value in every row. What should a low-variance filter do with it?
  2. 2. Two columns have a correlation of 0.99. What does a correlation filter with a threshold of 0.95 do?
  3. 3. Why must a selection that uses the target (such as importance from a model) be done inside each cross-validation fold?