Skip to content
Glacius

Learn the math behind machine learning.

Machine learning

Ridge vs lasso: why one coefficient becomes zero

Follow the same two coefficients through ridge and lasso, derive the zero threshold, and see why correlated features complicate feature selection.

In this article
  1. Give both methods the same data
  2. Ridge divides; lasso subtracts until it reaches zero
  3. Reproduce the coefficients in Python
  4. A zero coefficient does not settle which feature matters
  5. Choose using validation, scaling, and the model you need
Two coefficient paths on the same scales. For orthogonal standardized features, unpenalized coefficients are 3 and 0.8. As lambda increases from 0 to 3, ridge gives z/(1+lambda), leaving both nonzero. Lasso gives sign(z) times max(abs(z)-lambda,0); the smaller coefficient becomes zero at 0.8 and the larger at 3. Solid blue is coefficient 1; dashed amber is coefficient 2.Two coefficient paths on the same scales. For orthogonal standardized features, unpenalized coefficients are 3 and 0.8. As lambda increases from 0 to 3, ridge gives z/(1+lambda), leaving both nonzero. Lasso gives sign(z) times max(abs(z)-lambda,0); the smaller coefficient becomes zero at 0.8 and the larger at 3. Solid blue is coefficient 1; dashed amber is coefficient 2.
The smaller lasso coefficient reaches zero first

Original four-row example with orthogonal, centered features of mean square one. Solid blue is b₁, initially 3; dashed amber is b₂, initially 0.8. Both panels use the same coefficient and λ scales. Markers show λ = 1. These formulas apply to this orthogonal design, not every regression dataset.

Calculated from ridge b = z/(1+λ) and lasso b = sign(z) max(|z|−λ, 0), with the objective conventions stated in the article.

A fitted linear model has two coefficients: 33 and 0.80.8. Add a ridge penalty and, in the example below, they become 1.51.5 and 0.40.4. Add a lasso penalty and they become 22 and 00. Both fits accept more training error in exchange for smaller coefficients. Only the second has removed a feature from its prediction.

The difference comes from the penalty near zero. Ridge penalizes squared coefficients, so its pull toward zero weakens as a coefficient gets smaller. Lasso penalizes absolute values and can make zero the exact optimum over a whole range of inputs. That is the basis of lasso's feature selection, although a selected feature is not necessarily the uniquely important one.

We can derive those four numbers from a dataset small enough to inspect, then change the dataset to see where the feature-selection interpretation breaks down. You will need linear regression and basic derivatives; the matrix notation only packages the four rows.

Give both methods the same data

Our synthetic dataset has four observations and two features. The prediction is y^=b1x1+b2x2\hat y=b_1x_1+b_2x_2.

Observationx1x_1x2x_2yy
A113.8
B1−12.2
C−11−2.2
D−1−1−3.8

These values were constructed as y=3x1+0.8x2y=3x_1+0.8x_2. There is no noise, and ordinary least squares recovers (3,0.8)(3,0.8) with zero training error. This is a calculation about penalties, not evidence about predictive performance on new data.

Both feature columns have mean zero and mean square one. Their dot product is zero: knowing one column does not help reconstruct the other through a linear combination. If XX is the matrix of feature values and n=4n=4, those properties give XTX/n=IX^TX/n=I. The response is also centered, so the fitted intercept is zero; we leave it out of the calculation.

Use the following objective conventions throughout:

Jridge(b)=12nyXb22+λ2jbj2,J_{\text{ridge}}(b) =\frac{1}{2n}\lVert y-Xb\rVert_2^2 +\frac{\lambda}{2}\sum_j b_j^2, Jlasso(b)=12nyXb22+λjbj.J_{\text{lasso}}(b) =\frac{1}{2n}\lVert y-Xb\rVert_2^2 +\lambda\sum_j |b_j|.

Here λ0\lambda\geq0 controls the penalty. The factor of 1/21/2 in the ridge penalty simplifies its derivative. These are standard forms, but libraries differ in how they scale the loss and name the penalty parameter. We will translate them into scikit-learn's alpha below.

Because our columns are orthogonal and scaled, the data-loss term simplifies to

12nyXb22=12(b13)2+12(b20.8)2.\frac{1}{2n}\lVert y-Xb\rVert_2^2 =\frac12(b_1-3)^2+\frac12(b_2-0.8)^2.

Each coefficient can now be optimized separately. This separation is a convenience of the example; it does not hold for arbitrary correlated features.

Ridge divides; lasso subtracts until it reaches zero

Write zz for one unpenalized coefficient, either 33 or 0.80.8. Its ridge objective is

12(bz)2+λ2b2.\frac12(b-z)^2+\frac{\lambda}{2}b^2.

Setting the derivative to zero gives bz+λb=0b-z+\lambda b=0, so

