2.2.6 · D4Linear & Logistic Regression

Exercises — Polynomial regression

2,855 words13 min readBack to topic

The single picture below is our running mental model for the whole page: curved data that a straight line cannot follow but a polynomial can. Every exercise is a variation on this theme — building the features, fitting them, and choosing how much curve is too much.

Figure — Polynomial regression

Notice the coral dots bending upward: a plain lavender line leaves big gaps, while the mint quadratic follows the bend. Keep this contrast in mind as you work through the levels.


Level 1 — Recognition

Recall Solution L1.1

Answer: (b).

  • (a) is degree 1 (a straight line) — highest power of is .
  • (b) has powers up to → degree 3. ✓
  • (c) uses , not powers of , so it is not a polynomial.
  • (d) has the parameter sitting in the exponent. That makes it non-linear in the parameters, so it is not linear/polynomial regression at all.

Key idea: "polynomial regression" = the model is a sum of powers of , each multiplied by a parameter. The powers can be as high as we like, but the parameters must appear only as plain multipliers.

Recall Solution L1.2

False. It uses the same algorithm. The word "linear" in linear regression means linear in the parameters , not linear in . We simply create new features and hand them to ordinary linear regression. The algorithm never knows those columns were built from powers.

Recall Solution L1.3

parameters: . The "" is the intercept (the constant term, which multiplies ).


Level 2 — Application

Recall Solution L2.1

Each row is . Row 1: . Row 2: . Row 3: . Three examples → three rows; degree 2 plus intercept → three columns.

Recall Solution L2.2

Substitute directly:

  • .
  • .
  • .
Recall Solution L2.3

The three columns differ by up to four orders of magnitude. In this stretched-out cost landscape, gradient descent is forced to use one step size for all directions: it takes tiny, painfully slow steps in the small-scale direction (the column, low curvature) while overshooting and zig-zagging in the large-scale direction (the column, high curvature). The result is slow, unstable convergence, and becomes ill-conditioned for the normal equation. Scaling with puts every column on mean , standard deviation , so all directions have comparable curvature and one step size works well for all of them.

Recall Solution L2.4

Mean: . Deviations: . Squared: ; sum ; divide by → variance ; . Standardised values:


Level 3 — Analysis

Recall Solution L3.1

Write and plug in each point: Subtract eq1 from eq2: . Subtract eq2 from eq3: . Subtract those two: . Back-substitute: . Then .

Result: . Check: ✓, ✓, ✓. With 3 parameters and 3 points, the fit passes through all of them exactly.

Recall Solution L3.2
  • (i) — the straight line is too rigid to bend to the pattern: underfitting (high bias).
  • (ii) — enough flexibility to catch the curve without chasing noise: the sweet spot.
  • (iii) — with parameters and points, the curve threads exactly through every point (including its noise): overfitting (high variance). See 2.3.02-Overfitting-and-regularization.

The number of free parameters () reaching the number of data points () is exactly when a model can memorise the data.

Figure — Polynomial regression
Recall Solution L3.3
  • Lavender straight lineunderfit. It ignores the visible curvature of the points; large gaps between line and dots mean high training error.
  • Mint smooth curvegood fit. It follows the overall bend of the points and passes near (not through) each one; it will generalise to new inputs.
  • Coral wiggly curveoverfit. It contorts to hit every single point exactly, including the noisy ones, and swings wildly between them — new points landing in those swings would be badly predicted.

The tell-tale sign: a fit that touches every point perfectly but oscillates violently between them is memorising, not learning.


Level 4 — Synthesis

Recall Solution L4.1

Design matrix and targets: Solving gives Verify: — all exact. (Even with 4 points and 3 parameters, the data happened to lie perfectly on , so least-squares error is zero.)

Recall Solution L4.2

Feature columns: 6 parameters ( through ). The term is the interaction: it lets the model express "the effect of depends on the level of " (e.g. rainfall and temperature together affecting crop yield).

Recall Solution L4.3

With degree there are feature columns but only rows. has more columns than rows, so its columns cannot all be independent — (a matrix) has rank at most and is therefore singular (no inverse). Even below that limit, un-scaled high powers make it near-singular. Two fixes:

  1. Reduce the degree so that (fewer parameters than points), removing the rank deficiency.
  2. Add regularization (ridge): solve . Here (Greek "lambda") is a small positive number we choose, called the regularization strength, and is the identity matrix — a matrix with 's down the diagonal and 's elsewhere, the matrix version of the number . Adding nudges every diagonal entry of upward, which makes the matrix invertible again and curbs overfitting — see 2.3.02-Overfitting-and-regularization. (Also scale features first, per 2.2.05-Feature-scaling.)

Level 5 — Mastery

Recall Solution L5.1
  1. Split the data into train / validation / test (2.4.01-Train-validation-test-split). The test set is locked away until the very end.
  2. For each degree : build polynomial features on the training set, scale each column (2.2.05-Feature-scaling) using the training mean/std (then apply the same to validation/test — never re-fit them).
  3. Train the model (normal equation or gradient descent) on the training set.
  4. Record and (the cost on the training and validation sets).
  5. Plot both curves against degree. Training error falls monotonically; validation error typically dips then rises (a U-shape) — the classic bias-variance signature.
  6. Choose the degree at the minimum of the validation curve — that is the sweet spot where the model is complex enough to catch the pattern but not so complex that it starts chasing noise.
  7. Report the final, honest performance by evaluating the chosen model once on the untouched test set. Because the test set played no part in choosing the degree, this number is an unbiased estimate of real-world performance.

This uses validation — not training error — as the judge, exactly because low training error can be pure memorisation.

Recall Solution L5.2

Choose . It has the lowest validation error (0.15).

  • : both errors high → the model is too simple, high bias / underfitting.
  • : training and validation errors are both low and close together → good balance, generalises.
  • : training error keeps dropping (0.05, 0.01) but validation error rises sharply (0.28, 0.60) → the model is fitting noise, high variance / overfitting.

The widening gap between and past degree 3 is the tell-tale overfitting signal. The figure below sketches this U-shape.

Figure — Polynomial regression
Recall Solution L5.3

Polynomial regression applies a fixed feature map that turns one input into several. The prediction is — a plain weighted sum of those transformed features. Because the parameters enter only as multipliers (never inside a power, sine, etc.), the cost is a quadratic bowl in with a single minimum, and setting gives the same normal equation . The curvature lives entirely in the fixed feature columns of ; the learning is still a linear least-squares problem. Change and you change the shape of the fit, without ever changing the algorithm.

Recall Self-Test (reveal after attempting)

Polynomial regression is linear in what? ::: In the parameters — not in . A degree- single-feature model has how many parameters? ::: . What single quantity should decide the degree? ::: The validation error (lowest wins). Why scale after making powers, not before? ::: So each engineered column () is standardised on its own scale, keeping well-conditioned.