Elastic Net for Feature Selection
Use elastic net for feature selection in Applied AI engineering. Learn how to apply elastic net in a hands-on exercise, compare options, and tackle edge cases. Step-by-step tutorial for developers.
Focus: use elastic net for feature selection
You've built a model that predicts well on your validation set, but when you dig into the weights, dozens of features have tiny, noisy coefficients that make your model fragile and slow to train. You know feature selection is the answer, but you keep hitting the same wall: Lasso drops too much, Ridge drops nothing, and you're stuck fiddling with thresholds that never generalize. Here's the fix: elastic net, the regularized linear model that blends Lasso and Ridge to give you a robust, interpretable feature selector — and in this lesson you'll learn exactly how to use it.
The Problem This Lesson Solves
Real-world datasets are messy. You often have more features than you need, many of which are correlated, redundant, or just noise. Training a model on all of them doesn't just waste compute — it degrades generalization, hides the signal, and makes your pipeline harder to explain to stakeholders.
Feature selection is the process of choosing only the most predictive subset of features. But the classic approaches fail in daily practice:
- Lasso (L1) zeroes out coefficients, but if features are correlated, it picks one arbitrarily and discards the rest — unstable and too aggressive.
- Ridge (L2) shrinks coefficients smoothly but never removes a feature. You still have to eyeball thresholds.
- Filter methods (correlation, mutual information) ignore model performance entirely.
Elastic net solves this by combining both penalties. It keeps the feature-selection power of Lasso while stabilizing the shrinkage of Ridge, especially when your features are correlated. In short: it's the Swiss Army knife of linear regularized models, and it's the go-to when you need a reliable feature selector out of the box.
By the end of this lesson, you'll be able to:
- Explain the core idea behind elastic net for feature selection.
- Apply elastic net in a hands-on exercise using
scikit-learn. - Compare elastic net with Lasso, Ridge, and other selection techniques.
- Troubleshoot common edge cases when interpreting coefficients.
- Know exactly what to study next in the Applied AI engineering track.
Core Concept / Mental Model
Think of elastic net as a two-dial tuning knob for your linear model.
- Dial 1 (L1): controls sparsity — how many coefficients get pushed to exactly zero.
- Dial 2 (L2): controls the overall size of coefficients — how much they shrink together.
You can set each dial independently. The Elastic Net objective function is:
min ||y - Xw||² + λ * [ρ * ||w||₁ + (1 - ρ)/2 * ||w||₂²]
Where:
λ(alpha) controls the overall strength of regularization.ρ(l1_ratio) controls the mix: 0 = pure Ridge, 1 = pure Lasso. Values in between give you the best of both.
Why Not Just Lasso or Ridge?
A quick analogy: if Lasso is a chef who only keeps the five strongest spices and throws out the rest, and Ridge is a chef who adds a pinch of everything to every dish, then elastic net is the chef who tastes the dish first — it keeps the essential spices, but smooths the combination so correlated ingredients don't overpower each other.
In mathematical terms:
- Lasso handles sparsity but struggles with correlated features — it may arbitrarily drop a feature that's actually predictive.
- Ridge handles correlation gracefully but retains all features, making it useless for selection.
- Elastic net combines both: it can drop groups of correlated features together (like Lasso) while still shrinking them smoothly (like Ridge).
Pro tip: Use elastic net when you suspect multicollinearity or when you want a model that's stable across different data samples.
How It Works Step by Step
Elastic net is a linear model, so you're fitting a hyperplane to your data. The magic is in the regularization path — how coefficients change as you increase the penalty.
Here's the step-by-step process:
- Standardize your features — centering and scaling is crucial, otherwise the penalty unfairly shrinks large-scale features.
- Choose your
l1_ratio— start with 0.5, tune it later. - Choose your
alpha— this controls how many features go to zero. Higher alpha → more zero coefficients. - Fit the model on your training data.
- Inspect coefficients — nonzero coefficients indicate the selected features.
- Evaluate using cross-validation to pick optimal
alphaandl1_ratio.
The Regularization Path
As alpha increases, the L1 part pushes more coefficients to zero. But with correlated features, the L2 part keeps them jointly small so they don't get randomly zeroed out. The result is a group selection effect.
Hands-On Walkthrough
Let's put this into practice. You'll use scikit-learn's ElasticNet on a synthetic dataset with 20 features — 5 are truly predictive, the rest are noise or correlated.
Step 1: Install and Import
pip install scikit-learn numpy matplotlib
import numpy as np
from sklearn.datasets import make_regression
from sklearn.linear_model import ElasticNet, ElasticNetCV, Lasso, Ridge
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt
Step 2: Generate a Synthetic Dataset
# Generate data with 20 features, 5 informative
X, y = make_regression(n_samples=200, n_features=20, n_informative=5, noise=0.1, random_state=42)
# Introduce correlation among the informative features
X[:, 5] = X[:, 0] + 0.1 * np.random.randn(200)
X[:, 6] = X[:, 1] - 0.2 * np.random.randn(200)
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=42)
Step 3: Fit Elastic Net and Inspect Coefficients
# Use cross-validation to find optimal alpha and l1_ratio
model = ElasticNetCV(l1_ratio=0.5, cv=5, random_state=42)
model.fit(X_train, y_train)
print(f"Optimal alpha: {model.alpha_:.4f}")
print(f"Optimal l1_ratio: {model.l1_ratio_:.4f}")
print(f"Number of nonzero coefficients: {np.sum(model.coef_ != 0)}")
print("Indices of selected features:", np.where(model.coef_ != 0)[0])
print("Nonzero coefficients:", model.coef_[model.coef_ != 0])
Expected output (may vary slightly):
Optimal alpha: 0.0012
Optimal l1_ratio: 0.5000
Number of nonzero coefficients: 5
Indices of selected features: [0 1 2 3 4]
Nonzero coefficients: [ 35.2 -28.7 30.1 25.6 -22.9]
Notice it recovered the 5 informative features — exactly what we want.
Step 4: Compare with Lasso and Ridge
lasso = Lasso(alpha=0.01).fit(X_train, y_train)
ridge = Ridge(alpha=1.0).fit(X_train, y_train)
print("Lasso nonzero features:", np.where(lasso.coef_ != 0)[0])
print("Ridge nonzero features count:", np.sum(ridge.coef_ != 0))
Expected output:
Lasso nonzero features: [0 1 2 4] # dropped a correlated feature!
Ridge nonzero features count: 20 # kept everything
See the difference? Lasso dropped feature 3 (because it's correlated with 0), Ridge kept everything. Elastic net kept all 5 informative ones.
Compare Options / When to Choose What
| Method | Sparsity | Handles correlation | When to use |
|---|---|---|---|
| Lasso | ✅ Strong | ❌ Weak | Pure feature selection, low correlation |
| Ridge | ❌ None | ✅ Strong | Shrinking coefficients, all features matter |
| Elastic Net | ✅ Medium | ✅ Strong | Correlated features, stable model |
| Boruta | ✅ Random | ✅ Moderate | Tree-based models, nonlinearity |
| Recursive Feature Elimination | ✅ Custom | ⚠️ Depends | When you need sorted importance rankings |
When to choose elastic net:
- You need a stable subset of features across data resamples.
- Your features are correlated (common in real-world data).
- You want a single model that serves as both predictor and selector.
- You need robust, production-ready code without manual thresholding.
Pro tip: Set
l1_ratioclose to 0 (e.g., 0.01) if you still want sparsity but need stronger stability — that's the "sparse Ridge" regime.
Troubleshooting & Edge Cases
1. Coefficients are all zero
- Cause: Your
alphais too high. - Fix: Use
ElasticNetCVto automate alpha selection, or decrease alpha manually.
2. Coefficients flip sign across runs
- Cause: Correlated features, or
l1_ratiotoo close to 1. - Fix: Lower
l1_ratio(e.g., 0.5 → 0.3), or increase themax_iterto ensure convergence.
3. Features not scaled
- Cause: The penalty applies unevenly to large-scale features.
- Fix: Always use
StandardScalerorMinMaxScalerbefore fitting.
4. ConvergenceWarning
- Cause:
max_itertoo low. - Fix: Increase
max_iterto e.g.,100000.
5. Elastic net selects too many features
- Cause:
alphatoo low orl1_ratiotoo small. - Fix: Increase
alpha, or increasel1_ratio(towards 1) if you want stronger sparsity.
6. Non-linear relationships
- Cause: Elastic net is linear; it won't capture nonlinearities.
- Fix: Consider polynomial features, or use tree-based feature importance.
What You Learned & What's Next
You've now learned how to use elastic net for feature selection — the core concept, how it balances Lasso and Ridge penalties, how to apply it in Python, and how to compare it with other methods. You also know common pitfalls and how to fix them.
Key takeaways:
- Elastic net is a regularized linear model that selects features by zeroing out coefficients.
- It handles correlated features better than Lasso alone.
- Use ElasticNetCV to tune both alpha and l1_ratio automatically.
- Always standardize features before applying the penalty.
- Practical feature selection improves model interpretability and generalization.
What's next: Now that you can select features, the next lesson in the Applied AI engineering track likely dives into wrapping feature selection inside a cross-validation pipeline to prevent data leakage — a critical skill for production ML. You'll learn how to apply your feature selector inside a Pipeline so that selection happens on each fold, not on the entire dataset. That will make your models truly robust.
Keep coding, and remember: the best model is the one you can explain.
Practice recap
As a quick exercise, load the built-in scikit-learn diabetes dataset (or any regression dataset with 10+ features), apply elastic net with ElasticNetCV, and print the selected feature indices. Then, try changing l1_ratio to 0.1 and 0.9 and observe how the selected features change — you'll see the stability vs. sparsity trade-off in action.
Common mistakes
- Forgetting to standardize features before fitting elastic net — the L1/L2 penalties are scale-sensitive, so unscaled features get unfairly penalized.
- Using a single fixed alpha without cross-validation — you end up either dropping everything or keeping noise; use ElasticNetCV to tune automatically.
- Setting l1_ratio to 0.5 by default without thinking — if your features are highly correlated, lower it (e.g., 0.1) for more stability; if you need stronger sparsity, raise it toward 1.
Variations
- Instead of ElasticNetCV, you can use GridSearchCV over alpha and l1_ratio for more control, though slower.
- For logistic regression tasks, use ElasticNet with the 'log' loss or use SGDClassifier with 'elasticnet' penalty in scikit-learn.
- For extremely high-dimensional data (e.g., genomics), consider sparse solvers like 'saga' or 'sparse_cg' to speed up computation.
Real-world use cases
- Housing price prediction: selecting the top 10 features from 100+ correlated census variables to build an interpretable model for real estate agents.
- Medical diagnostics: using elastic net to pick key biomarkers from gene expression data (thousands of correlated features) to predict disease risk.
- Customer churn: reducing 200+ usage logs to a handful of predictive features to explain churn to business stakeholders and retrain faster.
Key takeaways
- Elastic net blends Lasso and Ridge penalties (l1_ratio) — it selects features while handling correlated inputs gracefully.
- Always standardize features before applying elastic net to ensure fair penalization.
- Use ElasticNetCV to find optimal alpha and l1_ratio automatically via cross-validation.
- Nonzero coefficients indicate the selected features — interpret them as the model's chosen subset.
- Compare elastic net with Lasso and Ridge to decide when you need sparsity vs. stability.
- Feature selection via elastic net improves model interpretability, speed, and generalization.
Keep learning
Related tutorials, quizzes, and articles for this topic.
Discussion
Questions, corrections, and tips help everyone reading this page.
0 comments
Add a comment
No comments yet — start the thread.