Lesson 3 of 9 · about 17 minutes
How Machine Learning Learns
You'll be able to
- Explain what training data, features and labels are
- Calculate a simple loss (average squared error) by hand
- Explain overfitting and why models are graded on a held-back test set
- Ask the right questions about a model's training data and accuracy claims
Machine learning sounds mysterious and is mostly arithmetic done a staggering number of times. Once you see one small model learn, every bigger one is the same idea with more numbers.
The parts
Every machine learning project has the same ingredients.
- Training data: a pile of past examples. For a spam filter, 100,000 old emails.
- Features: the inputs the model looks at for each example. Number of links, whether the sender is in your contacts, whether the subject line says FREE in capitals.
- Labels: the right answer for each example. This email was spam; that one wasn't. Somebody had to supply those answers, often by clicking "report spam."
- A model: a formula with adjustable numbers in it (called parameters or weights). At the start, the numbers are guesses.
- A loss: a score for how wrong the model is. Lower is better.
Training means: make a guess for every example, measure the loss, nudge the adjustable numbers to make the loss a little smaller, and repeat until it stops improving. That's it. The rest is engineering.
A worked toy example
The invented Pine Hollow Neighborhood Association wants to predict turnout at its meetings so it can rent enough chairs. It has records from five meetings: how many flyers volunteers posted, and how many people came.
| Meeting | Flyers posted (hundreds) | People who came |
|---|---|---|
| March | 1 | 30 |
| April | 2 | 50 |
| May | 3 | 60 |
| June | 4 | 90 |
| July | 5 | 100 |
Flyers is the feature. Attendance is the label. The model is a straight line:
predicted people = (slope x hundreds of flyers) + starting point
The slope and the starting point are the two adjustable numbers. Training means finding values that fit the five meetings best.
Step 1: a first guess and its loss
Say we guess slope 15 and starting point 12. For March (1 hundred flyers) the model predicts 15 x 1 + 12 = 27 people. Real: 30. Error: 3.
We need one number for "how wrong overall." The most common choice is mean squared error: square each error so negatives don't cancel positives and big misses count extra, then average.
| Flyers | Real | Guess (15x + 12) | Error | Error squared |
|---|---|---|---|---|
| 1 | 30 | 27 | 3 | 9 |
| 2 | 50 | 42 | 8 | 64 |
| 3 | 60 | 57 | 3 | 9 |
| 4 | 90 | 72 | 18 | 324 |
| 5 | 100 | 87 | 13 | 169 |
Sum of squared errors: 9 + 64 + 9 + 324 + 169 = 575. Divide by 5 meetings: a loss of 115.
Step 2: nudge and re-measure
Every guess was too low, so the slope should go up. Try slope 18, keeping the starting point at 12.
| Flyers | Real | Guess (18x + 12) | Error | Error squared |
|---|---|---|---|---|
| 1 | 30 | 30 | 0 | 0 |
| 2 | 50 | 48 | 2 | 4 |
| 3 | 60 | 66 | -6 | 36 |
| 4 | 90 | 84 | 6 | 36 |
| 5 | 100 | 102 | -2 | 4 |
Sum: 80. Divide by 5: a loss of 16. Down from 115. For this data, slope 18 and starting point 12 happen to be the best possible straight line; no other pair of numbers gets the loss lower. A computer finds that by making many small nudges, each one in the direction that shrinks the loss. Lesson 4 explains how it knows which direction.
The trained model says: each extra 100 flyers brings about 18 more people, and about 12 show up no matter what (the regulars). Not bad for five rows.
Overfitting: the model that memorized
A fancier model can hit all five meetings exactly. A curved formula with five adjustable numbers can bend through every point, for a training loss of zero. Looks perfect. Now the association holds three more meetings the model never saw.
| New meeting (flyers) | Real turnout | Straight line predicts | Memorizing curve predicts |
|---|---|---|---|
| 250 | 55 | 57 | 53 |
| 350 | 75 | 75 | 73 |
| 600 | 130 | 120 | minus 20 |
On the new meetings, the straight line's mean squared error is (4 + 0 + 100) / 3, about 35. The curve's is about 7,500, almost all from predicting that 600 flyers will produce negative 20 people. That is overfitting. The curve learned the noise in five meetings (May was a little light, maybe it rained) instead of the pattern. It looks brilliant on the data it studied and falls apart on anything new.
Why the test set exists
Builders guard against this with a test set. Before training, they lock away a slice of the data, often 10 to 20 percent. The model never sees it during training. At the end, they score the model on the locked slice. That score is the honest one, because it is the closest thing to the future.
If a company tells you its model is "98 percent accurate," the first question is: accurate on what? On its training data, that number means little. On a held-back test set from the same source, it means something. On fresh data from your town, collected after the model was built, it means the most.
Where real models get their data
A spam filter's labels come from millions of users clicking "report spam." A credit model's labels come from who actually paid back loans. A medical imaging model's labels come from doctors marking scans. Every one of those label sources has gaps. If the old loan records come from a bank that rarely lent in certain neighborhoods, the model has little data on those neighborhoods and may score them poorly for that reason alone. When the world shifts (a pandemic, a new scam, a new road), a model trained on the old world keeps predicting the old world. This is called drift, and it is why serious teams re-test models on a schedule.
Common mix-ups
- Claim: More complex models are always better. What the evidence shows: A complex model with little data overfits. For small tables, simple models often win on the test set.
- Claim: A computer can't be biased. What the evidence shows: Models copy patterns in their training labels. If past human decisions were skewed, the model learns the skew.
- Claim: High accuracy means the model is useful. What the evidence shows: If only 1 in 100 transactions is fraud, a model that says "not fraud" every time is 99 percent accurate and catches nothing. Ask how it does on the rare cases that matter.
Reporting angle
When a vendor or agency cites an accuracy figure, ask: measured on what data, collected where and when, and was it held out from training? What happens to the people the model gets wrong, and how many are there per year? What are the error rates broken out by group (age, neighborhood, language)? When was the model last re-tested? Ask for the validation report itself. A one-number accuracy claim in a sales deck is advertising.
Research angle
- Google Machine Learning Crash Course: free lessons on training data, loss, overfitting and evaluation.
- NIST face recognition study: a government test that reports error rates broken out by group.
- Gender Shades: a peer-reviewed audit showing accuracy that differs by skin tone and sex.
- Science study on health care algorithm bias: a case where a training label quietly skewed results.
Check yourself
1. In a model that predicts whether a library book will be returned late, which of these is a feature?
Features are the inputs the model uses to make its guess. Whether the book came back late is the label, the answer it is trying to predict.
2. A model's predictions are off by 2, 0 and 4 on three examples. What is its mean squared error?
Square each error (4, 0, 16), add them (20), divide by three examples: about 6.67.
3. A model scores 99 percent on its training data and 61 percent on new data it never saw. What is the most likely problem?
A big gap between training and test performance is the classic sign of overfitting.
4. Why do builders hold back a test set the model never trains on?
Grading a model on its own training data is like grading a student on the exact questions they memorized. The test set stands in for the future.
5. A hiring model was trained on ten years of a company's past hires, most of whom were men. What risk does that create?
A model learns whatever patterns predict the labels in its data, including unfair ones. The data's history becomes the model's habit.
Terms in this lesson
- Training data
- The examples a model learns from, usually with known right answers.
- Feature
- One input the model uses, such as a price, a date or a word count.
- Label
- The right answer for a training example: spam or not, the actual sale price, the real turnout.
- Loss
- A number measuring how wrong the model's guesses are. Training tries to make it smaller.
- Overfitting
- When a model memorizes its training examples, noise included, and does worse on new data.
- Test set
- Examples held back from training and used only to measure how well the model works on unseen data.