Contents/ Part XVIII · Supervised Learning/ Chapter 113

Feature Selection

More columns is not more information. Irrelevant and redundant features add noise, slow training, and invite overfitting. This chapter covers the three ways to keep only the inputs that matter, filter, wrapper, and embedded methods, shows that a lean model can match the full one, and ends with the leakage trap that quietly inflates every careless result.

⏱️ ~18 min read
🐍 Notebook included
📊 Chapter 113

Given a dataset with dozens of columns, a beginner throws them all at the model. But many features are noise or duplicates, and they actively hurt: they dilute the signal, inflate variance, cost time and money to collect, and make the model harder to trust. Feature selection is the discipline of keeping only what earns its place.

Feature selection picks the most useful subset of input features. Three families: filter (score each feature against the target, model-free), wrapper (search subsets by training a model), and embedded (selection happens during training, lasso and tree importances).
✂️
The chapter in one line

Fewer, better features usually match or beat all of them: use a filter for a fast first screen, a wrapper or embedded method to account for redundancy, and always run the selection inside cross-validation, never on the whole dataset.

1

Why Select Features?

Every extra feature is a chance for the model to find a pattern that is not really there, the curse of dimensionality. Irrelevant features add variance, redundant ones destabilize coefficients (the multicollinearity of Multiple Linear Regression), and both slow training and complicate deployment. A smaller model is faster, cheaper, easier to explain, and often more accurate.

The notebook's marketing dataset makes the point starkly. It has 16 candidate features, but only 5 are truly informative (3 are redundant copies and 8 are pure noise). A model on all 16 scores a cross-validated 0.774; a model on just the 5 informative features scores 0.769, statistically the same. The eleven extra columns contributed nothing, so dropping them costs no accuracy and buys simplicity.

2

The Three Families

Every selection method is a filter, a wrapper, or an embedded method, distinguished by how much the model is involved.

Three ways to choose features FILTER score each feature vs target keep ↑ fast · model-free blind to redundancy F-score, mutual info WRAPPER search subsets with a model ●○●○fit0.71 ●●○○fit0.75 ●●●○fit0.77 ★ accurate · handles redundancy slow (many refits) RFE, RFECV EMBEDDED selection during training model fits & selects at once w₁w₂0w₃00 lasso drives weak weights to 0 best accuracy per compute the everyday default L1 / lasso, tree importances

In the notebook, the filter (ANOVA F-score) instantly ranks the informative features high and the noise near zero, but it also ranks the redundant copies high because it judges each feature alone. The wrapper (RFECV) trains a model on growing subsets and lands on a compact set. The embedded approach, an L1 (lasso) logistic model and a random forest's importances, selects as part of the fit, driving noise weights to zero and ranking the rest.

3

The Payoff, and the Leakage Trap

Because only the first few features carry signal, accuracy rises steeply then flattens as you add more, adding the noise columns brings no gain at all. The wrapper's cross-validated curve makes this visible and tells you where to stop.

Accuracy climbs with the first few features, then flattens number of features kept → CV accuracy 0.780.650.50 informative features redundant + noise (no gain) enough the last 11 features add nothing; a lean model matches the full one
⚠️
Selection must live inside cross-validation

Feature selection is a decision made from the data, so it belongs inside the pipeline, refit within each fold, exactly like the preprocessing in The Machine Learning Workflow. In the notebook we hand the model 300 columns of pure noise: picking the five that correlate best with the target on the whole dataset and then cross-validating gives a falsely predictive 0.56, while running the same selection inside a Pipeline correctly returns 0.48, the coin-flip baseline. Selecting features before the split is one of the most common ways analysts fool themselves.

4

Real-World Example: Trimming a Marketing Model

A marketing team tracks 16 signals per customer and wants to predict who will respond to a campaign, but suspects most of the columns are dead weight. The three families agree on the verdict.

📂 Dataset · feature-selection--campaign.xlsx

One row per customer with informative signals (recency_days, frequency, monetary, email_open_rate, web_visits_30d), three redundant copies, eight noise columns, and the label responded.

