🏛️ Issues and Advocacy Training
Courses › How AI Works: The Kinds of AI and What They Are For

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.

MeetingFlyers posted (hundreds)People who came
March130
April250
May360
June490
July5100

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.

FlyersRealGuess (15x + 12)ErrorError squared
1302739
25042864
3605739
4907218324
51008713169

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.

FlyersRealGuess (18x + 12)ErrorError squared
1303000
2504824
36066-636
49084636
5100102-24

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.

Flyers posted (hundreds) People 123456 New meeting: 130 Curve heads below zero Solid: straight line Dashed: memorizing curve
The memorizing curve fits the past perfectly and the future terribly. (Not drawn to exact scale: the real curve hits minus 20 at 6.)
New meeting (flyers)Real turnoutStraight line predictsMemorizing curve predicts
250555753
350757573
600130120minus 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.

A model is only as good as its data. It learns whatever patterns predict the labels, including patterns you didn't want it to learn. Old bias in, new bias out.

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

Check yourself

1. In a model that predicts whether a library book will be returned late, which of these is a feature?

2. A model's predictions are off by 2, 0 and 4 on three examples. What is its mean squared error?

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?

4. Why do builders hold back a test set the model never trains on?

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?

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.

Full glossary