Elastic Net

Lasso and Ridge combined, for when your features come in related groups.

🟡 intermediate4 min readregularization
Elastic Net mixes the L1 and L2 penalties into one. Lasso, the L1 penalty, is great at dropping useless features, but when several features are strongly related it tends to keep one at random and throw away the rest. Adding a little of the L2 penalty fixes that, so related features rise or fall together as a group. Elastic Net is the safe choice when you have more features than samples, as in genetics, or when many of your features overlap.

What is Elastic Net?

L1 Lasso and L2 Ridge regularization each have unique strengths and weaknesses:

  1. L1 Lasso: Performs sparse feature selection by zeroing out irrelevant features, but selects at random among strongly correlated features.
  2. L2 Ridge: Keeps all features and handles correlated groups smoothly, but cannot zero out uninformative features.

Elastic Net (Zou & Hastie, 2005) combines both penalties into a single loss function to get the best of both worlds:

LossElasticNet=MSE+λ1∑j=1p∣wj∣+λ2∑j=1pwj2\text{Loss}_{\text{ElasticNet}} = \text{MSE} + \lambda_1 \sum_{j=1}^p |w_j| + \lambda_2 \sum_{j=1}^p w_j^2

In scikit-learn, this is parameterized using total penalty strength α\alpha (alpha) and ratio l1_ratiol1\_ratio:

Penalty=α⋅l1_ratio⋅∑∣wj∣+12α⋅(1−l1_ratio)⋅∑wj2\text{Penalty} = \alpha \cdot l1\_ratio \cdot \sum |w_j| + \frac{1}{2} \alpha \cdot (1 - l1\_ratio) \cdot \sum w_j^2

The Grouping Effect

Consider a genomics dataset where 10 different genes are strongly correlated because they belong to the same biological pathway.

  L1 LASSO:        Picks 1 gene at random, sets the other 9 to zero. (Instable across random seeds!)
  L2 RIDGE:        Keeps all 10 genes, but also keeps 10,000 non useful genes.
  ELASTIC NET:     Selects all 10 correlated genes as a GROUP, while zeroing out non useful genes!

Elastic Net creates a convex constraint shape (a rounded diamond) that enables Grouped Feature Selection.

When to Use Elastic Net

  1. p≫np \gg n Datasets: When you have far more features pp than data samples nn (like 50,000 gene expressions on 200 patients). Standard L1 Lasso can select at most nn features. Elastic Net can select more than nn features.
  2. Collinear Data: When features exhibit strong pairwise correlations (like sensor arrays or financial metrics).

Say this out loud

Elastic Net combines L1 Lasso and L2 Ridge penalties into a single objective. While pure L1 Lasso selects one feature at random from a correlated group, Elastic Net uses L2 regularization to select correlated feature groups together while using L1 regularization to zero out uninformative features.

Followups to expect

  1. How do you tune l1_ratio in scikit-learn? Use ElasticNetCV, performing grid search cross validation over l1_ratio values like [0.1, 0.5, 0.7, 0.9, 0.99].
  2. Is Elastic Net computationally slower than Lasso? Elastic Net adds a small matrix operation overhead, but uses coordinate descent optimization algorithms that converge quickly on sparse high dimensional data.

Check yourself

Question 1 of 3

Why does L1 Lasso regression struggle on datasets containing strongly correlated features (such as 10 gene expressions measuring the same pathway)?

More in Classical ML

See all →
Bias–Variance Tradeoff4 minOverfitting vs Underfitting3 minLinear Regression4 min