bridge=z1+λ.b_{\text{ridge}}=\frac{z}{1+\lambda}.

At λ=1\lambda=1, ridge halves both coefficients: (1.5,0.4)(1.5,0.4). For either nonzero zz in this example, any finite penalty leaves a nonzero result. If zz were already zero, ridge would return zero too. “Ridge never produces a zero coefficient” is therefore too strong; it has no lasso-like interval that thresholds small nonzero values to zero.

Lasso replaces the squared penalty with λb\lambda|b|. On the positive side, its derivative is bz+λb-z+\lambda, which suggests b=zλb=z-\lambda. That solution works only while it stays positive. On the negative side, the derivative is bzλb-z-\lambda, giving b=z+λb=z+\lambda while that value is negative.

At zero, look at the slopes from each side. The slope just to the left is zλ-z-\lambda; just to the right it is z+λ-z+\lambda. Zero minimizes this convex objective when the left slope is nonpositive and the right slope is nonnegative, which happens exactly when zλ|z|\leq\lambda. Combining the cases gives soft thresholding:

blasso=sign(z)max(zλ,0).b_{\text{lasso}} =\operatorname{sign}(z)\max(|z|-\lambda,0).

At λ=1\lambda=1, the coefficient 33 becomes 22, while 0.80.8 becomes zero. Lasso is solving a different optimization problem, rather than rounding a small ridge coefficient after fitting.

With lambda fixed at 1, the horizontal axis is unpenalized coefficient z and the vertical axis is fitted coefficient b. Ridge is the line b=z/2. Lasso has an exactly zero fitted coefficient throughout the interval from z=-1 to z=1; outside it, lasso subtracts one from the magnitude. A dashed guide marks z=0.8, where ridge is 0.4 and lasso is zero.With lambda fixed at 1, the horizontal axis is unpenalized coefficient z and the vertical axis is fitted coefficient b. Ridge is the line b=z/2. Lasso has an exactly zero fitted coefficient throughout the interval from z=-1 to z=1; outside it, lasso subtracts one from the magnitude. A dashed guide marks z=0.8, where ridge is 0.4 and lasso is zero.
Lasso has a whole interval that maps to zero

Hold λ = 1 and vary an unpenalized coefficient z in the orthogonal example. The solid ridge line crosses zero only at z = 0. The dashed lasso line stays at zero from −1 to 1. The paired markers show z = 0.8: ridge gives 0.4 and lasso gives zero.

Exact one-coordinate solutions to the objectives in the article; an illustrative calculation, not measured data.

The regularization paths in the opening figure follow these formulas as λ\lambda increases. Ridge's coefficients approach zero smoothly. Lasso's smaller coefficient reaches zero at λ=0.8\lambda=0.8, and its larger coefficient reaches zero at λ=3\lambda=3. These simple monotone paths depend on our orthogonal design; individual paths with correlated predictors can behave differently.

Reproduce the coefficients in Python

The scikit-learn Ridge objective is the sum of squared residuals plus alpha times the squared coefficient norm. Multiply our ridge objective by 2n2n and you get that form with alpha = n * lambda.

The scikit-learn Lasso objective already divides the squared residuals by 2n2n, so its alpha equals our λ\lambda. Using the same numerical alpha in both estimators would not reproduce the conventions above. Even after this conversion, a shared λ\lambda is an illustrative setting, not a claim that both models have equally strong regularization or equally good validation performance.

import numpy as np
from sklearn.linear_model import Ridge, Lasso

X = np.array([[1, 1], [1, -1], [-1, 1], [-1, -1]], dtype=float)
y = X @ np.array([3.0, 0.8])
n = len(y)
lam = 1.0

# X and y are already centered; feature mean squares are one.
ridge = Ridge(alpha=n * lam, fit_intercept=False, solver="svd")
lasso = Lasso(alpha=lam, fit_intercept=False, tol=1e-12,
              max_iter=10000)

for name, model in [("ridge", ridge), ("lasso", lasso)]:
    model.fit(X, y)
    mse = np.mean((y - model.predict(X)) ** 2)
    print(f"{name}: coefficients={model.coef_.round(4)}, mse={mse:.4f}")

The output is:

ridge: coefficients=[1.5 0.4], mse=2.4100
lasso: coefficients=[2. 0.], mse=1.6400

Neither training error beats the unpenalized fit, which is exact. Lasso's lower training error at this particular setting does not establish that lasso is the better predictor. Regularization deliberately changes the training objective; its value on unseen data must be checked separately.

A zero coefficient does not settle which feature matters

Now replace the two different columns with two copies of the same centered, unit-variance feature xx, and let y=3xy=3x. A prediction uses only the sum of the coefficients:

