A straight line cannot follow every pattern. Bend it, then find out what happens when you bend it too much, and how to calm it down.
The trick: build new input columns from x (x², x³, …) and fit an ordinary linear model on them. The curve bends, but the model is still a plain weighted sum, so the same least-squares maths works. (x is scaled to t = x ÷ 5 to keep the numbers manageable.)
Each degree is a different model. Training error only ever goes down as the curve gets more flexible. Test error, measured on points the model never saw, falls and then climbs back up: the curve has started memorising noise. Click the chart to try a degree.
Ridge regression adds a penalty on large weights: w = (XTX + λI)−1 XTy. A bigger λ pulls weights toward zero and smooths the curve. This table refits your current degree at every λ. Click a row to use it.
Too much penalty and the curve flattens into underfitting, so λ is another knob to tune using test error.
A straight line on a wave. Both errors stay high: the model is too simple.
Degree 12 and only 8 points. The curve threads every dot and goes wild in between.
Keep degree 12 but add a penalty. The curve calms down and test error drops.