Lessons › Chapter 4: Classification
The confusion matrix
We test a spam filter on 20 emails where we know the truth. Each block is an email, colored by the truth (coral = spam, teal = normal). The ball on top is what the model guessed.
Watch it move. This lesson is interactive on Gradient Lab: every step is an animated scene you can replay.
Start the interactive lessonStep by step
The fraud filter goes live
The fraud model is live. Some fraud is caught, some slips through, and some honest cards get frozen.
20 emails, two answers each
We test a spam filter on 20 emails where we know the truth. Each block is an email, colored by the truth (coral = spam, teal = normal). The ball on top is what the model guessed.
Sort into four boxes
Sort every email by two questions: what did the model say, and what was true? That gives a 2 × 2 table called the confusion matrix. The diagonal (top-left, bottom-right) is right; the other two boxes are mistakes.
Two kinds of mistakes
A false positive is a false alarm: a real email sent to spam. A false negative is a miss: spam that got through. They are different mistakes, and one may matter much more to you than the other.
Accuracy
accuracy counts the right answers: (6 + 9) / 20 = 75%. It’s the simplest score, and often the first one people look at.
Accuracy can fool you
Imagine 100 emails where only 5 are spam. A lazy model that always says “normal” scores 95% accuracy, yet it catches zero spam. When one answer is rare, accuracy alone hides the problem.
That’s why the next lesson introduces precision and recall.
In real life: the airport bag scanner
Security scanners face the same four outcomes. A false alarm means a bag gets searched by hand, which is annoying. A miss could be dangerous. Teams tune scanners by studying exactly this table.
Quick check: The filter sends your friend’s real email to the spam folder. That is a…
- True positive
- False positive (false alarm) (correct)
- False negative (miss)
The model said “spam” (positive) but it was wrong, so it’s a false positive.
Pseudocode
# test a spam filter on 20 emails we already know the answer for
for each email:
truth = is it really spam?
guess = did the model say spam?
put it in one of four boxes:
caught spam (said spam, was spam)
missed spam (said normal, was spam)
false alarm (said spam, was normal)
correctly normal (said normal, was normal)
accuracy = right answers / all answers
Key terms
- confusion matrix
- A 2 × 2 table counting each kind of right and wrong answer.
- false positive
- Said yes, but the answer was no: a false alarm.
- false negative
- Said no, but the answer was yes: a miss.
- accuracy
- The share of all answers that were right.
Build it: Spam filter with Naive Bayes (30 min, runs in your browser)
See the projectIn the news
Recent models and papers that build on this idea: