Visual explainer
Ridge & Lasso Regularisation
Compare smooth ridge shrinkage with lasso thresholding through checked coefficients, constraint geometry, identical features and a visible underfitting limit.
Ridge and lasso add coefficient penalties to a regression fit. Ridge shrinks weights smoothly; lasso can set weights exactly to zero. The penalty trades training fit for smaller coefficients, so validation must establish whether that trade helps prediction.
Begin with two identical predictor columns, x₁ = x₂. Predictions depend only on w₁ + w₂. The dashed line contains coefficient pairs summing to 3: (4, −1) and (1.5, 1.5) therefore make identical predictions on these columns. The axes measure weights, not inputs or predictions. Large opposing coefficients can cancel; a large coefficient is not automatically evidence that a feature matters.
This is an exact nonidentifiability example. Nearly identical columns can instead make the fitted coefficients sensitive to small data changes. Ordinary least squares does not always produce large weights or interpolate noisy data.
Pay For Coefficient Size
Switch explicitly to a centered, orthogonal synthetic training fixture so the shrinkage arithmetic is checkable. The four feature rows are (1,1), (1,−1), (−1,1), (−1,−1), with targets 4.4, 3.6, −3.6, −4.4. There are n = 4 rows; XᵀX/n = I and Xᵀy/n = z = (4, 0.4). The unpenalized solution is z, with zero residuals. No intercept is needed for these centered data.
Use the following consistent convention throughout this page:
Here λ is the nonnegative penalty strength. For this orthogonal fixture, F(w) = ½‖w − z‖². At λ = 0.5, the unpenalized coefficients have ridge penalty 0.25(4² + 0.4²) = 4.04. The ridge solution instead has fit cost 0.897778 and penalty 1.795556, totaling 2.693333. The solid bar segment is fit cost, the dashed segment penalty; lengths share 80 SVG units per objective unit. The bracket shows the reduction in total objective despite a worse training fit.
The official linear-model objectives use different normalizations for Ridge and Lasso. Under this page's convention, scikit-learn Ridge alpha equals nλ, while Lasso alpha equals λ. Copying the same number across differently normalized objectives changes its meaning.
Shrink With Ridge
Dashed rings mark the old weights; solid rings mark the solutions. The lower ruler is explicitly magnified tenfold so the small coefficient's movement remains visible. The upper scale is 100 SVG units per weight unit; the lower is 1,000. These rulers are reused in the next frame.
Differentiate the ridge objective: (w − z) + λw = 0, so w = z/(1 + λ). At λ = 0.5:
This simple componentwise division requires the orthogonal normalization above. Generally the solution is (XᵀX + nλI)⁻¹Xᵀy, computed with a numerical solver rather than explicit matrix inversion. Ridge is not a feature-selection rule: it does not impose a threshold that creates zeros. A coefficient can still be zero through symmetry or the data.
Threshold With Lasso
With the same fixture and objective normalization, each lasso coordinate minimizes ½(wⱼ − zⱼ)² + λ|wⱼ|. Its solution is soft thresholding:
At λ = 0.5, (4, 0.4) becomes (3.5, 0). The large weight loses 0.5; the small one reaches the origin rather than crossing into negative values. The local slope of the absolute-value penalty changes at zero, allowing a whole interval of z values to map to zero. With correlated predictors, this one-shot formula for every coefficient no longer applies; fitting must account for their coupling. A zero coefficient alone does not prove the feature has no predictive or causal value.
Meet A Smooth Boundary
For this geometric detail, change the unpenalized solution to z = (2,1) and λ to 1, retaining a normalized orthogonal design. Both weight axes use the same scale of 65 SVG units per weight unit. The solid circle is the feasible set boundary ‖w‖² = 1.25. The dashed circle is a constant-fit-cost contour centered on z; it expands until it first meets the feasible set at (1,0.5).
The contact is calculated: the constraint radius is √1.25, and the distance from z to (1,0.5) is also √1.25. The two centers are 2√1.25 apart. This constrained solution also solves the penalized ridge objective for λ = 1. Smooth contact can occur off-axis; it does not prove every possible ridge solution has nonzero coordinates.
Meet An Axis Corner
Keep z = (2,1), λ = 1 and the previous equal axis scales. Replace the circular constraint by |w₁| + |w₂| ≤ 1. The nearest feasible point is the corner (1,0): soft thresholding gives precisely that pair. The fit contour through it has radius √2 around z, matching the drawn dashed circle.
Corners lie on axes, so contact can eliminate a coordinate. The radius values for the L1 and L2 constraints were chosen to match their respective penalized solutions; they are not equal budgets or evidence that one predicts better. Ridge can suit distributed signals; lasso can suit a sparse representation. These are modeling choices to evaluate on the actual data.
Identical Features Remain Ambiguous
Return to identical, equally scaled predictor columns, now normalized so xᵀx/n = 1 and xᵀy/n = 1. At λ = 0.5, let s = w₁ + w₂. For nonnegative weights the lasso objective, up to a constant, is ½(s − 1)² + 0.5s. It is minimized at s = 0.5. Every point on the mint segment is optimal: (0.5,0), (0.25,0.25), (0,0.5), and all intervening nonnegative pairs.
A solver can choose an endpoint depending on its algorithm or feature order; it need not choose randomly, and an interior pair is also valid. Predictions can remain stable while feature attribution is ambiguous. With the ridge objective at λ = 0.5, symmetry and positive curvature instead give w₁ = w₂ = 0.4. Adding a positive L2 term to lasso, as in elastic net, can remove this exact coefficient ambiguity, while still requiring validation. Centimeters and inches are proportional columns, not equally scaled identical columns until their units are handled.
Account For Feature Units
This controlled unit-change example uses one predictor. The same prediction 4 can be represented by x = 1, w = 4 or x = 100, w = 0.04. The upper bars share 3 SVG units per input unit; the lower bars share 75 per coefficient unit. The hundredfold changes are genuine, so the smallest bars become short ticks at mobile size; the numeric labels retain their values.
The equivalent L1 penalty size drops from 4 to 0.04, and the squared L2 size from 16 to 0.0016, even though predictions match. Penalties therefore depend on feature units. Fit any centering and scaling on training data alone and reuse those parameters on validation and test data. Penalty strengths, coefficient interpretations and intercept treatment must follow the selected representation.
Check Validation Predictions
Return to the four-row orthogonal training fixture z = (4,0.4). Define two fresh validation rows (1,0) and (0,1), with targets 3.5 and 0. These are synthetic values chosen to make the trade-off reproducible, not measured evidence that lasso generally beats ridge.
For lasso at λ = 0, 0.5 and 4, predictions on the two rows are respectively (4,0.4), (3.5,0) and (0,0). Validation MSE is therefore:
Bar heights use 30 SVG units per MSE unit. The middle ring is exactly zero, not an invisible positive bar. A real validation set needs enough representative examples; keep final test data outside model selection. The training penalized objectives are not validation metrics, and ridge/lasso objective totals cannot be compared directly as predictive scores.
Where It Breaks
At λ = 4, both lasso coefficients vanish. For the same fresh row (1,0), the prediction is 0 while the target is 3.5. The response ruler uses 60 SVG units per response unit; the bracket spans a 3.5-unit miss. Shrinkage has removed useful signal, producing underfitting rather than an automatically better model.
Ridge can also underfit under excessive regularization. Smaller coefficients and sparse models are tools for controlling sensitivity, not guarantees of correct predictions or reliable feature attribution. Choose the representation and penalty using representative validation evidence.
The Quick Version
- Identical predictors can hide very different coefficient pairs.
- Penalized fitting trades residual error against coefficient size.
- Ridge shrinks smoothly under the declared objective normalization.
- Lasso thresholding can create exact zeros.
- Smooth constraints can meet contours away from axes.
- L1 corners can put a coefficient on an axis.
- Identical features can leave lasso attribution nonunique.
- Feature units change the effective penalty.
- Validation evaluates predictions, not coefficient size alone.
- Excessive regularization can erase genuine predictive signal.