y^=b1x+b2x=(b1+b2)x.\hat y=b_1x+b_2x=(b_1+b_2)x.

At λ=1\lambda=1, the lasso objective becomes

12(3b1b2)2+b1+b2.\frac12(3-b_1-b_2)^2+|b_1|+|b_2|.

Fix a nonnegative total s=b1+b2s=b_1+b_2. Every nonnegative split of that total has the same penalty, b1+b2=s|b_1|+|b_2|=s. Minimizing 12(3s)2+s\frac12(3-s)^2+s gives s=2s=2. Thus (2,0)(2,0), (1,1)(1,1), and (0,2)(0,2) all minimize the same lasso objective and produce identical predictions. A solver can return a sparse answer, but the data have not established that one of the duplicate features deserves the credit.

For ridge, a fixed total ss has the smallest squared penalty when the coefficients share it equally: b1=b2=s/2b_1=b_2=s/2. Substitution gives 12(3s)2+s2/4\frac12(3-s)^2+s^2/4, whose minimum is also s=2s=2 at this setting. Ridge has the unique solution (1,1)(1,1).

Two identical centered feature columns x and response y=3x, with lambda=1. Every amber point on the segment b1+b2=2, from (0,2) to (2,0), is a lasso optimum giving the same prediction 2x. Ridge has the unique optimum (1,1), marked by a blue filled point on that segment. Equal units on both axes.Two identical centered feature columns x and response y=3x, with lambda=1. Every amber point on the segment b1+b2=2, from (0,2) to (2,0), is a lasso optimum giving the same prediction 2x. Ridge has the unique optimum (1,1), marked by a blue filled point on that segment. Equal units on both axes.
Identical predictions can have different selected features

Replace both feature columns by the same centered x with mean square one, and set y = 3x. At λ = 1, every nonnegative pair summing to 2 minimizes the lasso objective. Ridge has the unique solution (1, 1). The blue point is also a lasso optimum; lasso does not require a zero coefficient in this nonunique example.

Analytic duplicate-column example. Lasso objective is ½(3−b₁−b₂)²+|b₁|+|b₂|; ridge replaces that penalty with (b₁²+b₂²)/2.

Exact duplication makes the ambiguity visible without numerical noise. With strongly correlated but distinct features, coefficients and the selected subset can be sensitive to changes in the data; perfect duplication is not required for selection to deserve scrutiny. The scikit-learn linear-model guide describes elastic net as a combination of L1 and L2 penalties that is useful with correlated features. It offers another candidate to validate, rather than a guarantee of correct feature discovery.

Choose using validation, scaling, and the model you need

Ridge is a useful baseline when you want shrinkage while keeping contributions from many features. Lasso is worth testing when a sparse model has practical value, such as reducing the number of inputs you must collect. Compare them on validation data appropriate to the task, with a separately chosen penalty for each. A coefficient becoming zero is an optimization result; it is not a statistical significance test or proof of a causal relationship.

Scaling belongs inside that comparison. If a feature is multiplied by 100100, its coefficient can be divided by 100100 without changing the prediction. Its absolute-value penalty then becomes 100100 times smaller, and its squared penalty becomes 10,00010{,}000 times smaller. Leaving such unit differences in place changes which coefficients the penalty makes expensive. StandardScaler is a common way to put varying features on comparable scales, although the right treatment depends on what the inputs mean.

Fit that scaling on training data only. During cross-validation, put preprocessing and the estimator in a pipeline so each fold learns its own scaling; otherwise validation information can leak into training. Keep the test set for the final assessment. The scikit-learn guide to data leakage explains this workflow.

For a numerical look at preprocessing, our normalization and standardization comparison follows one column through both transformations and then transforms an unseen value using the fitted training statistics.

The useful question is whether the resulting predictions, retained inputs, and stability meet your needs. In our four-row example, every zero had an exact mathematical reason. With real data, the same penalty rules apply, but the evidence for choosing a model comes from how it performs beyond the rows that fitted it.

Learn with Glacius

Want to go deeper into the math?

Build your understanding with visual lessons and practice on the concepts from this article.

Check the reasoning

Sources & notes

  1. scikit-learn: Ridge

    Ridge objective, alpha convention, and intercept handling.

  2. scikit-learn: Lasso

    Lasso objective, alpha convention, coordinate descent, and numerical tolerance.

  3. scikit-learn: linear models

    Regularization, correlated predictors, elastic net, and model selection.

  4. scikit-learn: StandardScaler

    Feature scaling before penalized estimation.

  5. scikit-learn: avoiding data leakage

    Fit preprocessing on training data; use pipelines during cross-validation.

Our figures use illustrative mathematical examples unless a dataset is explicitly identified. You can share the original Glacius figures with attribution and a link to this article; linked third-party material retains its own terms.

Keep exploring

Lessons behind this article