2.2.6 · D2Linear & Logistic Regression

Visual walkthrough — Polynomial regression

1,886 words9 min readBack to topic

Step 1 — The problem a straight line cannot solve

WHAT. We have a scatter of points. Each point is a pair: a horizontal position we call (the input) and a vertical position we call (the output we want to predict). A straight line is any rule of the form , where is the height where the line crosses the vertical axis and is its slope (how much climbs for each step right in ).

WHY. Before inventing a fancy tool, we must see why the simple one fails. Look at the figure: the dots bend upward, curving away from any straight line you could draw. No choice of (height) or (slope) can hug that curve — a line has only two knobs and both control straightness, neither controls bend.

PICTURE.

Figure — Polynomial regression

The red line is the best possible straight line, and it still misses the curved dots at both ends. We need a knob for curvature.


Step 2 — The one idea: invent a new axis that is

WHAT. We take each input and manufacture a brand-new number from it: (that is times itself). We now pretend this is a second, independent feature. So a point that used to be described by one number is now described by two numbers: .

WHY. Here is the whole trick. Our line-fitter only knows how to draw flat things. But a flat plane living in a higher-dimensional space can look curved when we shadow it back down. By adding the axis, we lift the curved data up into a space where it becomes straight again — and then the flat fitter can handle it.

PICTURE.

Figure — Polynomial regression

On the left: the curved data in the ordinary plane. On the right: the same points lifted onto the new axis — notice they now line up straight! The curvature was hidden in the direction all along.

  • The ::: a constant column, so the fit still has an intercept knob .
  • The ::: keeps the ordinary slope information.
  • The ::: the new knob — it lets the fit bend.

This manufacturing of features is feature engineering's twin idea.


Step 3 — The model: still linear, just wearing a curve costume

WHAT. Our prediction rule is now

WHY — read every symbol.

  • ::: the model's guess for at input (the little subscript means "using knob settings ").
  • ::: height knob (multiplies the constant ).
  • ::: straight-slope knob (multiplies ).
  • ::: bend knob (multiplies ) — positive bends the curve into a smile, negative into a frown.

The crucial observation: the knobs enter only by multiplication and addition — never as or . That is exactly what "linear in the parameters" means, and it is why our old linear-regression machinery still applies untouched.

PICTURE.

Figure — Polynomial regression

Watch the same curve morph as we turn the bend knob from negative (frown) through zero (flat line) to positive (smile).


Step 4 — Stack every example: the design matrix

WHAT. We have training points (say ). For each point we build its feature row and stack all rows into one grid called the design matrix :

WHY the shape.

  • Each row ::: one training example, wearing all its manufactured features.
  • Column 1 (all s) ::: feeds the intercept .
  • Column 2 ( values) ::: feeds the slope .
  • Column 3 ( values) ::: feeds the bend .
  • The superscript ::: labels which example (not a power!). So is the 2nd point's input, and is that input squared.

PICTURE.

Figure — Polynomial regression

Each colored dot on the left becomes one colored row on the right. The grid is just the data, re-dressed.


Step 5 — Measure wrongness: the cost surface

WHAT. For a given setting of knobs, the model's total error is

WHY every piece.

  • ::: the residual — how far the guess sits from the truth for point .
  • The square ::: makes over- and under-shoots both count as positive, and punishes big misses more. It also makes the surface a smooth bowl (see picture), so calculus can find the bottom.
  • ::: add the squared misses over all points.
  • ::: average () and a friendly so the derivative later comes out clean.

PICTURE.

Figure — Polynomial regression

is a bowl over the knob-space. The lowest point of the bowl is the best fit. Our whole job is to find that bottom.


Step 6 — Roll to the bottom: the normal equation

WHAT. The bottom of a smooth bowl is exactly where the ground is flat — where the gradient (the vector of slopes of in each knob direction) is zero. Setting gives:

WHY each symbol.

  • ::: "which way is uphill, and how steep" for every knob at once; zero means we're at the flat bottom.
  • ::: flipped so rows become columns — needed to line up the sums.
  • ::: a small square grid (here ) summarizing how the features overlap.
  • ::: the inverse — the matrix "undo" button that isolates .
  • ::: how each feature correlates with the target.

This is word-for-word the linear-regression normal equation. Only changed — the algebra never noticed we bent the curve. (Steepest-descent alternatives live in gradient descent variants.)

PICTURE.

Figure — Polynomial regression

The gradient arrows all point downhill; the star marks where they vanish — the solved .


Step 7 — Edge case A: features of wildly different size

WHAT. If ranges up to , then reaches and reaches . The columns of now span millions to ones.

WHY it breaks. The bowl becomes a long, thin, tilted canyon instead of a round bowl. Gradient descent zig-zags forever, and becomes numerically explosive (nearly non-invertible). The fix is feature scaling: after manufacturing each feature, replace it with

where is that feature's mean and its spread. This re-rounds the canyon into a bowl.

PICTURE.

Figure — Polynomial regression

Left: the stretched canyon (unscaled) — descent ricochets. Right: the round bowl (scaled) — descent walks straight in.


Step 8 — Edge case B: too many knobs (overfitting)

WHAT. Raise the degree so the number of knobs meets or beats the number of points . Now the curve can thread every dot exactly.

WHY it's a trap. Zero training error looks perfect, but the curve wiggles violently between points, chasing noise instead of the trend. Test error explodes. This is the bias–variance tradeoff: high degree kills bias but detonates variance. The cure is picking degree with a validation set, or reining knobs in with regularization.

PICTURE.

Figure — Polynomial regression

Degree 2 (green) rides the trend. Degree 8 (red) touches every dot yet lurches wildly at the edges — memorizing, not learning.


The one-picture summary

Figure — Polynomial regression

Curved data lift with stack into slide to the bottom of the cost bowl read off project the flat fit back down as a curve.

curved data x and y

manufacture features 1 x x squared

stack into design matrix Phi

cost bowl J of theta

normal equation solves theta

fitted curve back in x y plane

Recall Feynman retelling — say it back in plain words

A straight line has two knobs and both make it straight, so it can never bend to hug curved dots. The clever move: for each input , secretly invent a companion number and treat it as a whole new direction. In that taller space the curved data straightens out, so our ordinary straight-line fitter works perfectly — we never rebuilt the fitter. We pile every example's features into a grid , measure total squared miss as a bowl-shaped cost, and roll to the bottom of the bowl with one clean formula . Two dangers: if dwarfs , the bowl becomes a nasty canyon (scale the features to fix it); and if we add too many knobs, the curve memorizes the dots instead of the pattern (choose degree with validation). Fit a flat thing in a lifted space, and its shadow back home is a curve.

Recall

Why is polynomial regression still "linear"? ::: It is linear in the parameters (they only multiply and add), even though it is curved in . What single object changes versus plain linear regression? ::: Only the design matrix (now holding powers of ); the cost and normal equation are identical. What two dangers get their own edge-case steps? ::: Wildly different feature scales (fix: scaling) and too-high degree (fix: validation / regularization).