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.
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.
(a) is degree 1 (a straight line) — highest power of x is 1.
(b) has powers up to x3 → degree 3. ✓
(c) uses sin, not powers of x, so it is not a polynomial.
(d) has the parameter θ1 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 x, 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 x. We simply create new features x1=x,x2=x2,x3=x3 and hand them to ordinary linear regression. The algorithm never knows those columns were built from powers.
Recall Solution L1.3
d+1 parameters:θ0,θ1,…,θd. The "+1" is the intercept θ0 (the constant term, which multiplies x0=1).
Each row is [1,x(i),(x(i))2].
Φ=111123149,y=236
Row 1: [1,1,12]. Row 2: [1,2,22]=[1,2,4]. Row 3: [1,3,32]=[1,3,9]. Three examples → three rows; degree 2 plus intercept → three columns.
Recall Solution L2.2
Substitute directly:
hθ(0)=2−3(0)+02=2.
hθ(1)=2−3(1)+12=2−3+1=0.
hθ(4)=2−3(4)+42=2−12+16=6.
Recall Solution L2.3
x∈[1,100]
x2∈[1,10000]
x3∈[1,1000000]
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 x column, low curvature) while overshooting and zig-zagging in the large-scale direction (the x3 column, high curvature). The result is slow, unstable convergence, and ΦTΦ becomes ill-conditioned for the normal equation. Scaling with xj←σjxj−μj puts every column on mean 0, standard deviation 1, so all directions have comparable curvature and one step size works well for all of them.
Recall Solution L2.4
Mean: μ=42+4+6+8=5.
Deviations: −3,−1,1,3. Squared: 9,1,1,9; sum =20; divide by m=4 → variance =5; σ=5≈2.236.
Standardised values:
Write hθ(x)=θ0+θ1x+θ2x2 and plug in each point:
θ0+θ1+θ2θ0+2θ1+4θ2θ0+3θ1+9θ2=2(x=1)=3(x=2)=6(x=3)
Subtract eq1 from eq2: θ1+3θ2=1.
Subtract eq2 from eq3: θ1+5θ2=3.
Subtract those two: 2θ2=2⇒θ2=1.
Back-substitute: θ1+3(1)=1⇒θ1=−2.
Then θ0−2+1=2⇒θ0=3.
Result:hθ(x)=3−2x+x2.
Check: h(1)=3−2+1=2 ✓, h(2)=3−4+4=3 ✓, h(3)=3−6+9=6 ✓. With 3 parameters and 3 points, the fit passes through all of them exactly.
Recall Solution L3.2
d=1 → (i) — the straight line is too rigid to bend to the pattern: underfitting (high bias).
d=2 → (ii) — enough flexibility to catch the curve without chasing noise: the sweet spot.
d=4 → (iii) — with d+1=5 parameters and m=5 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 (d+1) reaching the number of data points (m) is exactly when a model can memorise the data.
Recall Solution L3.3
Lavender straight line — underfit. It ignores the visible curvature of the points; large gaps between line and dots mean high training error.
Mint smooth curve — good fit. It follows the overall bend of the points and passes near (not through) each one; it will generalise to new inputs.
Coral wiggly curve — overfit. 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.
Design matrix and targets:
Φ=1111123414916,y=371321ΦTΦ=41030103010030100354,ΦTy=44140484
Solving ΦTΦθ=ΦTy gives
θ=111⇒hθ(x)=1+x+x2.
Verify: h(1)=3,h(2)=7,h(3)=13,h(4)=21 — all exact. (Even with 4 points and 3 parameters, the data happened to lie perfectly on x2+x+1, so least-squares error is zero.)
Recall Solution L4.2
hθ(x1,x2)=θ0+θ1x1+θ2x2+θ3x12+θ4x22+θ5x1x2
Feature columns: {1,x1,x2,x12,x22,x1x2} → 6 parameters (θ0 through θ5).
The term x1x2 is the interaction: it lets the model express "the effect of x1 depends on the level of x2" (e.g. rainfall and temperature together affecting crop yield).
Recall Solution L4.3
With degree 9 there are d+1=10 feature columns but only m=8 rows. Φ has more columns than rows, so its columns cannot all be independent — ΦTΦ (a 10×10 matrix) has rank at most 8 and is therefore singular (no inverse). Even below that limit, un-scaled high powers make it near-singular.
Two fixes:
Reduce the degree so that d+1≤m (fewer parameters than points), removing the rank deficiency.
Add regularization (ridge): solve θ=(ΦTΦ+λI)−1ΦTy. Here λ (Greek "lambda") is a small positive number we choose, called the regularization strength, and I is the identity matrix — a matrix with 1's down the diagonal and 0's elsewhere, the matrix version of the number 1. Adding λI nudges every diagonal entry of ΦTΦ 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.)
For each degree d: 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).
Train the model (normal equation or gradient descent) on the training set.
RecordJtrain and Jval (the cost on the training and validation sets).
Plot both curves against degree. Training error falls monotonically; validation error typically dips then rises (a U-shape) — the classic bias-variance signature.
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.
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 d=3. It has the lowest validation error (0.15).
d=1,2: both errors high → the model is too simple, high bias / underfitting.
d=3: training and validation errors are both low and close together → good balance, generalises.
d=4,5: 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 Jtrain and Jval past degree 3 is the tell-tale overfitting signal. The figure below sketches this U-shape.
Recall Solution L5.3
Polynomial regression applies a fixed feature mapϕ(x)=[1,x,x2,…,xd] that turns one input into several. The prediction is hθ(x)=θTϕ(x) — a plain weighted sum of those transformed features. Because the parameters θ enter only as multipliers (never inside a power, sine, etc.), the cost J(θ) is a quadratic bowl in θ with a single minimum, and setting ∇θJ=0 gives the same normal equation θ=(ΦTΦ)−1ΦTy. 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 x.
A degree-d single-feature model has how many parameters? ::: d+1.
What single quantity should decide the degree? ::: The validation error (lowest wins).
Why scale after making powers, not before? ::: So each engineered column (x,x2,x3) is standardised on its own scale, keeping ΦTΦ well-conditioned.