08 / 27 · Linear Regression
Regularization
Adding a penalty on the size of the weights to least squares: ridge regression, its closed form and its shrinkage of the unstable directions, the prior behind each penalty, the constrained form and why an L1 ball produces exact zeros, the lasso with soft thresholding, coordinate descent and its path, the elastic net for correlated features, and choosing the penalty's strength by cross-validation.
Least squares · Validation and overfitting · Bayesian inference
On this page 12
The optimizers note finished the machinery: least squares can be fitted for any number of examples and features. Fitting it is not the problem any more; trusting the fit is. The least-squares note showed two nearly identical features whose coefficients swung between and from one draw of the noise to the next, and a training that climbed to with 40 useless features while the validation fell below zero. This note changes the objective rather than the optimizer: it adds a penalty that makes large weights expensive. The penalty trades a little bias for a large reduction of variance, has a Bayesian reading as a prior, and in its L1 form selects features by setting weights exactly to zero.
Notation. As in the least-squares note, is the design matrix and the targets. Throughout this note the features are standardized (mean 0, variance 1) and the target is centred, so no intercept is needed: it is and is never penalized. is the strength of the penalty, and . is the rank of the design matrix. Its singular value decomposition is , where and have orthonormal columns and and is diagonal with the singular values (always with an index, unlike the noise level ), so the nonzero eigenvalues of are . is the soft-thresholding function.
Ridge: a penalty on the size of the weights#
Ridge regression (Hoerl and Kennard, 1970) minimizes the squared error plus the squared length of the weight vector:
The gradient is , and setting it to zero gives
The matrix has eigenvalues (and along the directions with no data), all at least . For any it is positive definite and invertible: the solution exists and is unique even with collinear features or with more features than examples, the cases where least squares broke down. Scaling matters: the penalty treats a weight of 1 on a feature measured in metres the same as on one measured in millimetres, which is why the features are standardized first, with statistics computed on the training data only.
Worked example. One feature and centred target : and . Least squares gives ; ridge with gives , half the size. Now duplicate the feature, . Least squares has infinitely many solutions, every with . Ridge has one: by symmetry , and the first equation of reads , so . The penalty splits the weight equally between identical features, the split with the smallest norm.
On the nearly identical features of the least-squares note (correlation , true weights ), three fresh draws of the noise give least-squares weights , and , and ridge with gives , and (the Code section).
Which directions it shrinks#
Substitute the SVD. Since and , the fitted values are
Least squares projects onto each principal direction of the data and keeps the projection in full. Ridge multiplies it by : directions in which the features vary a lot () are kept almost intact, directions of small variance () are shrunk almost to zero (Hastie, Tibshirani and Friedman, 2009, section 3.4.1). These are exactly the directions that made least squares unstable, because the variance of the least-squares coefficients along is . The sum of the factors, , is the trace of the ridge hat matrix and is called the effective degrees of freedom: at , falling to 0 as .
On the diabetes data (442 patients, 10 standardized features), the eigenvalues of range from down to and . The smallest belongs to a combination dominated by the blood-serum measurements s1 and s2, whose correlation is . With the largest direction is kept at , the two smallest at and , and ; with , ; with , .
The shrinkage introduces bias: . It removes much more variance, which is why the mean squared error can fall, the trade-off of the estimators note that the bias–variance note applies to every model of the course.
The Bayesian reading#
The penalty is a prior. With Gaussian noise of variance and independent priors , the negative log-posterior is the ridge objective divided by , with ; a Laplace prior gives the L1 penalty of the lasso below, with . The derivation is in the MAP section of the Bayesian inference note. Two consequences are worth stating here:
- A strong penalty is a confident prior that the weights are small; is a flat prior and gives back least squares.
- For the Gaussian prior the posterior is Gaussian, so is both its mode and its mean, and the posterior covariance gives uncertainties. For the Laplace prior the lasso estimate is only the mode; the posterior puts zero probability on any weight being exactly 0, so the sparsity below is a property of the optimization, not of the posterior.
The constrained form: why L1 gives zeros#
For each there is a radius such that the penalized problem has the same solution as the constrained one,
by the Lagrangian duality of convex problems: is the multiplier of the constraint, and a smaller corresponds to a larger . The constrained form explains the different behaviour of the two penalties.
Why the lasso zeroes weights and ridge does not
The contours of the squared error are ellipses around the least-squares solution . The constrained solution is the point of the feasible set on the lowest contour, the point where a growing ellipse first touches the set. The L2 ball is round, so the touching point is wherever the ellipse’s shape puts it, generically with all coordinates nonzero. The L1 ball is a diamond (a cross-polytope in dimensions) whose corners lie on the axes, and its corners stick out: an ellipse approaching from a wide range of directions hits a corner first, and at a corner some coordinates are exactly zero. In the figure the condition is visible as a computation: at the corner the negative gradient of the loss is , which lies strictly between the normals and of the two edges that meet there, so no move along the diamond’s boundary lowers the loss.
The lasso#
The lasso (Tibshirani, 1996) is the L1-penalized problem,
It is convex but not differentiable where a weight is zero, and it has no closed form in general. With orthonormal features, , it does. Write , the least-squares solution in that case; then , and the objective splits into one problem per coordinate, . For the derivative vanishes at , which is positive only if ; symmetrically for ; otherwise the minimum is at 0. Together:
Ridge shrinks every coefficient by the same factor; the lasso subtracts the same amount from every coefficient’s absolute value and sets those smaller than to zero.
Worked example. Orthonormal features with and . The lasso gives : two features are dropped. Ridge gives : all three are kept, smaller. scikit-learn’s Lasso minimizes , so its is in this note’s units; with that conversion it returns (the Code section).
Coordinate descent. For general , minimize over one weight at a time with the others fixed. The one-dimensional problem is the orthonormal one with replaced by the correlation of feature with the partial residual :
Cycling over the coordinates converges for the lasso (Friedman et al., 2007), and computing the solutions for a decreasing sequence of , each started from the previous one, gives the whole regularization path cheaply; this is what scikit-learn does. A related method takes a gradient step on the squared error and then soft-thresholds all coordinates (the proximal gradient method); the least angle regression algorithm computes the path exactly, because it is piecewise linear in (Efron et al., 2004).
Where the path starts. At the subgradient condition for optimality is for every . So all weights are zero for , and the first feature to enter is the one most correlated with the target. On the diabetes data , attained by bmi.
Coefficient paths of ridge and the lasso
The two paths in the figure show the difference on real data. Ridge pulls the opposite coefficients of the correlated pair s1 and s2 ( and in least squares) towards each other and then both towards zero; no coefficient is ever exactly zero. The lasso adds features one at a time as decreases, in the order bmi, s5, bp, s3, sex, s6, s1, s4, s2, age, and at the value chosen by cross-validation it keeps seven of the ten.
Correlated features: the elastic net#
The lasso’s selection is unstable within a group of correlated features. When two features carry the same signal, the L1 penalty is the same for any split of the weight between them, for weights of one sign, and the squared error barely distinguishes the splits, so the lasso tends to keep one feature and drop the other, and which one depends on the noise. On two copies of one signal with correlation plus a useless feature, three draws of the data give lasso weights , and (scikit-learn’s ).
The elastic net (Zou and Hastie, 2005) adds a ridge term: . The squared term is strictly convex, so it prefers the equal split, and Theorem 1 of the paper bounds the difference between the weights of two features by a multiple of , where is their correlation: the grouping effect. The same three draws give , and (scikit-learn’s and l1_ratio=0.5), and the useless feature is still removed.
Choosing the penalty by validation#
The penalty strength is a hyperparameter, and the validation note gives the procedure: fit for a grid of on a log scale, estimate the error of each by cross-validation, keep the best, and report the final performance with a nested scheme or a test set, since the best of many cross-validated scores is optimistic. Ridge has a shortcut: its fitted values are linear in , , and the leave-one-out residual is , so leave-one-out cross-validation costs one fit per (Hastie, Tibshirani and Friedman, 2009, section 7.10.1, eq. 7.51); scikit-learn’s RidgeCV uses it.
How much regularization helps depends on how many features there are per example. On the diabetes data, 5-fold cross-validated :
| Features | Count | Least squares | Ridge, best | Lasso, best |
|---|---|---|---|---|
| original | 10 | () | () | |
| with all products of degree 2 | 65 | () | () | |
| with all products up to degree 3 | 285 | () | () |
With ten features and 442 patients, the variance of least squares is already small and the penalty changes nothing. With 285 features on about 350 training patients per fold, least squares fits the noise so badly that its validation is ; ridge and the lasso bring it back to and . On this data the added features carry little signal, and the best regularized models only match the plain linear model on the original ten; regularization makes many features safe, not useful.
Other ways of limiting a model act like penalties and appear later in the course: stopping gradient descent early (roughly an L2 penalty with after steps), dropout and data augmentation, all in the training note, and pruning in the decision-tree note. Weight decay, shrinking every weight by a fixed fraction at each step, is the same as an L2 penalty for plain SGD; with adaptive optimizers such as Adam the two differ, and the decoupled form, AdamW, is the one used in practice (weight decay under Adam). Weight decay regularizes the fine-tuning of language models, and an L1 penalty on activations makes the sparse autoencoder sparse.
What changed#
| Least squares | Ridge | Lasso | |
|---|---|---|---|
| Objective | |||
| Solution | , if it exists | , always | no closed form; coordinate descent |
| Collinear features or | not unique, unstable | unique, weight shared | unique fit, selection unstable within groups |
| Zeros | not enforced | not enforced (only by coincidence) | exact zeros: feature selection |
| Prior | flat | Gaussian | Laplace (as MAP) |
| Hyperparameter | none | , by cross-validation | , by cross-validation |
| Bias and variance | unbiased, high variance | biased, lower variance | biased, lower variance |
Limitations#
- The penalty depends on the units. Features must be standardized, and the result changes with the scaling chosen.
- Bias. Both penalties shrink large true coefficients too; the lasso’s estimates of the features it keeps are biased towards zero, and refitting least squares on the selected features is a common remedy.
- The lasso keeps at most features when and the features are in general position, for example drawn from a continuous distribution: the solution is then unique with probability one and has at most nonzero weights (Tibshirani, 2013). With exactly duplicated columns the fit stays unique but the weights do not: any split of a weight between two copies, with the same sign, has the same fit and penalty. Within strongly correlated groups the choice is arbitrary; the elastic net softens both limits.
- Selection is not inference. A feature dropped by the lasso may still matter, and standard errors computed after selection are too small.
- One more hyperparameter. costs a cross-validation loop and makes an honest error estimate need a nested one.
What you should remember#
- Ridge: , unique for every ; it shrinks each principal direction by , the small-variance directions most; .
- The penalties are priors: Gaussian gives ridge with , Laplace gives the lasso as a MAP estimate.
- The L1 ball has corners on the axes, so the lasso sets weights exactly to zero; with orthonormal features it soft-thresholds the least-squares coefficients at , while ridge divides them by .
- All lasso weights are zero for ; the path is computed by coordinate descent with warm starts.
- The elastic net adds an L2 term so that correlated features share their weight.
- Standardize features, do not penalize the intercept, choose by cross-validation and report with nested cross-validation.
Code#
The worked examples, ridge on collinear features, the shrinkage factors and the lasso path on diabetes, cross-validation with polynomial features, and the elastic net. The printed numbers come from NumPy 2.1.3 and scikit-learn 1.9.1, and with max_iter=50_000 no lasso fit raises a convergence warning:
import numpy as np, warnings
from sklearn.datasets import load_diabetes
from sklearn.linear_model import Ridge, Lasso, ElasticNet, LinearRegression, lasso_path
from sklearn.model_selection import KFold, cross_val_score
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import PolynomialFeatures, StandardScaler
warnings.filterwarnings('ignore', category=FutureWarning) # only scikit-learn 1.9's notice about n_alphas; convergence warnings stay on
rng = np.random.default_rng(0)
ridge = lambda X, y, lam: np.linalg.solve(X.T @ X + lam * np.eye(X.shape[1]), X.T @ y)
# worked example: x = (-1, 0, 1), centred y = (-2, 0, 2); then the same feature twice
x = np.array([[-1.], [0], [1]]); y = np.array([-2., 0, 2])
print(ridge(x, y, 1e-12), ridge(x, y, 2.0), ridge(np.hstack([x, x]), y, 2.0).round(3)) # [2.] [1.] [0.667 0.667]
# orthonormal features: the lasso soft-thresholds z = X^T y at lambda/2, ridge divides it by 1 + lambda
n = 50
Xo = np.linalg.qr(rng.normal(size=(n, 3)))[0] # orthonormal columns
z = np.array([3.0, 1.0, -0.5]); yo = Xo @ z
lam = 2.0 # objective |y - Xw|^2 + lam |w|_1; scikit-learn's alpha = lam / (2n)
print(Lasso(alpha=lam / (2 * n), fit_intercept=False, tol=1e-12).fit(Xo, yo).coef_.round(3) + 0, ridge(Xo, yo, lam).round(3))
# [2. 0. 0.] [ 1. 0.333 -0.167]
# two nearly identical features (the least-squares note's example): ridge with lambda = 1 is stable
x1 = rng.normal(size=100); X2 = np.column_stack([x1, x1 + 0.01 * rng.normal(size=100)])
for _ in range(3):
t = X2.sum(1) + rng.normal(size=100)
print(np.linalg.lstsq(X2, t, rcond=None)[0].round(2), ridge(X2, t, 1.0).round(2))
# [ 4.81 -2.86] [0.99 0.95] | [-15.84 17.95] [0.97 1.13] | [-0.11 2.09] [0.98 0.99]
# diabetes, standardized features and centred target: closed form = scikit-learn, shrinkage per principal direction
X, yd = load_diabetes(return_X_y=True)
Xs = (X - X.mean(0)) / X.std(0); yc = yd - yd.mean()
print(np.abs(ridge(Xs, yc, 10.0) - Ridge(alpha=10.0).fit(Xs, yc).coef_).max() < 1e-9) # True
d2 = np.linalg.eigvalsh(Xs.T @ Xs)[::-1]
print(d2.round(1))
for lam in (1, 10, 100, 1000):
f = d2 / (d2 + lam)
print(lam, f[[0, 8, 9]].round(3), f.sum().round(2)) # shrinkage of the largest and two smallest directions; effective df
# [1778.7 659.6 533. 422.3 292.7 266.4 237.2 191.7 34.6 3.8]
# 1 [0.999 0.972 0.791] 9.74 | 10 [0.994 0.776 0.275] 8.83 | 100 [0.947 0.257 0.036] 6.59 | 1000 [0.64 0.033 0.004] 2.51
# the lasso path: the largest useful lambda, and the order in which features enter
names = load_diabetes().feature_names
print(round(2 * np.abs(Xs.T @ yc).max(), 0), names[np.abs(Xs.T @ yc).argmax()]) # 39921.0 bmi
alphas, coefs, _ = lasso_path(Xs, yc, n_alphas=2000, eps=1e-4)
order = []
for k in range(coefs.shape[1]):
order += [names[j] for j in np.flatnonzero(coefs[:, k]) if names[j] not in order]
print(order) # ['bmi', 's5', 'bp', 's3', 'sex', 's6', 's1', 's4', 's2', 'age']
# choosing lambda by 5-fold cross-validation, on the 10 features and on all their products up to degree 2 and 3
cv = KFold(5, shuffle=True, random_state=0)
for deg in (1, 2, 3):
pipe = lambda m: make_pipeline(PolynomialFeatures(deg, include_bias=False), StandardScaler(), m)
score = lambda m: round(cross_val_score(pipe(m), X, yd, cv=cv, scoring='r2').mean(), 3)
p = PolynomialFeatures(deg, include_bias=False).fit(X).n_output_features_
r = {a: score(Ridge(alpha=a)) for a in (1, 10, 100, 300, 1000)}
l = {a: score(Lasso(alpha=a, max_iter=50_000)) for a in (0.3, 1, 2, 3, 5)}
print(deg, p, score(LinearRegression()), max(r, key=r.get), max(r.values()), max(l, key=l.get), max(l.values()))
# 1 10 0.489 1 0.49 1 0.49 | 2 65 0.412 100 0.475 2 0.49 | 3 285 -41.699 300 0.441 3 0.47
# two copies of one signal (correlation 0.999) and a useless feature, three draws: the lasso picks a copy, the elastic net shares
for seed in (0, 1, 2):
r = np.random.default_rng(seed); zz = r.normal(size=200)
Xe = np.column_stack([zz + 0.03 * r.normal(size=200), zz + 0.03 * r.normal(size=200), r.normal(size=200)])
Xe = (Xe - Xe.mean(0)) / Xe.std(0); ye = 2 * zz + r.normal(size=200); ye -= ye.mean()
print(Lasso(alpha=0.5).fit(Xe, ye).coef_.round(2) + 0, ElasticNet(alpha=0.5, l1_ratio=0.5).fit(Xe, ye).coef_.round(2) + 0)
# [1.46 0. 0. ] [0.76 0.76 0. ] | [0. 1.4 0. ] [0.72 0.74 0. ] | [0.6 0.76 0. ] [0.71 0.72 0. ]
Next#
The linear regression section ends with a model that predicts a number, is fitted by formula or by descent, and does not chase the noise. Many targets are not numbers but classes: spam or not, default or repayment. The next note asks what a linear model should minimize when the answer is a side of a hyperplane, why squared error is the wrong loss for that, and which losses replace it. The ridge penalty comes along: it is the second term of the support vector machine objective and the heart of kernel ridge regression.
References#
- E. Sokolov et al. Машинное обучение (HSE), lecture notes, lecture 4: regularization, hyperparameters, sparse models and the proximal step, feature transformations.
- A. E. Hoerl, R. W. Kennard. Ridge regression: biased estimation for nonorthogonal problems. Technometrics, 12(1), 1970.
- R. Tibshirani. Regression shrinkage and selection via the lasso. Journal of the Royal Statistical Society B, 58(1), 1996.
- H. Zou, T. Hastie. Regularization and variable selection via the elastic net. Journal of the Royal Statistical Society B, 67(2), 2005. Section 2.3, Theorem 1 (the grouping effect).
- B. Efron, T. Hastie, I. Johnstone, R. Tibshirani. Least angle regression. Annals of Statistics, 32(2), 2004.
- J. Friedman, T. Hastie, H. Höfling, R. Tibshirani. Pathwise coordinate optimization. Annals of Applied Statistics, 1(2), 2007.
- R. J. Tibshirani. The lasso problem and uniqueness. Electronic Journal of Statistics, 7, 2013. Uniqueness for features from a continuous distribution and the bound of nonzero weights.
- T. Hastie, R. Tibshirani, J. Friedman. The Elements of Statistical Learning, 2nd ed. Springer, 2009. Section 3.4.1 (ridge regression, the SVD view and effective degrees of freedom), Section 3.4.2 (the lasso), Section 3.4.3 (the constraint regions and soft thresholding), Section 3.8.6 (pathwise coordinate optimization), Section 7.10.1 (cross-validation and the leave-one-out shortcut).
- C. M. Bishop. Pattern Recognition and Machine Learning. Springer, 2006. Section 3.1.4 (regularized least squares), Section 3.3 (Bayesian linear regression).