Skip to content
Lambdian

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: y^=w0+w1x+w2x2+⋯+wdxd\hat{y} = w_0 + w_1x + w_2x^2 + \dots + w_dx^d.

The trick is that this is still linear regression. Create new features x2,x3,…x^2, x^3, \dots 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 dd controls how flexible the model is. Too little and it underfits; too much and it memorises noise. Choosing dd 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 nn training points, a polynomial of degree n−1n-1 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 kk-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 xx is around 1000, x9x^9 is around 102710^{27} and the least-squares solve becomes numerically unstable. Centre and scale xx, or use orthogonal polynomials.
  • Never extrapolate. Outside the training range a high-degree polynomial shoots off to ±∞\pm\infty.
  • Many inputs explode. With pp inputs and degree dd there are (p+dd)\binom{p+d}{d} features. Splines, kernels and trees are usually better for many inputs.

The algorithm, step by step

  1. 1
    Scale the input: u=(x−μ)/su = (x - \mu)/s.
  2. 2
    Build the design matrix Φ\Phi with columns 1,u,u2,…,ud1, u, u^2, \dots, u^d.
  3. 3
    For each candidate degree, fit w\mathbf{w} by least squares on the training split.
  4. 4
    Keep the degree with the lowest validation (or cross-validated) error; refit on train + validation; report test error once.

The maths

Feature map

ϕ(x)=(1,x,x2,…,xd)⊤\phi(x) = (1, x, x^2, \dots, x^d)^\top

Model

y^=w⊤ϕ(x)\hat{y} = \mathbf{w}^\top\phi(x)

Least squares on the expanded features

w∗=arg⁡min⁡w∥y−Φw∥2=(Φ⊤Φ)−1Φ⊤y\mathbf{w}^\ast = \arg\min_{\mathbf{w}} \lVert \mathbf{y} - \Phi\mathbf{w} \rVert^2 = (\Phi^\top\Phi)^{-1}\Phi^\top\mathbf{y}

k-fold cross-validation error

CV(d)=1k∑j=1kMSE(fold j; wd(−j))\mathrm{CV}(d) = \frac{1}{k}\sum_{j=1}^{k} \mathrm{MSE}\big(\text{fold } j;\ \mathbf{w}_{d}^{(-j)}\big)
wd(−j)\mathbf{w}_d^{(-j)} is fitted on every fold except jj.

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…

There is one or a few inputs with a smooth, curved relationship to the target, and you can validate the degree.

Check your understanding

Question 1

Q1Why is polynomial regression still called a linear model?

Question 2

Q2Training error keeps falling as the degree grows. What should you use to pick the degree?

Question 3

Q3A degree-12 fit looks perfect on 13 training points. The most likely problem is…

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 x0,x1,…,xdx^0, x^1, \dots, x^d.
  • 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.
Open the labPython + NumPy + scikit-learn runs in your browser. Sign-in keeps your code and attempts.