Feature Selection (Filter, Wrapper, Embedded)
Reduce noise with correlation filters, recursive elimination, and model-based importance.
In plain words
Feature selection keeps the helpful columns and drops the useless ones so the model is simpler and works better on new data, as long as you choose them without peeking at the test data.
Think of it like…
Packing for a trip: a quick filter removes obviously useless items (filter), trying outfits repeatedly to see what works together is thorough but slow (wrapper), and a suitcase with a weight limit forces you to drop things as you pack (embedded). Whatever you do, don't choose items after seeing the weather you'll be tested on.
Where you’ve seen it
A genomics-style dataset has n = 100 samples and p = 5,000 noise features with a random 50/50 label. Selecting the 50 features most correlated with the label using all 100 rows, then running 5-fold cross-validation on those 50 features, reports accuracy above 80% to 90% although the true accuracy is 50%: with 5,000 noise features, the strongest one is expected to show a correlation of about 3.72 / √99 ≈ 0.37 by chance alone (the 1-in-5,000 tail of a standard normal is 3.72 standard deviations, and the standard deviation of a sample correlation is about 1/√(n − 1)). Doing the selection inside each fold (a pipeline with SelectKBest then the model) correctly gives about 50%. On a real churn dataset with 200 features, a pipeline applies a variance threshold (drops 14 constant columns), a correlation filter (drops one of each pair with |r| > 0.9, removing 31), then recursive feature elimination with a logistic regression keeps 40 features: cross-validated AUC is 0.812 versus 0.805 with all 200, with 5 times fewer inputs and faster scoring. L1-regularised logistic regression selects a similar set (38 features, AUC 0.810).
Words you’ll hear
- Filter method
- Scores features independently of the model.
- Wrapper method
- Searches feature subsets by repeatedly training a model.
- Embedded method
- Selection performed during model training (L1, tree importance).
- Selection leakage
- Choosing features using labels from the data used to validate the model.
Visual diagram
Theory & deep dive
Filter, wrapper and embedded methods
Filters are fast and model-agnostic. Variance threshold removes constant or near-constant columns; a correlation filter drops one feature from each highly correlated pair (|r| > 0.9) to reduce redundancy; univariate scores rank features against the target: ANOVA F (numeric features, classification), chi-square (non-negative counts or categorical), mutual information (captures non-linear dependence) with SelectKBest or SelectPercentile. They ignore feature interactions (a feature useless alone but useful in combination is dropped) and redundancy (they may keep ten near-duplicates), so use them as a first screen.
Wrappers evaluate subsets with the actual model: RFE (recursive feature elimination) fits a model, removes the least important features, and repeats; RFECV chooses the number by cross-validation. Forward/backward selection (SequentialFeatureSelector) adds or removes one feature at a time based on cross-validated score. Wrappers account for interactions and the model's behaviour but cost many model fits (O(p²) for naive sequential search). Embedded methods get selection for free: L1/lasso shrinks coefficients to exactly zero, elastic net handles correlated groups, tree ensembles give importances (prefer permutation importance on held-out data to biased impurity importance), and SelectFromModel thresholds them. Regularisation-based selection can be unstable when features are correlated, so check which features are chosen across bootstrap samples (stability selection).
200 features → variance threshold drops 14 → correlation filter (|r| > 0.9) drops 31 → 155 remain → RFE with logistic regression keeps 40. Cross-validated AUC 0.812 versus 0.805 with all 200 features.
Avoiding selection leakage
Because selection uses the labels to rank features, doing it on the full dataset before cross-validation leaks information from the validation folds. The effect is dramatic with many features and few samples. With n = 100 and p = 5,000 noise features, each feature's sample correlation with a random label has a standard deviation of about 1/√99 ≈ 0.10; the maximum over 5,000 features is about 3.72 × 0.10 = 0.37. Selecting the top 50 by such correlations on all rows and then cross-validating a classifier on just those features makes noise look predictive (accuracies of 80 to 90% are common) because the chosen features were selected to correlate with the labels of the very samples used for validation.
The fix is to treat selection as part of the model: put it in a Pipeline (make_pipeline(StandardScaler(), SelectKBest(f_classif, k=50), LogisticRegression())) and cross-validate the whole pipeline, so each fold selects features from its own training rows only; for hyperparameter tuning of the number of features use nested cross-validation. Sanity checks: run the pipeline on shuffled labels (expect chance performance), compare train and validation gaps and examine the stability of selected features across folds. Remember also that selection does not imply importance or causality: a feature dropped for redundancy may be as informative as the one retained, and selecting by predictive power says nothing about effect. Document the criteria and recheck as data drifts.
n = 100, p = 5,000 noise features. SD of correlation ≈ 1/√99 ≈ 0.10. Two-sided tail 1/5,000 → z ≈ 3.72. Max |r| ≈ 0.37. A model built on the top features by |r| looks informative on those same rows although the label is random.
How it works, step by step
Step 1 of 41. Remove obvious junk
Drop constant, duplicated and ID-like columns and highly correlated pairs using training data.
- Keep
- domain-critical features
When to use it & trade-offs
What it is
Feature selection chooses a subset of features to keep, aiming for simpler, faster, more robust and more interpretable models. Three families exist. Filter methods score each feature independently of any model (variance threshold, correlation, mutual information, chi-square, ANOVA F-test) and keep the best. Wrapper methods search subsets by training a model repeatedly (recursive feature elimination, forward and backward selection) and are accurate but expensive. Embedded methods select as part of model training (L1/lasso coefficients, tree importances, boosting gain). The crucial rule is that selection is part of the model: it must happen inside cross-validation on training data only, otherwise it leaks the labels and inflates performance.
Why it matters: More features aren't free: they add noise, multicollinearity, training and serving cost and overfitting risk, especially with few rows (p ≫ n). Selection can improve generalisation and make models explainable, but done carelessly it produces one of the most common and most deceptive errors in data science: picking features using all the data and then cross-validating, which can report high accuracy on pure noise.
When to reach for it
- Wide datasets with many noisy features
- Interpretable and cheaper-to-serve models
- p much greater than n problems
When to avoid or be careful
- Selecting features on the whole dataset before cross-validation
- Trusting impurity importance for high-cardinality features
- Reading selected features as causal drivers
Selection families
Strengths: Cheap, model-agnostic, scalable.
Watch out: Ignores interactions and redundancy.
Strengths: Accounts for the model and interactions.
Watch out: Many model fits; overfitting risk.
Strengths: Efficient; often good trade-off.
Watch out: Can be unstable with correlated features.
Interview signals & numbers to know
When this topic comes up
- “5,000 features and 100 rows”
- SelectKBest and RFE
- L1 feature selection
- Selection bias
- Stability selection
Benchmarks & rules of thumb
- ≈ 0.37
- expected maximum chance correlation among 5,000 noise features with n = 100
- 200 → 40
- features kept in the staged example
- 0.812 vs 0.805
- AUC with 40 versus 200 features
Common mistakes & follow-ups
Common interview mistakes
- Running SelectKBest on all rows and then cross-validating.
- Keeping ten near-duplicate features because each scores well alone.
- Dropping a feature that matters only in combination with others.
Likely interviewer follow-ups
- What are filter, wrapper and embedded methods?
- How can feature selection leak information?
- How would you test for selection leakage?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Feature Selection (Filter, Wrapper, Embedded) with data science interview Q&A and hands-on practice.