On this page
Ordinary least squares (OLS) picks the weights w that minimise ‖Xw − y‖². With few data points, or inputs that move together, those weights can swing wildly and fit noise. Regularisation puts a price on large weights. Ridge (L2) and lasso (L1) are the two standard prices, and one piece of geometry separates them.
What are ridge and lasso regularisation?
Ridge regression minimises ‖Xw − y‖² + λ‖w‖², where ‖w‖² is the sum of the squared weights and λ ≥ 0 sets the strength. Arthur Hoerl and Robert Kennard set it out in their 1970 paper (Technometrics, 1970). The scikit-learn user guide notes that a larger penalty means more shrinkage and coefficients more robust to collinearity (scikit-learn: ridge regression).
Lasso minimises ‖Xw − y‖² + λ‖w‖₁, where ‖w‖₁ is the sum of the absolute values of the weights. Robert Tibshirani proposed it in 1996, and his abstract notes that because of the nature of this constraint it tends to produce some coefficients that are exactly 0 (Journal of the Royal Statistical Society B, 1996).
How do you get the ridge closed form (XᵀX + λI)⁻¹Xᵀy?
Write f(w) = (Xw − y)ᵀ(Xw − y) + λwᵀw and set its gradient to zero:
The gradient is 2Xᵀ(Xw − y) + 2λw.
Setting it to zero gives (XᵀX + λI)w = Xᵀy.
So w = (XᵀX + λI)⁻¹Xᵀy.
The inverse always exists for λ > 0: XᵀX has eigenvalues of at least 0, and adding λI lifts each by λ, so the matrix is positive definite even when two columns of X are identical and OLS fails. In singular value decomposition terms, ridge scales the fit along each singular direction by d²/(d² + λ), so the weakest directions shrink most. Lasso has no such formula, because |w| has a corner at 0.
If eigenvalues or matrix gradients feel shaky, the Fermi Concept Library gives every concept in mathematics and computing its own lesson and its place in the map.
Why does lasso set coefficients exactly to zero?
Each penalty matches a constraint: minimise the error subject to ‖w‖₁ ≤ t for lasso, or ‖w‖² ≤ t² for ridge. λ and t are linked by a Lagrange multiplier, as in our guide on how to use Lagrange multipliers.
Picture two weights. The error has elliptical contours around the OLS point. Grow an ellipse until it first touches the allowed region:
The ridge region is a disc with no corners, so the first touch is almost always where both weights are non-zero.
The lasso region is a diamond with corners on the axes. The ellipse often meets a corner first, and at a corner one weight is exactly 0.
The diagram at the top shows both for one error function. In more dimensions the L1 ball has corners on the axes and edges in the coordinate planes, which is why lasso can switch off several inputs at once.
A worked example: shrinkage with one variable
Take the points (1, 2) and (3, 6) and fit y = wx. You need Σx² = 1 + 9 = 10 and Σxy = 2 + 18 = 20, so OLS gives w = 20/10 = 2.
Ridge. Minimise Σ(y − wx)² + λw². Differentiating gives −2Σxy + 2wΣx² + 2λw = 0, so w = 20/(10 + λ).
λ = 10 gives w = 1.
λ = 20 gives w = 0.667.
λ = 100 gives w = 0.182.
The weight shrinks but never reaches 0.
Lasso. Minimise Σ(y − wx)² + λ|w|. For w > 0, differentiating gives −2Σxy + 2wΣx² + λ = 0, so w = (20 − λ/2)/10. At w = 0 the error falls at rate 2Σxy = 40 as w grows, and the kink in λ|w| holds w at 0 only if λ ≥ 40.
λ = 20 gives w = 1.
λ = 30 gives w = 0.5.
λ ≥ 40 gives w = 0 exactly.
The general rule is soft-thresholding: w = sign(Σxy) × max(|Σxy| − λ/2, 0)/Σx².
How do you choose λ?
Too small and you are back to overfitting; too large and every weight is squashed. A checklist:
Standardise each input to mean 0 and standard deviation 1, since the penalty treats all weights alike. Leave the intercept unpenalised.
Choose a grid of λ values spread evenly on a log scale.
Run k-fold cross-validation: fit on all folds but one, measure error on the held-out fold, rotate and average.
Pick the λ with the lowest average error, refit on all training data, and report error on a test set that played no part.
In scikit-learn, RidgeCV and LassoCV choose the penalty by cross-validation. Its lasso divides the squared error by 2n (scikit-learn: lasso), so compare λ values only within one convention.
Check yourself by redoing the example above with λ = 50: ridge gives 0.333, lasso gives exactly 0. For more practice, the Fermi Problem Library has worked problems with a step-by-step solver that explains the reasoning rather than printing an answer.