ApproachResultCV accuracy
All 16 featuresthe naive baseline0.774
Filter (F-score)ranks the 5 informative + redundant high, noise near 0screen
Wrapper (RFECV)auto-selects 8 features~0.77
Embedded (lasso / RF)zeros noise weights; ranks importances~0.77
5 informative onlythe lean model0.769
8 noise columns onlyno signal at all~0.49 (baseline)

The conclusion writes itself: the 5 informative features match the full 16, the noise columns are worthless, and the filter's blind spot (ranking the redundant copies high) is exactly what the wrapper and embedded methods correct. The team can ship a model on a third of the columns with no loss of accuracy, and stop paying to collect the rest.

5

Feature Selection in Machine Learning & AI

Selection is a permanent part of the workflow, from small tabular models to the largest systems.

Idea (this chapter)In ML / AI it becomes
Drop irrelevant featuresless overfitting, faster training, cheaper data collection
Drop redundant featuresstabler coefficients, clearer interpretability (SHAP, importances)
Embedded selection (L1)built into regularized models and used for high-dimensional data
Selection inside CVa core defense against the data-leakage that breaks reproducibility
Curse of dimensionalitywhy dimensionality reduction (PCA) and embeddings exist
🤖
Why this matters for AI research

Selection scales in two directions. On classic tabular problems it is the difference between a lean, explainable model and a bloated, leaky one, and doing it inside cross-validation is one of the most common fixes for results that fail to reproduce. At the frontier, the idea transforms: instead of hand-picking columns, deep networks learn their own features (representations) from raw data, and dimensionality reduction and embeddings (see Dimensionality Reduction and the vector search of Distance Metrics) compress thousands of dimensions into a few informative ones. The instinct is the same throughout, keep what carries signal, discard what does not.

🐍

Select features in Python

The companion notebook scores features with a filter (F-score, mutual information), searches subsets with a wrapper (RFECV and its accuracy curve), selects with embedded methods (L1 lasso and random-forest importances), proves a lean model matches the full one, and demonstrates the selection-leakage trap live, every cell explained.

📓 View Notebook (code & outputs) ▶ Open in Colab ⬇ View / Download on GitHub

View opens the rendered notebook instantly. Open in Colab runs it live. To run locally, install numpy, pandas, scikit-learn, matplotlib, seaborn, and openpyxl.

🎓 Key Takeaways

  • More features is not more information: irrelevant and redundant columns add variance, cost, and overfitting.
  • Filter methods score each feature vs the target (fast, model-free) but are blind to redundancy.
  • Wrapper methods (RFE / RFECV) search subsets with a model, accurate but slow; embedded methods (lasso, tree importances) select during training.
  • A lean model can match the full one: 5 informative features scored the same as all 16; the rest added nothing.
  • Select inside cross-validation: choosing features on the whole dataset leaks the answer and fakes accuracy (0.56 vs a true 0.48 on pure noise).
6

Practice Challenges

Five short challenges on the campaign table. Try them with scikit-learn before checking the solutions.

1

Filter by F-score

Rank the features by ANOVA F-score; name the top and confirm the noise columns score low.

Hint: f_classif(X, y) then sort.
2

Wrapper: how many features?

Use RFECV to choose the number of features automatically.

Hint: RFECV(estimator, cv=5).n_features_.
3

Embedded selection

Fit an L1 logistic model and a random forest; report what L1 zeros and the forest's top feature.

Hint: penalty="l1", solver="liblinear"; feature_importances_.
4

Fewer features, same accuracy

Compare all features to the 5 informative ones by CV accuracy.

Hint: cross_val_score on X[cols].
5

Selection leakage

Show that selecting features on the whole dataset inflates accuracy versus selecting inside a pipeline.

Hint: put SelectKBest inside a Pipeline for the honest score.
Check your work

A fully-worked solutions notebook walks through all five challenges, each verified in code. Try them yourself first, then compare.

📓 View Solutions ▶ Open Solutions in Colab ⬇ View / Download on GitHub
7

Quiz: Test Yourself

Eight quick questions on feature selection. Answer them, hit Check Answers, and keep refining until you score 100%. Your progress is saved, so you can hop back to the chapter and return anytime.

➡️
Up next

Selection keeps the right features; the next question is how a model actually learns from them. Optimization & Gradient Descent opens the engine room, loss functions, gradient descent, SGD and Adam, and the learning-rate choices that power every model from logistic regression to deep nets.