🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Machine Learning Notes
Topic #200

Elastic Net

Elastic Net combines L1 and L2 regularization into a single penalty — getting Lasso's automatic feature selection alongside Ridge's more stable handling of correlated features, instead of having to pick strictly one or the other.

Formula

\[ J_{\text{ElasticNet}}(w) = J(w) + \lambda\left[\rho\sum_{i}|w_i| + (1-\rho)\sum_{i}w_i^2\right] \]

\(\rho\) (the "L1 ratio," between 0 and 1) controls the mix: \(\rho=1\) recovers pure Lasso, \(\rho=0\) recovers pure Ridge, and values in between blend both penalties' behavior.

Why This Combination Solves a Real Lasso Weakness

Plain Lasso, when facing a group of highly correlated features, tends to arbitrarily select just one and zero out the rest — an unstable, somewhat arbitrary choice. Elastic Net's added L2 component encourages correlated features to be selected (or shrunk) together, rather than picking one winner arbitrarily — combining sparsity with more grouped, stable behavior.

Python Implementation

from sklearn.linear_model import ElasticNet
from sklearn.model_selection import GridSearchCV

param_grid = {
    "alpha": [0.01, 0.1, 1, 10],
    "l1_ratio": [0.1, 0.5, 0.7, 0.9, 1.0],   # 1.0 = pure Lasso, 0.0 = pure Ridge
}
grid_search = GridSearchCV(ElasticNet(), param_grid, cv=5, scoring="neg_mean_squared_error")
grid_search.fit(X_train, y_train)
print("Best params:", grid_search.best_params_)

Practical Use Cases

  • High-dimensional data with groups of correlated features, where you still want some feature selection
  • Genomics and other domains where features are naturally grouped and correlated, and Lasso's arbitrary single-feature selection is undesirable

The Real Cost — An Extra Hyperparameter

Elastic Net requires tuning two hyperparameters (\(\lambda\) and \(\rho\)) instead of one, meaningfully increasing the tuning search space compared to plain Lasso or Ridge — a real practical tradeoff for its added flexibility.

Common Mistakes

  • Defaulting to Elastic Net without first checking whether plain Lasso or Ridge alone would already work fine — the extra tuning complexity is only worth it when correlated-feature grouping is genuinely a concern.
  • Fixing \(\rho\) arbitrarily instead of tuning it alongside \(\lambda\) — the right mix genuinely depends on the data's correlation structure.

Interview Relevance

Q: "When would you choose Elastic Net over plain Lasso?" When features are both numerous (suggesting feature selection is useful) and meaningfully correlated in groups (where plain Lasso's tendency to arbitrarily pick one feature per group and zero the rest would be unstable) — Elastic Net's added L2 component encourages more stable, grouped selection behavior.

Practice Question

You have a dataset with 5 highly correlated features representing slightly different measurements of the same underlying quantity. Would you lean toward Lasso or Elastic Net, and why?

Want to go beyond the notes?

Join CodingNow 2.0's Machine Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Elastic Net – FAQs

Quick answers about learning Elastic Net in Machine Learning.

This free note from CodingNow 2.0 explains Elastic Net in Machine Learning — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Machine Learning topic on CodingNow 2.0, including Elastic Net, is 100% free with no signup required.
With focused practice, most students grasp Elastic Net in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now