How do you approach feature selection?
Written by the mlprep team · Updated
You have 500 features. How do you decide which ones to keep? Walk me through your feature selection process.
Why feature selection matters#
Fewer features: faster training, smaller models, lower latency at inference, less risk of overfitting, and easier to understand. More importantly, irrelevant features add noise to distance calculations and can confuse gradient-based models.
Filter methods (model-agnostic, fast)#
Rank features by their univariate relationship with the target, independent of the model.
Variance threshold: remove features with near-zero variance — they carry no information. Quick first pass.
Correlation: remove features highly correlated with each other (|r| > 0.9). Keep one representative from each correlated cluster.
Mutual information: measures the mutual dependence between feature and target. Unlike correlation, captures non-linear relationships:
MI(X; Y) = ∑ p(x,y) · log[p(x,y) / (p(x)·p(y))]
sklearn.feature_selection.mutual_info_classif for classification. Good for initial screening.
Statistical tests: chi-squared (categorical features, classification), ANOVA F-test (numeric features). Rank by p-value.
Filter methods are fast but evaluate features independently — they miss redundancy between features.
Wrapper methods (expensive, often best)#
Recursive Feature Elimination (RFE): train the model, rank features by importance, remove the worst k, retrain. Repeat until target number of features. Captures feature interactions but O(features × folds) training runs.
Forward/backward selection: add (or remove) features greedily based on validation performance. Rarely used at scale due to cost.
Embedded methods (built into training)#
L1 / Lasso regularization: drives less useful feature weights to exactly zero. Run lasso with cross-validated λ, take features with nonzero coefficients. Interpretable, fast, but assumes linear relationships.
Tree-based importance: train a random forest or gradient boosted tree, read off feature importances.
- Gini importance (default): sum of impurity decrease weighted by samples. Fast. Biased toward high-cardinality and correlated features — often misleading.
- Permutation importance: for each feature, randomly shuffle it, measure validation performance drop. Drop = importance. Unbiased, model-agnostic, but slow.
- SHAP values: additive attribution per feature per sample. Most reliable, captures direction and magnitude. Computationally heavier but worth it for final selection.
Collinearity handling#
Correlated features hurt coefficient interpretability and inflate variance of linear model estimates. Detect with VIF (Variance Inflation Factor): VIF > 10 signals problematic collinearity. Drop or combine correlated features.
Dimensionality reduction vs selection#
Selection keeps original features (interpretable). Reduction (PCA, UMAP, autoencoders) creates new compressed representations that may not correspond to original features.
Use PCA when: features are dense numeric, no interpretability requirement, preprocessing for neural nets or k-NN.
Use feature selection when: you need to identify which original features matter (regulatory requirements, debugging, serving cost).
Practical workflow#
- Remove zero/near-zero variance features
- Remove features with > 90% missing that you can't impute meaningfully
- Compute mutual information or correlation with target — discard bottom 20-30%
- Remove redundant correlated features (keep higher MI one from each pair)
- Train a tree model, compute permutation importance or SHAP — final ranking
- Use RFE or validation curve over feature subsets to pick cutoff
The key takeaway unlocks after you reveal an expert answer.
Key Takeaway
Feature selection methods: filter (variance threshold, MI, correlation — fast, model-agnostic, miss interactions), wrapper (RFE — best quality, expensive), embedded (L1/lasso, tree importance — built into training). For tree importance: prefer permutation importance or SHAP over Gini importance (biased toward high-cardinality features). Workflow: variance filter → correlation filter → MI ranking → tree-based final ranking.
Follow-up Questions to Test Yourself
Interviewers often pivot here once you cover the core concept.
- Why is Gini importance from random forests biased toward high-cardinality features, and why doesn't permutation importance have this bias?
- How would you use feature selection in a production pipeline while avoiding data leakage?
- When would you use PCA over feature selection, and vice versa?
How confident do you feel explaining this?
Track your understanding or flag it to revisit before your next loop.
Related Questions
Walk me through feature engineering for tabular data
How do you encode categorical features? Compare one-hot, target encoding, and embeddings.
How do you handle class imbalance in ML?
When do feature crosses matter, and how do you control their complexity?