I5 · Machine Learning Foundations

Precision, Recall and F1 Score

Precision is the share of positive predictions that were correct. Recall is the share of actual positives that were found. F1 is the harmonic mean of the two, so it's only high when both are high.

12 min Intermediate · Sublevel Intermediate

What you need: The earlier topics of this module.

Definition

Precision is the share of positive predictions that were correct. Recall is the share of actual positives that were found. F1 is the harmonic mean of the two, so it's only high when both are high.

In simple words

A fisherman casts a net. Precision asks: of what's in the net, how much is fish, and not junk? Recall asks: of all the fish in the lake, how many did I catch? A tiny net gives high precision and low recall. A huge net gives high recall and low precision.

Overview

  • Precision is the share of predicted positives that are correct: TP ÷ (TP + FP).
  • Recall (sensitivity) is the share of actual positives found: TP ÷ (TP + FN).
  • F1 is the harmonic mean of precision and recall, high only when both are high.
  • Raising the threshold usually increases precision and lowers recall, and vice versa.
  • These metrics matter most when classes are imbalanced.
  • The right balance depends on the costs of false alarms and misses.

Why it matters

Choosing the wrong metric means optimising for the wrong thing. A search engine or spam filter needs precision, because false alarms annoy people. Disease screening needs recall, because missing cases is dangerous.

Key terms

Precision
TP / (TP + FP): of everything flagged positive, the share that was right.
Recall or sensitivity
TP / (TP + FN): of all real positives, the share that was found.
F1 score
The harmonic mean of precision and recall: 2PR / (P + R).
Precision-recall trade-off
Raising the threshold usually raises precision and lowers recall, and vice versa.
Specificity
TN / (TN + FP): the share of real negatives correctly identified.
ROC and PR curves
Charts showing performance across every threshold.

Types

Several metrics are built from precision and recall. Each type answers a slightly different question.

1. Precision

Of the cases flagged as positive, how many really are.

  • How it works: Count true positives and divide by everything the model flagged; false alarms lower it.
  • Example: If a spam filter flags 100 emails and 90 are spam, precision is 0.9.
  • When to use it: Prioritise it when false positives are costly, such as blocking real emails or accusing innocent customers.

2. Recall (sensitivity, true positive rate)

Of the actual positive cases, how many the model found.

  • How it works: Count true positives and divide by all actual positives; misses lower it.
  • Example: If 120 spam emails arrived and the filter caught 90, recall is 0.75.
  • When to use it: Prioritise it when missing a positive is dangerous, as in disease screening or fraud detection.

3. F1 score

A single number combining precision and recall as their harmonic mean.

  • How it works: F1 = 2PR ÷ (P + R); because it is a harmonic mean, a low value of either pulls F1 down sharply.
  • Example: Precision 0.9 and recall 0.75 give F1 = 2 × 0.675 ÷ 1.65 ≈ 0.82.
  • When to use it: Use it to compare models with one number when precision and recall matter about equally.

4. F-beta score

A weighted version of F1 that favours recall (β > 1) or precision (β < 1).

  • How it works: F_β = (1 + β²)PR ÷ (β²P + R); F2 treats recall as twice as important as precision.
  • Example: Using F2 to choose a cancer-screening model where misses are much worse than false alarms.
  • When to use it: Use it when the costs of the two errors are clearly unequal.

5. Macro, micro and weighted averages

Ways to combine per-class precision, recall or F1 in multi-class problems.

  • How it works: Macro averages classes equally; micro pools all decisions; weighted averages by class size; classification_report shows all three.
  • Example: Macro F1 drops if a rare class is handled badly, even when micro F1 stays high.
  • When to use it: Use macro averages when every class matters equally, including rare ones.

Explained in detail

Two different questions

Precision asks, "when the model says yes, how often is it right?" Recall asks, "of all the real yeses, how many did the model find?" Using the fraud example from the confusion matrix page, with 40 true positives, 200 false positives and 10 false negatives: precision is 40 / 240 ≈ 17 per cent, so most alerts are false alarms; recall is 40 / 50 = 80 per cent, so most fraud is caught. A model can have high recall and low precision, or the reverse, and which one matters more depends on the problem.

Which to favour

SituationFavourReason
Screening for a serious diseaseRecallMissing a patient is worse; a second test removes false alarms
Spam filterPrecisionBlocking a real email is costly
Search resultsPrecision at the topUsers look only at the first few results
Finding all relevant legal documentsRecallMissing one document can lose a case
Balanced needF1Both errors matter similarly

Why the harmonic mean?

The ordinary average of precision 1.0 and recall 0.02 is 0.51, which would flatter a model that finds almost nothing. The harmonic mean, F1 = 2PR / (P + R), is pulled strongly towards the smaller value, giving about 0.04. F1 is high only when both precision and recall are high. When one matters more, the more general Fβ score weights recall β times as much as precision; F2 is common when recall is more important.

Thresholds and curves

  • Most classifiers output a probability, and the threshold turns it into a yes or no.
  • Lowering the threshold flags more cases: recall rises, precision usually falls.
  • A precision-recall curve plots both across every threshold, so you can choose the operating point that suits the costs.
  • The ROC curve plots recall against the false-positive rate; the area under it, AUC, summarises ranking quality, but for very rare positives the PR curve is more informative.
  • scikit-learn's classification_report prints precision, recall and F1 for every class at once.

How it works

Use casePriorityWhy
Spam filterPrecisionLosing a real email hurts
Cancer screeningRecallMissing a patient is worst
Fraud detectionBalance, by costBoth errors cost money
Search resultsPrecision at topPeople look at the first few

The trade-off

Lowering the threshold catches more positives, so recall goes up, but more false alarms slip in, so precision falls. You can't usually maximise both, and you choose the balance based on what each error costs.

The maths

Precision
Recall (sensitivity)
F1 score

Using TP = 8, FP = 5, FN = 2 from the screening example:

Precision = 8 / (8 + 5) = 0.615
Recall    = 8 / (8 + 2) = 0.800
F1 = 2 × (0.615 × 0.8) / (0.615 + 0.8) = 0.696

In code

metrics.py
from sklearn.metrics import classification_report, f1_score, precision_score, recall_score

actual =    [1] * 10 + [0] * 90
predicted = [1] * 8 + [0] * 2 + [1] * 5 + [0] * 85

print("Precision:", round(precision_score(actual, predicted), 3))
print("Recall:", round(recall_score(actual, predicted), 3))
print("F1:", round(f1_score(actual, predicted), 3))
print(classification_report(actual, predicted, target_names=["healthy", "sick"]))

Output

Precision: 0.615
Recall: 0.8
F1: 0.696
              precision    recall  f1-score   support

     healthy       0.98      0.94      0.96        90
        sick       0.62      0.80      0.70        10

    accuracy                           0.93       100
   macro avg       0.80      0.87      0.83       100
weighted avg       0.94      0.93      0.93       100

Common mistakes

  • Reporting one without the other. A model that flags everyone has perfect recall and is useless.
  • Using F1 when errors don't cost the same. F1 treats precision and recall as equally important.

Check yourself

Question 1

1. TP = 30, FP = 10, FN = 20. What's the precision?

Question 2

2. And the recall for those numbers?

Question 3

3. Why does F1 use the harmonic mean?

Summary

  • Precision: of those flagged, how many were right?
  • Recall: of the real ones, how many did you find?
  • F1 combines them. Choose based on what errors cost.

Finished reading?

Sources: Google Machine Learning Crash Course