L1 vs L2 Regularization: Why Lasso Deletes Features and Ridge Only Shrinks Them
Both penalties punish large weights. One of them drives weights to exactly zero and the other almost never does, and the whole difference comes down to how hard each one pushes on a weight that is already small.
A model with enough freedom will fit anything, including the noise in your training data. Regularization is the standard defense: add a term to the loss that makes large weights expensive, so the model uses them only when the data insists.
There are two classic ways to measure “large”, and they go by more names than they deserve. Penalize the sum of squared weights and you have L2 regularization, called ridge in regression and weight decay in neural networks. Penalize the sum of absolute values and you have L1 regularization, also known as the lasso. They sound like minor variations of each other. They behave very differently, and the reason fits in one picture.
The comparison at a glance
| L2 (ridge) | L1 (lasso) | |
|---|---|---|
| Penalty added to the loss | ||
| Pull on each weight | Proportional to the weight: strong on big weights, faint on small ones | The same on every weight, however small |
| Effect | Shrinks the weights overall; almost none reach zero | Shrinks the weights overall and sets many to exactly zero |
| Resulting model | Dense: every feature keeps some weight | Sparse: a subset of the features, the rest removed |
| Correlated features | Shares the weight among them | Tends to keep one and drop the others |
| Solving it | A closed-form solution for linear regression | No formula in general; solved by iteration |
| Reach for it when | Many features each matter a little | You expect few features to matter, or want a smaller model |
What regularization is for
Suppose you fit a linear model with 200 features to 150 examples. There are more knobs than data points, so the model can match the training set perfectly, and it will often do so by giving some features huge positive weights and others huge negative ones that happen to cancel on the examples it has seen. On new data they don’t cancel, and the predictions swing wildly.
Regularized training minimizes a different quantity:
The number sets the exchange rate. At zero you have the unregularized model. As it grows, the weights are squeezed toward zero and the model becomes simpler and steadier, at the cost of fitting the training data a little worse. In the language of the bias–variance trade-off, you are buying a large drop in variance with a small rise in bias. Make too large and the model can’t fit anything at all.
L1 and L2 differ only in what “size of the weights” means.
Why L1 produces exact zeros
Strip the problem down to a single weight. Say that the data, left alone, would like this weight to be , and that moving away from costs . Add each penalty in turn and find the best .
With the L2 penalty , the best weight is
Every weight is divided by the same factor. A big weight loses a lot, a small one loses a little, and nothing reaches zero unless it started there.
With the L1 penalty , the best weight is
Every weight moves toward zero by the same fixed amount , and any weight that started within of zero is set to zero and stays there. This rule is called soft thresholding.
Put numbers in. Take and two features: one the data cares about, with , and one it barely notices, with .
| No penalty | L2 | L1 | |
|---|---|---|---|
| Strong feature | 3.0 | 2.0 | 2.5 |
| Weak feature | 0.4 | 0.27 | 0 |
L2 kept both features and shrank each by a third. L1 kept the strong one almost intact and deleted the weak one.
The reason is the slope of each penalty near zero. The slope of is , which fades to nothing as shrinks: the closer a weight gets to zero, the more gently L2 pushes, so it never quite arrives. The slope of is 1 no matter how small is. L1 pushes just as hard on a weight of 0.001 as on a weight of 100, and if the data’s pull in the other direction is weaker than that push, the weight goes all the way to zero.
The picture: a circle and a diamond
There is a geometric way to see the same thing, and it is the picture in every textbook. Penalizing the weights is equivalent to giving them a budget. The solution has to lie inside a region around the origin, and it will be the point of that region where the training error is lowest.
For L2 with two weights, the region is a disc. The contours of the training error first touch a disc somewhere on its smooth edge, at a point where both weights are almost always non-zero.
For L1, the region is a diamond with its four corners on the axes. Corners stick out, so for a whole range of problems the first point of the diamond the contours reach is a corner, and at a corner one of the weights is exactly zero. A disc has no corners, so with L2 that happens only by coincidence. In higher dimensions the diamond has many more corners and edges, and on every one of them some weights are exactly zero.
You can watch this happen in our lesson on Regularization: Ridge & Lasso: raise in the simulation until the lasso solution lands on a corner of the diamond, and one weight drops to zero.
How they treat correlated features
Real features overlap. A house’s floor area in square feet and in square meters is the extreme case, and the number of rooms is not far behind.
Ridge handles this gracefully. Given two nearly identical features, it splits the weight between them, because under a squared penalty two weights of 5 cost less than one weight of 10 (50 against 100). The result is stable: collect the data again and you get much the same answer.
The lasso has no such preference. Under an absolute-value penalty, 5 + 5 costs exactly the same as 10 + 0, so it tends to keep one of the pair and drop the other, and which one survives can flip with a small change in the data. If you are reading the surviving features as “the ones that matter”, this is a trap. A dropped feature may be just as informative as the twin that stayed.
The elastic net applies both penalties at once, to get sparse models that keep correlated features together. Hui Zou and Trevor Hastie introduced it in 2005 for this reason.
How to choose
- Start with L2 if you have no strong reason to do otherwise. It is stable, it has a closed-form solution for linear regression, and it rarely does harm.
- Use L1 when you believe most features are irrelevant, or when a smaller model is worth something in itself: cheaper to run, easier to explain, fewer measurements to collect. One limit to know: with more features than examples, the lasso keeps at most as many features as you have examples.
- Use the elastic net when you want a sparse model but your features come in correlated groups.
- Whichever you pick, choose by cross-validation, not by eye. Try values spread over several orders of magnitude.
Two practical rules apply to both. Standardize your features first. The penalty treats every weight alike, so a length measured in millimeters, whose weight is naturally tiny, gets off far more lightly than the same length measured in kilometers. Putting features on a common scale makes the penalty fair. And leave the intercept out of the penalty: there is no reason to shrink a model’s baseline toward zero. Most libraries do this for you.
The same idea in neural networks
In deep learning, L2 regularization usually goes by the name weight decay, because with plain gradient descent the two are the same thing: adding to the loss makes every update multiply each weight by a number slightly below one. With adaptive optimizers such as Adam they stop being equivalent, which is why AdamW applies the decay as a separate step of its own. Our deep learning course covers both in Weight Decay, Dropout & Early Stopping and Adaptive Optimizers.
L1 is rarer in deep networks, but it turns up wherever sparsity is the goal. Sparse autoencoders, for instance, classically use an L1 penalty on their activations to keep most of them at zero.
Common questions
Is L1 or L2 regularization better?
Neither wins in general. L2 tends to predict a little better when many features each carry some signal. L1 does better when only a few do, and it leaves you with a smaller model. If you can’t guess which world you are in, try both, or the elastic net, and compare them on data held back from training.
Why is it called the lasso?
The name is an acronym: least absolute shrinkage and selection operator. Robert Tibshirani introduced it in 1996. Ridge regression is older, from a 1970 paper by Arthur Hoerl and Robert Kennard.
What do “L1” and “L2” mean?
They are two ways of measuring the length of a vector. The L2 norm is ordinary straight-line length: the square root of the sum of squares. The L1 norm is the sum of absolute values, sometimes called taxicab distance because it is how far you drive on a grid of streets. Ridge penalizes the square of the L2 norm, and the lasso penalizes the L1 norm.
Where does the “prior” interpretation come from?
From maximum a posteriori estimation. If you assume before seeing any data that the weights were drawn from a bell curve centered on zero, the most probable weights given the data are the ridge solution. Assume a Laplace distribution instead, which has a sharp peak at zero and heavier tails, and you get the lasso.
Does regularization always help?
No. With plenty of data and few features there is little overfitting to prevent, and the best will be close to zero. Regularization earns its keep when data is scarce relative to the number of features, or when features are strongly correlated.
What is the regularization setting called in scikit-learn?
It depends on the model, which catches people out. Ridge and Lasso call it alpha, and larger means more regularization. LogisticRegression uses C, which is the inverse: smaller means more regularization.