Lesson 0002

Beyond the straight line

You already have a feel for linear regression — one straight line, fitted by sliding two knobs. This lesson builds a feel for two more model families the same way: play first, name it after.

Are other model families "dependent on the data"?

Short answer: the shape you're willing to consider is a decision you make before touching any data — same as choosing "only straight lines" was for linear regression. What differs is how much of the data each family keeps around after training. That split is the one new idea this lesson is really about.

🗂️
Nonparametric — keeps the data
Doesn't compress anything. The training examples themselves are the fitted model. Every prediction means going back and looking at the stored examples again. kNN is like this.

Why softmax regression has to exist: output space first

Before picking any model family, the slides insist on one rule: figure out the shape of a correct answer before you go looking for a family to produce it. Think of a kids' shape-sorter toy — round hole, star hole, square hole. You look at your block's shape first, then find the matching hole. Nobody jams a star block through the round hole and hopes for the best.

⭐➡️⭐
Right order: output space first
Ask first: "what does a correct answer here actually look like?" For MNIST, it's one of 10 unordered categories — not a number, a label. Only then go pick a family whose output has that exact shape. Softmax regression is built specifically to output "a probability for each of several unordered categories" — the matching hole.

That's the whole reason softmax regression exists as a separate family rather than just reusing linear regression: the output space S is part of what a model family even is (recall 𝓕 = {f : X → S} from the notation decoder) — get S right first, and the choice of family follows from it, not the other way around.

Softmax regression: scoring a talent show

Linear regression predicts one number. But MNIST digit recognition isn't "how much" — it's "which one of 10 categories." Picture a talent show with judges: each contestant (each possible digit) gets a raw score reflecting how much evidence there is for them. A high score isn't a percentage yet — it's just "more evidence than the others." Softmax is the rule that turns those raw scores into percentages that always add up to 100%, so you can compare them directly.

Below are the actual raw scores from the slides for a handwritten "7" being classified — drag them and watch the probabilities update. Notice the total always lands on 100%, no matter what you set the sliders to.

Two things worth noticing as you drag: (1) raising one slider pulls the other bars down even though you didn't touch them — because they all have to share 100% between them. (2) The green bar (the model's actual prediction) is just argmax from the earlier lesson — whichever bar ends up tallest. Softmax gives you the percentages; argmax picks the winner.

Fitted state: still just numbers

Softmax regression is parametric, same family as linear regression — it just needs one set of coefficients per category instead of one overall. Ten digits means ten small coefficient vectors, but still a small, fixed, data-independent-in-size bundle of numbers. Compress, store, discard the original data.

kNN: asking your neighbors

Now for something structurally different. Imagine houses on a map, colored by neighborhood type — blue for one kind, orange for another. You want to guess the type of a brand-new address just dropped on the map (the ring outline). kNN's rule: look at its K closest existing neighbors, and go with whichever color wins the majority vote among them. No coefficients, no formula fitting — just "who's nearby, and what are they?"

Click anywhere on the map below to drop the query point, and drag K to change how many neighbors get a vote.

Drop the query point right in the contested zone between the two clusters, then flip K between 1 and 7. The prediction can flip back and forth — that's not a bug, it's the whole mechanism. K=1 means "trust only your single closest neighbor" (jumpy, sensitive to one weird example). A larger K averages over more opinions (steadier, but can blur real boundaries). Choosing K is itself a structural decision, same category as "how many features" or "which distance to use" — made before training, same as everything else in the model family.

Fitted state: the data itself

Unlike the two regressions above, kNN's "training" barely does anything — it just remembers the dataset (plus the choices you made: how many neighbors K, which distance measure, how ties are broken). All the real work happens at prediction time, walking through the stored examples fresh for every query. That's why it's nonparametric: nothing was compressed away.

All three, side by side

Model familyFitted stateParametric?
Linear regressiona handful of coefficientsYes — compresses data away
Softmax regressionone coefficient vector per categoryYes — compresses data away
kNNthe training data itselfNo — keeps everything

Check your recall

Try from memory first.

What do we call a model that compresses its training data into a small, fixed set of numbers?
What do we call a model whose fitted state is the entire training dataset itself?
What mechanism does kNN use among its K nearest neighbors to make a prediction?

Go deeper

An Introduction to Statistical Learning covers kNN classification in Ch. 2 and 4, and multi-class/softmax regression in Ch. 4 — the free official PDF. See RESOURCES.md for more, including StatQuest videos that walk through the same ideas visually if you want a second explanation.

Want to try different data placements in the kNN map, or see what happens with a 4th softmax category? Just ask.