Sub topic 15
Elastic net: mixing L1 and L2
This chapter assumes you know ridge (L2) and lasso (L1) regularisation, and that you scale features before fitting a penalised model. Elastic net combines both penalties in one model.
Why combine two penalties
Each penalty has a weakness:
- Lasso can behave unstably when features are strongly correlated. It tends to keep one feature from a correlated group and drop the others, and which one it keeps can change with small changes in the data.
- Ridge handles correlated features well and shares weight among them. It never sets a coefficient to exactly zero, so it does not select features.
Elastic net adds both penalties to the loss. You get sparsity from L1 and stable handling of correlated groups from L2.
The objective
scikit-learn's ElasticNet minimises this:
(1 / (2n)) * ||y - Xw||^2
+ alpha * l1_ratio * ||w||_1
+ 0.5 * alpha * (1 - l1_ratio) * ||w||^2Two hyperparameters control it:
| Parameter | Meaning | Extremes |
|---|---|---|
| `alpha` | Overall penalty strength | 0 means no regularisation. Larger values shrink coefficients more. |
| `l1_ratio` | Share of the penalty that is L1 | 1 is lasso. Values near 0 behave like ridge. |
Comparing lasso and elastic net
The data below has two nearly identical features, both driven by the same hidden signal, plus four noise features.
import numpy as np
from sklearn.linear_model import ElasticNet, Lasso
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
rng = np.random.default_rng(0)
base = rng.normal(size=100)
X = np.column_stack([
base + rng.normal(0, 0.1, size=100),
base + rng.normal(0, 0.1, size=100),
rng.normal(size=(100, 4)),
])
y = 3 * base + rng.normal(0, 0.5, size=100)
lasso = make_pipeline(StandardScaler(), Lasso(alpha=0.1)).fit(X, y)
enet = make_pipeline(StandardScaler(), ElasticNet(alpha=0.1, l1_ratio=0.5)).fit(X, y)
print("lasso:", np.round(lasso[-1].coef_, 2))
print("enet: ", np.round(enet[-1].coef_, 2))Expect lasso to put most of the weight on one of the first two features. Elastic net should spread the weight more evenly across both. The noise features should be at or near zero in each model. Exact numbers depend on your library version.
This script only inspects coefficients. To estimate error, keep the scaler inside the pipeline and score with cross-validation, as in the ridge chapter.
Choosing the settings
Tune alpha and l1_ratio together with cross-validation, never on the test set. scikit-learn's ElasticNetCV does this and accepts a list such as l1_ratio=[0.1, 0.5, 0.9, 1.0].
Common errors
- Skipping feature scaling. The penalty depends on coefficient size, so unscaled features are penalised unevenly.
- Reading
l1_ratio=0.5as a "neutral" default. It is only a starting point, so tune it. - Assuming elastic net always beats lasso or ridge. When features are uncorrelated, the gain is often small.
Exercise
You fit a lasso model on a group of five highly correlated features. It keeps one and zeros the other four, and the kept feature changes when you resample the data. Which model would you try next, and which hyperparameter would you adjust?
Show answer
Try elastic net. Lower l1_ratio below 1 so the L2 part of the penalty spreads weight across the correlated features. Tune alpha and l1_ratio together with cross-validation.