L1 or L2: which one sets weights to exactly zero, and why?
Question
A shop predicts basket value from three yes/no features. Eight orders, one for each combination:
| coupon | from email | weekend | basket ($) |
|---|---|---|---|
| 0 | 0 | 0 | 21 |
| 0 | 0 | 1 | 29 |
| 0 | 1 | 0 | 33 |
| 0 | 1 | 1 | 29 |
| 1 | 0 | 0 | 49 |
| 1 | 0 | 1 | 49 |
| 1 | 1 | 0 | 53 |
| 1 | 1 | 1 | 57 |
You scale the features, then fit Lasso (L1 penalty) and Ridge (L2 penalty) with alpha 0.5, 2 and 4. Without a penalty, the weights are 12, 3 and 1.
Which model sets some weights to exactly 0? Which weight goes first? Why does the other model never get there?
Show hints (3)
- "SIZE OF THE PULL"
For each weight, compare how hard the penalty pulls it toward 0. Does that pull get weaker as the weight gets small?
- "SLOPE OF EACH PENALTY"
The slope of
|w|is 1 for every positivew, however small. The slope ofw²is2w, so it fades to 0 aswshrinks. - "THIS DATA"
Here Lasso subtracts
alphafrom the size of each weight and stops at 0. The weekend weight starts at 1. What is left atalpha2?
Show checked explanation
Lasso (L1) sets weights to exactly 0. Ridge (L2) only shrinks them. The L1 pull toward 0 has the same force however small the weight is, so weak features drop out. The L2 pull shrinks with the weight and fades near 0, so a weight almost never lands on exactly 0.
import numpy as np from sklearn.linear_model import Lasso, Ridge from sklearn.preprocessing import StandardScaler # one order per row: used a coupon, came from email, weekend (1 = yes) X = np.array([[0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 0, 0], [1, 0, 1], [1, 1, 0], [1, 1, 1]]) y = np.array([21, 29, 33, 29, 49, 49, 53, 57]) # basket value, $ X = StandardScaler().fit_transform(X) # each column becomes -1 or +1 def show(weights): return " ".join(f"{w + 0.0:5.2f}" for w in weights) # + 0.0: no "-0.00" for alpha in [0.5, 2, 4]: l1 = Lasso(alpha=alpha).fit(X, y).coef_ l2 = Ridge(alpha=alpha).fit(X, y).coef_ print(f"alpha {alpha}: Lasso {show(l1)} | Ridge {show(l2)}")
alpha 0.5: Lasso 11.50 2.50 0.50 | Ridge 11.29 2.82 0.94 alpha 2: Lasso 10.00 1.00 0.00 | Ridge 9.60 2.40 0.80 alpha 4: Lasso 8.00 0.00 0.00 | Ridge 8.00 2.00 0.67
Code run on CPython 3.12 with scikit-learn 1.9 against the example above. Checked by an editor other than the author.
Walkthrough
The run gives these weights for coupon, email and weekend:
| alpha | Lasso | Ridge |
|---|---|---|
| 0.5 | 11.5, 2.5, 0.5 | 11.29, 2.82, 0.94 |
| 2 | 10, 1, 0 | 9.6, 2.4, 0.8 |
| 4 | 8, 0, 0 | 8, 2, 0.67 |
Lasso subtracts alpha from the size of each weight and stops at 0. At alpha 2, the weekend weight would be 1 − 2. That is below zero, so it becomes exactly 0. At 4, the email weight is gone too.
Ridge divides every weight by the same number: 1 + alpha / 8 here, where 8 is the number of rows. At alpha 4 that is 1.5, so the weekend weight 1 becomes 0.67. Dividing never gives 0.
These simple rules hold because the scaled columns are uncorrelated. With correlated features the numbers get messier, but the zeros come from the same place: the shape of the penalty.
- L1 adds
alpha × |w|. Its slope is the same for a big weight and a tiny one, so the pull keeps full force all the way to 0. If a feature helps the fit less than that pull, its weight stops at 0. - L2 adds a cost that grows with
w². Its slope,2w, shrinks aswshrinks. Near 0 the pull almost vanishes while the feature still helps a little, so the weight ends up small but not 0. Ridge gives an exact 0 only in a special case, such as a feature with exactly zero correlation with the target.
In the usual picture, the weights L1 allows form a diamond with corners on the axes. The best point often lands on a corner, where some weights are 0. The L2 circle has no corners.
Common mistakes
- Comparing the same
alphaacross the two models. In scikit-learn,Lassodivides the squared error by 2 × the number of rows, andRidgeuses the plain sum. Soalpha2 is a different strength in each. - Skipping feature scaling. A feature in large units needs only a small weight, so the penalty barely touches it. In small units, the same feature gets cut hard. Scale first, inside a
Pipeline. - Reading a zero weight as "this feature is useless". Lasso may have dropped it for a correlated feature that carries the same signal.
- Expecting Ridge to select features. It keeps every feature, only smaller. For a short list of features, use Lasso or Elastic Net.
Follow-up questions
- "Two features are near copies of each other. What do Lasso and Ridge do?" Ridge splits the weight about evenly between them. Lasso tends to keep one and set the other to 0, so its chosen features are unstable.
ElasticNetmixes both penalties: it still zeros weak features but keeps the copies together. - "How do you choose
alpha?" Try a range of values with cross-validation and keep the one with the best validation score.LassoCVandRidgeCVdo this for you. A largeralphagives a simpler model.
What a strong answer covers
Says L1 gives exact zeros and L2 only shrinks, and explains why with the slope of each penalty, not only the diamond. Scales features first. Knows that alpha means a different strength in Lasso and Ridge, and what happens with correlated features.
Open in interactive practice deck →