Foundations to Frontier
Tests
D. Classical Machine Learning › D16 Feature Selection Algorithms

Filter Methods: Variance Threshold, Chi-square, ANOVA F-test, Mutual Information

Filter methods rank features one at a time by a statistic of their relationship with the target, before and independently of any model. A variance threshold removes features that barely change; the ANOVA F-test compares class means relative to within-class spread; the chi-square test measures dependence between a categorical (or binned) feature and the class; mutual information measures any kind of dependence, in nats or bits. Filters are fast, scale to millions of features and resist overfitting better than search-based methods, but they judge each feature alone: they keep redundant copies, miss features that only matter in combination, and each statistic is blind to some kinds of dependence. This page covers each statistic, its assumptions and what it detects, estimation of mutual information, multiple testing and false discovery rate control, redundancy-aware variants (mRMR, ReliefF), and doing selection inside cross-validation. In the exercises you implement all four filters on data with known structure, then measure how well each recovers the useful features and what that does to a classifier as the sample size grows.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Screen the candidates one by one

Before a long interview process, a recruiter screens CVs one at a time against a checklist. It is fast and catches obvious mismatches, but it cannot notice that two candidates have identical skills, or that two together would make a great team. Filter methods screen features the same way: each feature gets a score for how strongly it relates to the target, and the top scorers are kept.

Filter methods: what one feature at a time can and cannot see
mean shift (strong)
F 177.2 · χ² 133.5 · MI 0.264
mean shift (weak)
F 44.7 · χ² 43.3 · MI 0.077
copy of the first
F 158.4 · χ² 118.3 · MI 0.223
spread changes
F 0.5 · χ² 122.5 · MI 0.252
XOR feature a
F 2.7 · χ² 12.3 · MI 0.021
XOR feature b
F 2.1 · χ² 5.4 · MI 0.009
pure noise
F 0.1 · χ² 8.9 · MI 0.015
XOR pair together: a (across) vs b (up)
ANOVA F compares class means, so it misses the feature whose spread changes; χ² and MI on binned values catch it. The copy scores as high as the original, because filters judge each feature alone and cannot see that it adds nothing new. The XOR features score like noise one at a time, yet the scatter shows they separate the classes perfectly together. With small n, noise features can outscore the weak signal.
Why engineers care

Spacecraft and plant telemetry can have thousands of channels, and gene or spectral data tens of thousands. Filters cheaply shrink these to a manageable set before modelling. They also produce interpretable statements, such as “channel 412 differs between nominal and faulty runs”, which engineers can check physically.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A filter method scores features…
  2. 2. Filters keep redundant copies because…
  3. 3. Two features that are useful only together are…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

Four statistics by hand

Variance threshold. Remove features whose variance is below t, including constants. With columns (1, 1, 1, 1), (5, 6, 7, 8) and (0, 0.1, 0, 0.1), the variances are 0, 1.25 and 0.0025. With t = 0.01 only the second column survives. Variance ignores the target, and it depends on the feature’s scale.

ANOVA F-test. F = [between-class sum of squares / (k − 1)] / [within-class sum of squares / (n − k)]. For values 1, 2 (class 0) and 5, 6 (class 1):

  • the overall mean is 3.5;
  • the between-class sum of squares is 2(1.5 − 3.5)² + 2(5.5 − 3.5)² = 16;
  • the within-class sum of squares is 4 × 0.25 = 1.

So F = (16/1)/(1/2) = 32. F detects differences in class means.

Chi-square test. For a contingency table of feature value (or bin) by class, χ² = Σ (O − E)²/E, with E = row total × column total / grand total. For the table [[30, 10], [10, 30]], all E = 20, so χ² = 4 × 100/20 = 20. A continuous feature must be binned first.

Mutual information. I(X; Y) = Σ p(x, y) log[p(x, y)/(p(x)p(y))]. It is zero if X and Y are independent and ln 2 ≈ 0.693 nats if X determines a balanced binary Y. It detects any dependence, given enough data.

📝 Quick check (Level 1)
0/3 answered
  1. 1. Variance threshold uses…
  2. 2. ANOVA F detects differences in…
  3. 3. Mutual information of independent variables is…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 30 minUndergraduate students and practising engineers.

What each filter can see

Exercise 1 (n = 1000, 20 features with known roles). Scores:

feature kind variance ANOVA F χ² (10 bins) MI (nats) rank by F / χ² / MI
0 mean shift 0.8 1.715 792.7 470.3 0.282 1 / 1 / 1
1 mean shift 0.6 1.303 362.3 272.7 0.154 3 / 4 / 4
2 mean shift 0.4 1.173 146.0 138.2 0.073 4 / 5 / 5
3 noisy copy of 0 1.771 724.6 448.9 0.267 2 / 2 / 2
4 spread changes, not mean 2.576 0.2 399.2 0.243 12 / 3 / 3
5 XOR a 0.880 4.1 8.2 0.004 5 / 11 / 11
6 XOR b 1.030 0.1 3.9 0.002 17 / 19 / 19
7 almost constant 0.000 1.8 6.4 0.003 7 / 15 / 15

The 12 noise features scored up to F = 3.0, χ² = 18.4 and MI = 0.009.

  • F missed the spread feature. Its class means are equal, so F was 0.2. χ² and MI on binned values ranked it third.
  • All filters ranked the copy second. It adds almost nothing once feature 0 is in.
  • No univariate filter saw the XOR pair, although together the two features determine the class exactly.
  • The almost-constant feature was caught only by the variance threshold. F, χ² and MI do not depend on scale. Feature 7 even ranked 7th by F by chance.
