Sub topic 10
Regularisation: penalising complexity
This chapter assumes you know what a loss function is, how linear regression fits parameters by minimising it, and the scikit-learn fit and predict workflow. It also assumes you can tell overfitting from underfitting, as covered in the earlier chapters.
The core idea
An overfit model usually has large, finely tuned coefficients. It bends to follow noise in the training set. Regularisation discourages this by changing what the model minimises. Instead of only the data loss, it minimises the data loss plus a penalty that grows with the size of the weights:
total loss = data loss + λ × penalty(weights)
The model now has to trade fit against simplicity. A weight can only grow large if it reduces the data loss by more than the penalty costs.
The strength λ
λ (called alpha in scikit-learn's Ridge and Lasso) controls the balance.
| λ | Effect on the model | Risk |
|---|---|---|
| Near 0 | Almost no penalty, so the model behaves as before | Overfitting remains |
| Moderate | Weights shrink, variance falls, bias rises slightly | Usually the best validation error |
| Very large | Weights are forced close to 0 | Underfitting |
So regularisation moves a flexible model along the bias-variance trade-off. A higher λ gives more bias and less variance. The right value is found by validation, which a later chapter covers.
A worked example
The function below computes the penalised loss for a linear model. The penalty is the sum of squared weights, which is the L2 form.
import numpy as np
def penalised_mse(w, X, y, lam):
residual = X @ w - y
data_loss = np.mean(residual ** 2)
penalty = np.sum(w ** 2)
return data_loss + lam * penalty
rng = np.random.default_rng(0)
X = rng.normal(size=(20, 3))
y = X @ np.array([1.0, 0.0, 0.0]) + rng.normal(0, 0.1, size=20)
small_w = np.array([1.0, 0.0, 0.0])
large_w = np.array([1.0, 5.0, -5.0])
for lam in (0.0, 0.1):
print(lam, round(penalised_mse(small_w, X, y, lam), 3),
round(penalised_mse(large_w, X, y, lam), 3))With λ = 0 the two weight vectors are judged only on fit. With λ = 0.1 the large weights add a penalty of 5.1, so the optimiser prefers the small ones unless the larger weights improve the fit enough to pay for it.
Common errors
- Penalising the intercept. Libraries such as scikit-learn leave the bias term out of the penalty. If you write your own, do the same. Otherwise predictions are pulled toward zero for no good reason.
- Skipping feature scaling. The penalty treats all weights alike, but a feature measured in small units needs a large weight. Unscaled features get penalised unevenly. The next chapter covers this.
- Tuning λ on the test set. Choose λ with a validation set or cross-validation, and keep the test set for the final check.
- Reading a low training score as a bug. A regularised model often has slightly higher training error than an unregularised one. Judge it by validation error.
Exercise
A ridge model has training error 0.05 and validation error 0.40. You raise λ and get training error 0.12 and validation error 0.18. Explain what happened. If you kept raising λ until both errors were 0.60, what would that tell you?
Show answer
The first model overfit, with low bias and high variance. A larger λ shrank the weights, which raised training error a little and cut validation error a lot, so generalisation improved. If both errors reach 0.60, λ is too large and the model now underfits: the penalty is overwhelming the data loss, so bias is high.