Polynomial regression and model capacity
Fit curves by expanding features, and learn to pick the complexity that generalises.
Animated explanation
The idea
Many relationships bend: fuel use rises faster at high speed, crop yield peaks at some temperature. A straight line cannot follow them, but a polynomial can: .
The trick is that this is still linear regression. Create new features and the model is a weighted sum of them, so everything from the last lesson (least squares, the normal equation, gradient descent) works unchanged.
The real lesson is about capacity. The degree controls how flexible the model is. Too little and it underfits; too much and it memorises noise. Choosing is your first taste of model selection.
Underfitting and overfitting
An underfit model is too simple for the pattern: errors are high on training and new data. An overfit model is so flexible that it fits the noise in this particular sample: training error is tiny, but predictions between and beyond the points swing wildly.
With training points, a polynomial of degree passes through every one of them exactly. Zero training error, and usually terrible predictions. Training error alone can never tell you the right complexity.
Choosing the degree honestly
Split the data three ways. Fit each candidate degree on the training set, compare them on a validation set, and report the winner once on a test set you never touched while choosing.
With little data, use -fold cross-validation instead of a single validation set: every point gets to be validation data once, and the average error is a steadier guide.
Practical cautions
- Scale first. If is around 1000, is around and the least-squares solve becomes numerically unstable. Centre and scale , or use orthogonal polynomials.
- Never extrapolate. Outside the training range a high-degree polynomial shoots off to .
- Many inputs explode. With inputs and degree there are features. Splines, kernels and trees are usually better for many inputs.
The algorithm, step by step
- 1Scale the input: .
- 2Build the design matrix with columns .
- 3For each candidate degree, fit by least squares on the training split.
- 4Keep the degree with the lowest validation (or cross-validated) error; refit on train + validation; report test error once.
The maths
Feature map
Model
Least squares on the expanded features
k-fold cross-validation error
Strengths and weaknesses
Strengths
- Captures smooth curvature with the simplicity of linear least squares
- Closed-form fit, easy to explain
- A clean way to see the bias–variance trade-off
Weaknesses
- High degrees oscillate wildly and extrapolate terribly
- Numerically unstable without scaling
- Feature count explodes with many inputs
Reach for it when…
Check your understanding
Lambdian Lab
Pick the degree by validation
Fit polynomials of several degrees and choose one with a validation set.
- poly_features(x, d): an (n, d + 1) matrix with columns .
- fit_poly(x, y, d): least-squares weights for degree d.
- predict_poly(x, w): predictions (the degree is len(w) − 1).
- best_degree(x_tr, y_tr, x_va, y_va, degrees): the degree with the lowest validation MSE.