from sklearn.feature_selection import VarianceThreshold, SelectKBest, f_classif, chi2, mutual_info_classif
SelectKBest(mutual_info_classif, k=5).fit_transform(X, y)   # chi2 needs non-negative features (e.g. counts)
🐍 Exercise 1: four filters
Implement variance, ANOVA F, chi-square on quantile bins and plug-in mutual information; score 20 features of known kinds and see what each filter detects.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Which filter missed the spread feature in exercise 1?
  2. 2. Which filters found the XOR pair?
  3. 3. The almost-constant feature was flagged only by…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory and good practice

Distributions. Under the null hypothesis of no dependence:

  • F follows an F distribution with (k − 1, n − k) degrees of freedom, assuming normal classes with equal variances;
  • χ² follows a chi-square distribution with (r − 1)(c − 1) degrees of freedom, assuming expected counts of at least about 5;
  • 2n × (plug-in MI) is asymptotically χ² with (r − 1)(c − 1) degrees of freedom, so the plug-in MI is biased upward by about (r − 1)(c − 1)/(2n) nats.

Estimating MI for continuous features. Binning is simple but depends on the number of bins. The k-nearest-neighbour estimator (Kraskov, Stögbauer and Grassberger, 2004) is used by scikit-learn’s mutual_info_classif.

Multiple testing. With 10 000 features at p < 0.05, about 500 noise features pass by chance. Control the false discovery rate with Benjamini–Hochberg, or keep a fixed top k and validate.

Redundancy-aware filters.

  • mRMR (Peng, Long and Ding, 2005) adds features with high relevance I(X; Y) minus their mean redundancy with features already chosen.
  • ReliefF (Kononenko, 1994) scores features by how well they separate each point from its nearest neighbours of the other class, so it can detect some interactions.

The selection-bias trap. If you choose features on the full dataset and then cross-validate the classifier, the estimate is optimistic, sometimes wildly so with many features (Ambroise and McLachlan, 2002). Do the selection inside each training fold, for example in a Pipeline.

Interview questions.

  • When are filters the right choice? When there are very many features and few samples, as a fast first stage, and when interpretable per-feature statements are needed.
  • Why can F and MI disagree? F sees only mean differences. MI sees any dependence but is noisier and depends on estimation choices.
📝 Quick check (Level 3)
0/3 answered
  1. 1. Plug-in MI is biased…
  2. 2. mRMR subtracts…
  3. 3. Feature selection must be done…

Level 4: Expert

★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.

Does filtering help a classifier?

History.

  • K. Pearson (1900) introduced the chi-square test, and R. A. Fisher (1925) the analysis of variance.
  • C. Shannon (1948) defined mutual information.
  • R. Battiti (1994) used mutual information for feature selection.
  • K. Kira and L. Rendell (1992) introduced Relief.
  • I. Guyon and A. Elisseeff (2003) wrote the standard introduction to variable and feature selection.

Exercise 2. Top 5 features by each filter, then Gaussian naive Bayes. 10 datasets per size. The useful features are 0, 1, 2 and 4; feature 3 is the redundant copy.

n filter useful of 4 in top 5 copy picked noise in top 5 accuracy (top 5) accuracy (all 20) accuracy (the 4 useful)
50 ANOVA F 2.9 100% 1.0 0.827 0.864 0.894
50 χ² 3.1 100% 0.8 0.874 0.864 0.894
50 MI 3.1 100% 0.8 0.873 0.864 0.894
200 ANOVA F 3.0 100% 1.0 0.845 0.891 0.903
200 χ² 4.0 100% 0.0 0.897 0.891 0.903
200 MI 4.0 100% 0.0 0.897 0.891 0.903
2000 ANOVA F 3.1 100% 0.7 0.851 0.898 0.911
2000 χ² 4.0 100% 0.0 0.899 0.898 0.911
2000 MI 4.0 100% 0.0 0.899 0.898 0.911

(The full output also covers n = 100 and 500.)

  • The wrong statistic hurts at every sample size. F never found the spread feature, so its top 5 lost about 5 points of accuracy against using all features. More data did not help, because F was not looking for that kind of signal.
  • The binned filters were not noisier at small n than F here, contrary to the expectation set by MI’s bias. At n = 50 they found slightly more useful features than F did.
  • Redundancy wasted a slot every time. The copy was always picked, and the top-5 sets stayed about 1 point behind the 4 useful features alone. Naive Bayes counts the duplicated evidence twice.
  • Filtering gave little gain over all 20 features with this model (at most about 1 point at n = 50–100). Its value is in cost, interpretability and very wide data, not automatic accuracy.
🐍 Exercise 2 (research): filters and sample size
For n = 50–2000, select the top 5 features by ANOVA F, chi-square and MI; count useful, redundant and noise features picked; and compare Gaussian naive Bayes accuracy with the top 5, all features and the true useful set.

Filter methods in AI, robotics and aerospace

  • Telemetry triage: rank thousands of channels by MI with an anomaly label to focus engineers’ attention.
  • Spectral band selection: pick informative wavelengths for multispectral sensors, where fewer bands means cheaper hardware.
  • Text and log mining: χ² ranking of words or log tokens associated with failure reports.

References

  1. I. Guyon, A. Elisseeff (2003). “An introduction to variable and feature selection.” JMLR 3.
  2. R. Battiti (1994). “Using mutual information for selecting features in supervised neural net learning.” IEEE Trans. Neural Networks 5(4).
  3. H. Peng, F. Long, C. Ding (2005). “Feature selection based on mutual information: criteria of max-dependency, max-relevance, and min-redundancy.” IEEE TPAMI 27(8).
  4. A. Kraskov, H. Stögbauer, P. Grassberger (2004). “Estimating mutual information.” Physical Review E 69.
  5. C. Ambroise, G. J. McLachlan (2002). “Selection bias in gene extraction on the basis of microarray gene-expression data.” PNAS 99(10).