← All AI/ML articles

Polynomial regression, visualised.

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.

1. Bend the line2. Spot underfit & overfit3. Tame it with ridge
training points (drag, add, double-click to remove)test points (never used to fit)
Train MSE0
Test MSE0
Best degree0
Verdict-
Behind the scenes · 1

It is still linear regression

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.)

The learned weights
Behind the scenes · 2

Underfitting, just right, overfitting

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.


Cyan = train error, pink = test error. Very large values are clipped at the top. The white line marks your current degree.

Behind the scenes · 3

Taming a wild curve with ridge

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.

Experiment

Break it on purpose

Underfit it

A straight line on a wave. Both errors stay high: the model is too simple.

Overfit it

Degree 12 and only 8 points. The curve threads every dot and goes wild in between.

Rescue it with ridge

Keep degree 12 but add a penalty. The curve calms down and test error drops.

Check yourself

Three quick questions

Key takeaways

  • Polynomial regression is linear regression on extra columns: x, x², x³, …
  • Degree controls flexibility: too low underfits, too high overfits.
  • Training error always improves with degree, so judge a model on test error.
  • Huge weights and wild swings between points are the signs of overfitting.
  • Ridge (λ) shrinks the weights and smooths the curve; more data helps too.
← Back to all articles