Gradient LabStart the lesson

Lessons › Chapter 4: Classification

Precision, recall and F1

Look only at the emails the model called spam: the left column. 6 really were spam and 3 were false alarms. precision = 6 / 9 = 67%. High precision means you can trust a “yes”.

Watch it move. This lesson is interactive on Gradient Lab: every step is an animated scene you can replay.

Start the interactive lesson

Step by step

Story

Two kinds of complaint

Customers complain about frozen cards. The fraud team complains about misses. They can’t both be at zero.

Concept

Precision: when it says spam, is it right?

Look only at the emails the model called spam: the left column. 6 really were spam and 3 were false alarms. precision = 6 / 9 = 67%. High precision means you can trust a “yes”.

Concept

Recall: how much spam did it find?

Now look only at the emails that really were spam: the top row. The model caught 6 and missed 2. recall = 6 / 8 = 75%. High recall means few things slip through.

Key idea

You can’t max out both

A strict filter only flags emails it’s very sure about: precision goes up, but more spam slips through (recall drops). A loose filter catches nearly all spam but raises more false alarms. Moving the cutoff trades one for the other.

Insight

Which one matters more?

It depends on which mistake hurts more. A cancer screening test must not miss sick patients, so recall comes first. A spam filter must not hide your real emails, so precision comes first.

Build

F1: one balanced score

When you need a single number, f1 blends precision and recall. It’s high only when both are high: here 2 × 0.67 × 0.75 / (0.67 + 0.75) ≈ 71%.

Real life

In real life: a screening test

Cancer screening aims for very high recall: finding nearly every real case matters more than a few extra follow-up checks. A spam filter does the opposite and protects precision, so your real email never vanishes.

Quick check: A test for a serious disease must not miss sick patients. Which score should it focus on?

Recall measures how many real cases are found. Missing a sick patient is a false negative, which lowers recall.

Pseudocode

caught = 6        # said spam, was spam
false_alarms = 3  # said spam, was normal
missed = 2        # said normal, was spam

precision = caught / (caught + false_alarms)   # 6 / 9 = 67%
recall    = caught / (caught + missed)         # 6 / 8 = 75%

F1 = 2 × precision × recall / (precision + recall)   # 71%

Key terms

precision
When the model says yes, how often is it right?
recall
Of all the real yes cases, how many did the model find?
f1
A single score that balances precision and recall.

Build it: Spam filter with Naive Bayes (30 min, runs in your browser)

See the project

Build it: A screening classifier that doesn’t miss cases (35 min, runs in your browser)

See the project

Build it: Spot fraud with anomaly detection (25 min, runs in your browser)

See the project

In the news

Recent models and papers that build on this idea: