Sub topic 13
L1 regularisation (lasso) and sparse models
This chapter assumes you know how ridge regression adds an L2 penalty to a loss, that features should be scaled before penalising them, and how scikit-learn's fit and predict work.
The L1 penalty
Lasso regression minimises the usual squared error plus a penalty on the sum of the absolute values of the coefficients:
loss = mean squared error + alpha × (|w1| + |w2| + ... + |wp|)
Here alpha sets the penalty strength. Ridge squares the weights, while lasso takes their absolute values. That difference changes the result a lot. Lasso often drives some coefficients to exactly zero, so the model uses only a subset of the features. A model with many zero coefficients is called sparse.
Why L1 gives exact zeros
Look at the size of the push toward zero. The L2 penalty's gradient shrinks as a weight gets small, so ridge makes weights small but rarely zero. The L1 penalty pulls with a constant force, alpha, no matter how small the weight is. If a feature's usefulness is smaller than that constant pull, the weight is held at exactly zero.
Example: finding the useful features
The data below has ten features, but only the first two affect the target.
import numpy as np
from sklearn.linear_model import Lasso
from sklearn.preprocessing import StandardScaler
rng = np.random.default_rng(0)
X = rng.normal(size=(100, 10))
y = 3 * X[:, 0] - 2 * X[:, 1] + rng.normal(0, 0.5, size=100)
X_scaled = StandardScaler().fit_transform(X)
lasso = Lasso(alpha=0.1).fit(X_scaled, y)
print(np.round(lasso.coef_, 2))The first two coefficients come out large, near 3 and -2. Most of the other eight are typically exactly 0.0. Some may remain slightly nonzero at this alpha, and a larger alpha removes more of them.
Scaling matters here for the same reason as in ridge. The penalty treats all weights equally, so features on different scales would be penalised unfairly.
Lasso as feature selection
Because zeroed features no longer affect predictions, lasso performs feature selection while it fits. This helps in two ways:
- The model is easier to interpret, since you can list the features that survived.
- With many noisy features and few samples, removing useless ones often lowers variance.
You can read off the selected features with np.flatnonzero(lasso.coef_).
Strength and trade-offs
alpha controls how sparse the model is. A very small value behaves like plain linear regression. A very large value zeroes every coefficient and the model underfits, predicting roughly the mean. Pick alpha with cross-validation, as covered in the previous chapter on strength tuning. LassoCV does this for you.
Know these limits:
- Correlated features. If two features carry the same signal, lasso tends to keep one and drop the other, and which one it keeps can change with small changes in the data. Ridge spreads weight across both.
- Shrinkage of kept weights. The surviving coefficients are biased toward zero, so they are smaller than an unpenalised fit would give.
- Selection is unstable on small data. Do not treat the chosen set as a definitive list of important features.
Exercise
You fit a lasso model with a very large alpha and every coefficient is zero. Is the model overfitting or underfitting, and what should you change?
Show answer
It is underfitting (high bias), because it ignores all features and predicts about the mean. Reduce alpha, ideally by choosing it with cross-validation.