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. (One of them, softmax regression, turns out to secretly be that same line — just run several times over.)

The nervous part: choosing blind

Before you've seen a single row of data, STOR 323 asks you to commit to a model family — linear regression, softmax regression, kNN — and only then hand it real examples to fit. That should feel uncomfortable. Isn't picking the family supposed to come after you've looked at the data, not before? What if you commit to the wrong one?

Hold onto that discomfort — it's the right instinct, and this lesson resolves it properly in a minute. For now, notice what you can already answer with zero data in hand: what shape does the correct answer have to take? A house price is a number. A digit is one of ten unordered categories. That's a fact about the problem itself, not about the data — and it already rules out most of the choice before you've collected a single example.

"Predict a house's price"
needs an answer shaped like
a single number
before any data, that alone says:
✓ Linear regression fits ✗ Softmax regression doesn't
"Recognize a handwritten digit"
needs an answer shaped like
1 of 10 unordered categories
before any data, that alone says:
✗ Linear regression doesn't ✓ Softmax regression fits

No data collected yet in either case — just the problem statement.

Sharpen your pencil: name the shape

Five more problems, zero data attached to any of them. For each, pick the shape a correct answer has to take — a plain number, a probability between 0 and 1, or one of several unordered categories.

So what actually differs between model families?

Not the output shape — that's fixed by the problem, as you just saw, not chosen by the model family. What differs is something else entirely: how much of the training data each family keeps around after it's done training. That's a separate question from output shape, and it's the one new idea the rest of this lesson is 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 its own family instead of just reusing linear regression: a model family isn't only a formula — it's a formula plus a promise about what shape of answer it's allowed to produce. Change the shape you need, and you've changed which family even qualifies, not just tweaked the one you already had. Get the shape right first, and the right family follows on its own — never the other way around.

Softmax regression: scoring a talent show

First, the bridge from what you already trust. You've already met β₀ + xᵀβ — that's linear regression's whole formula. Softmax regression doesn't throw that away; it just runs ten copies of it side by side, one per digit, each with its own coefficients: zk(x) = βk0 + xᵀβk for each digit k. Same formula, same fitting machinery — just ten of them instead of one, each producing its own raw score for its own digit.

So where does the name come from? A plain max would just hand the single largest of those ten scores 100% of the vote and give everyone else 0% — a hard, all-or-nothing cutoff (that's actually what argmax already does, further back in lesson 1). The trouble with a hard cutoff: nudge a score slightly and the winner either doesn't move at all, or flips instantly — there's no "give" to learn from. Softmax is the soft, smoothed-out version of that same idea: scores still compete for a shrinking 100%, but a close second place keeps a meaningful share instead of getting zeroed out. That smoothness is exactly what training needs — the same gradual, nudge-and-check fitting process used to find linear regression's coefficients can be used here too, which a hard max would never allow.

x linear regression for digit 7 → z(7) linear regression for digit 1 → z(1) linear regression for digit 9 → z(9) softmax p(7) p(1) p(9)

Every digit gets its own linear regression, run on the same x. Softmax takes all their raw scores at once and turns them into probabilities — p(7) + p(1) + p(9) = 100%, always, no matter what the raw scores were.

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.