# Chapter 5: Regularization

> Keeping models simple with ridge and lasso penalties. Opening question: Can a flexible model be stopped from overfitting without making it less flexible?

From *The Book of Machine Learning*, a free interactive machine learning textbook. Interactive version, with figures to use: https://thebookofml.com/regularization/

[The last chapter](https://thebookofml.com/generalization/) ended with a choice: pick a model simple enough not to overfit. That works when "simple" is a dial you can turn, like the degree of a curve. Often it is not. A model with a hundred features has a hundred weights, and you cannot know in advance which ones to remove.

This chapter takes a different route. Keep the flexible model, and change what it is rewarded for.

## What overfitting looks like inside

Go back to the degree-9 curve that swung wildly between the students. Here are a few of its weights, next to those of the well-behaved degree-2 curve.

| Model | Largest weight | Error on fitted students | Error on new students |
|---|---|---|---|
| Degree 2 | 7.4 | 11.9 | 28.8 |
| Degree 9 | 442 | 5.2 | 192 |

The overfitted model has enormous weights. That is not a coincidence. To bend sharply enough to pass near every dot, the curve needs huge positive weights and huge negative ones that almost cancel each other. Small changes in the input then cause huge changes in the output.

**Note:** The features here have been put on a common scale first, so the sizes of the weights can be compared fairly. More on that at the end of the chapter.

So large weights are a symptom of overfitting. The idea of regularization is to treat the symptom directly: make large weights cost something.

## A price on large weights

Until now training has minimised one thing, the mean squared error. Add a second term that grows with the size of the weights:

$$
\text{loss} = \text{MSE} + \lambda \sum_{j} w_j^{\,2}
$$

The model now has two aims that pull against each other. It still wants to fit the data, but every bit of weight has to earn its place by reducing the error more than it adds to the penalty. The number λ sets the exchange rate between the two.

- With $\lambda = 0$ there is no penalty, and we are back to plain least squares.
- With a huge $\lambda$ the penalty is all that matters. Every weight is driven to zero and the model predicts the average score for everyone.

Least squares with this squared penalty is called ridge regression. The bias $b$ is left out of the sum: it only moves the curve up and down, which is not the kind of flexibility we are trying to limit.

> **Interactive figure: Taming the degree-9 curve.** The same nine-bend model as before, on the same twelve students. Hollow blue dots are the new students.
> Try it: add the penalty, then drag λ from the far left to the far right. Watch the largest weight, and find where the error on new students is lowest.
> (Use it at https://thebookofml.com/regularization/)

With no penalty the largest weight is 442 and the error on new students is 192. A penalty of only $\lambda = 0.003$ brings that error down to about 36, close to what the degree-2 model achieved, and the curve stops lurching. Push $\lambda$ too far and the curve flattens toward a horizontal line: the model is now underfitting.

**Check your understanding:** You increase λ in ridge regression. What happens to the error on the training data?

- It goes down — The penalty stops the model fitting the training data as closely as it could, so training error cannot improve.
- It goes up or stays the same **(correct)** — The model gives up some fit on the training data in exchange for smaller weights. The hope is that error on new data falls.
- It is unaffected — The weights change with λ, so the fit to the training data changes too.

## A different penalty: lasso

Ridge penalises the *squares* of the weights. Another choice is to penalise their *absolute values*:

$$
\text{loss} = \text{MSE} + \lambda \sum_{j} \lvert w_j \rvert
$$

This is called the lasso. The change looks minor and has a striking effect.

Squaring a small number makes it much smaller: $0.1^2 = 0.01$. So under ridge, a weight that is already small costs almost nothing, and there is no pressure to remove it entirely. Ridge shrinks every weight and almost never makes one exactly zero.

The absolute value does not have that property. A weight of $0.1$ costs $0.1$. Each weight is pushed toward zero with the same force however small it gets, and many of them arrive there. A weight of exactly zero means the model ignores that feature completely.

> **Interactive figure: Shrinking against switching off.** Each bar is one weight of the degree-9 model. Feature 1 is x, feature 2 is x squared, and so on.
> Try it: with Ridge selected, drag λ upward and watch all the bars shrink together. Switch to Lasso and do the same, counting the orange rings.
> (Use it at https://thebookofml.com/regularization/)

A model in which most weights are zero is called sparse. The lasso has in effect chosen which features to keep, a job known as feature selection. At $\lambda = 1$ it keeps four of the nine features and its error on new students is about 40, far below the 192 it started from.

| | Ridge | Lasso |
|---|---|---|
| Penalty on each weight | its square | its absolute value |
| Effect | shrinks all weights | sets some weights to exactly zero |
| Good when | most features matter a little | only a few features matter |
| Result | every feature still used | a shorter, more readable model |

## Choosing the strength

The penalty strength $\lambda$ is a hyperparameter, like the degree in [the last chapter](https://thebookofml.com/generalization/), and it is chosen the same way. Try a range of values, score each by cross-validation on the training data, and keep the best. Because useful values span several powers of ten, the values tried are spaced by multiplication: 0.001, 0.01, 0.1, 1, 10.

> **Interactive figure: Finding λ without the test set.** The orange curve is the cross-validation error, computed from the twelve fitted students only. The blue curve is the error on the twenty new students, shown for comparison. The λ axis goes up in powers of ten.
> Try it: drag λ to the lowest point of the orange curve and read the error on new students. Then switch between ridge and lasso.
> (Use it at https://thebookofml.com/regularization/)

The orange curve is the same U shape as before. Too little penalty overfits. Too much underfits. The lowest point of the curve is the value to use.

How good is that choice? Switch between the two penalties and compare with the blue curve.

| Penalty | λ chosen by cross-validation | Error on new students there | Best possible on new students |
|---|---|---|---|
| Lasso | 0.063 | 37.4 | 36.1 |
| Ridge | 0.25 | 55.5 | 35.9 |

For the lasso, cross-validation lands almost exactly on the best value. For ridge it asks for more penalty than the new students would have wanted. Both are far better than the 192 of the unpenalised model, but the ridge result is a reminder that cross-validation is an estimate. With only twelve students each round holds out two or three, and a score built from so few is noisy. With more data the two curves agree much more closely.

**Check your understanding:** A lasso model trained on 200 features ends up with 12 non-zero weights. What has happened?

- Training failed for the other 188 — Nothing failed. Weights of exactly zero are what the lasso penalty is designed to produce.
- The model decided 188 features were not worth their cost **(correct)** — Each non-zero weight has to reduce the error by more than it adds to the penalty. Only 12 did.
- λ was set to zero — With λ = 0 there is no penalty and nearly all 200 weights would be non-zero.

**Common mistake: Reporting the penalised loss**

During training the model minimises error plus penalty. When you report how good the model is, report the error alone. The penalty is a tool for steering training and says nothing about how far off the predictions are. Comparing two models by their penalised losses is doubly misleading when they use different values of λ, because the same weights would then get different scores.

## In code

In scikit-learn the penalty strength is called `alpha`. The `RidgeCV` and `LassoCV` classes run the cross-validation for you.

```python
import numpy as np
from sklearn.linear_model import LassoCV, RidgeCV
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import PolynomialFeatures, StandardScaler

alphas = np.logspace(-3, 1.5, 30)              # 30 values from 0.001 to about 32

def penalised(final_step):
    # Scaling comes before the penalty, so every feature is charged fairly.
    return make_pipeline(PolynomialFeatures(9, include_bias=False), StandardScaler(), final_step)

ridge = penalised(RidgeCV(alphas=alphas, cv=5)).fit(X_train, y_train)
lasso = penalised(LassoCV(alphas=alphas, cv=5, max_iter=100_000)).fit(X_train, y_train)

print(ridge[-1].alpha_, lasso[-1].alpha_)      # the strengths chosen by cross-validation
print(np.sum(lasso[-1].coef_ == 0))            # how many features the lasso switched off
print(ridge.score(X_test, y_test), lasso.score(X_test, y_test))   # R² on the test set
```

Libraries scale the penalty slightly differently from one another, so the best `alpha` here will not equal the best $\lambda$ in the figures. The behaviour is the same.

## Key takeaways

- Overfitted models have **very large weights**. Regularization makes large weights cost something.
- The new loss is the error **plus λ times the size of the weights**. λ sets how much simplicity is worth.
- **Ridge** (squared weights) shrinks all weights smoothly. **Lasso** (absolute weights) sets some to exactly zero.
- Too little penalty overfits, too much underfits. **Choose λ by cross-validation.**
- Put features on a **common scale** before penalising, or the penalty treats them unfairly.

## What you should now be able to do

- Explain why large weights are a sign of overfitting.
- Write down the ridge and lasso losses and say how they differ.
- Predict what happens to the weights, the training error and the error on new data as λ grows.
- Choose λ without touching the test set.

## Exercises

### Exercise (by hand)

Two models have the same mean squared error, 10. Model A has weights $[2,\ 2]$ and model B has weights $[0,\ 3]$. With $\lambda = 1$, which model does ridge prefer, and which does lasso prefer?

**Hint:**

Ridge adds the sum of squared weights to the error. Lasso adds the sum of their sizes.

**Solution:**

Ridge adds the squared weights. A: $4 + 4 = 8$, a loss of 18. B: $0 + 9 = 9$, a loss of 19. **Ridge prefers A.**

Lasso adds the sizes of the weights. A: $2 + 2 = 4$, a loss of 14. B: $0 + 3 = 3$, a loss of 13. **Lasso prefers B.**

Squaring punishes one large weight more than two medium ones, so ridge likes the weight spread out. Lasso counts every unit of weight the same, so it is happy to put everything on one feature and switch the other off.

### Exercise (by hand)

A model has a single weight. Without a penalty the best value is 4, and the loss is $(w - 4)^2$. Add a ridge penalty, giving $(w - 4)^2 + \lambda w^2$. Find the best $w$ for $\lambda = 0$, 1 and 3. Does it ever reach zero?

**Hint:**

Set the derivative to zero: $2(w - 4) + 2\lambda w = 0$, and solve for $w$.

**Solution:**

Set the derivative to zero: $2(w - 4) + 2\lambda w = 0$, which gives $w = 4 / (1 + \lambda)$.

- $\lambda = 0$: $w = 4$
- $\lambda = 1$: $w = 2$
- $\lambda = 3$: $w = 1$

The weight shrinks towards zero as λ grows, and it never gets there. However large λ is, $4 / (1 + \lambda)$ is still positive. Ridge shrinks weights and does not remove them.

### Exercise (by hand)

Now use a lasso penalty on the same model: $(w - 4)^2 + \lambda w$, for $w$ at or above zero. Find the best $w$ for $\lambda = 2$, 8 and 10.

**Hint:**

Set the derivative to zero as before. If the answer is negative, the best allowed value is 0.

**Solution:**

Set the derivative to zero: $2(w - 4) + \lambda = 0$, which gives $w = 4 - \lambda / 2$.

- $\lambda = 2$: $w = 3$
- $\lambda = 8$: $w = 0$
- $\lambda = 10$: the formula gives $-1$, which is outside the allowed range. The lowest point within the range is $w = 0$.

The weight falls in a straight line and hits zero at $\lambda = 8$. From there on it stays at exactly zero. This is why lasso can switch features off and ridge cannot.

### Exercise (think)

You fit a ridge model for a range of λ values, from very small to very large. Sketch, in words, what happens to the training error and to the validation error along the way. Where is the best λ?

**Hint:**

At $\lambda = 0$ the model fits the training data as closely as it can. Think about what each increase takes away.

**Solution:**

**Training error rises all the way.** With no penalty the model fits the training data as closely as it can. Every increase in λ pulls the weights away from that best fit, so the training error can only get worse.

**Validation error falls, then rises.** At very small λ the model overfits and validation error is high. As λ grows the weights calm down and validation error drops. Past a certain point the model is too restrained to follow the real pattern, and validation error climbs again.

The best λ is at the bottom of the validation curve. The training curve cannot tell you where that is, because it is lowest at $\lambda = 0$.

### Exercise (with the figure)

In the figure "Shrinking against switching off", raise λ slowly with ridge selected, then again with lasso. How does the way the bars shrink differ? At $\lambda = 1$ under lasso, how many features are still in use?

**Hint:**

Look for bars that reach exactly zero, and count the ones that remain.

**Solution:**

Under ridge all the bars shrink together, each in proportion to its size, and none of them ever reaches zero. Under lasso the bars drop one at a time to exactly zero and stay there.

At $\lambda = 1$ the lasso keeps four of the nine features. It has chosen a simpler model for you, which ridge never does.

## Going further

### Why scaling comes first

A weight's size depends on its feature's units. Measure sleep in minutes instead of hours and the same model needs a weight sixty times smaller. A penalty on weight size would then punish the two versions differently, though they make identical predictions. To standardize each feature first, giving it an average of 0 and a spread of 1, removes the problem. The scaling must be learned from the training data only.

### Both at once

The elastic net uses both penalties together, with a second setting for the mix between them. It keeps the lasso's ability to switch features off while behaving better when several features carry nearly the same information.

### Other ways to regularize

A penalty on weights is one form of regularization among many. Stopping gradient descent early, before it has fully fitted the training data, has a similar effect. Neural networks add their own methods, such as dropout, which have [a chapter](https://thebookofml.com/keeping-networks-honest/) later in the book.

## References

- G. James, D. Witten, T. Hastie and R. Tibshirani, *An Introduction to Statistical Learning*, chapter 6. Free at [statlearning.com](https://www.statlearning.com).
- T. Hastie, R. Tibshirani and J. Friedman, *The Elements of Statistical Learning*, chapter 3, [hastie.su.domains/ElemStatLearn](https://hastie.su.domains/ElemStatLearn/).
- H. Kinsley and D. Kukieła, *Neural Networks from Scratch in Python*, chapter 14, [nnfs.io](https://nnfs.io).

## Next

That completes the core of the book: fit a model, train it step by step, check it on unseen data, and restrain it when it overfits. Everything so far predicted a number. Part II turns to predicting a category, starting with the method that changes a line as little as possible to do it.
