Skip to content

Grokem · https://www.grokem.com/ai-and-machine-learning/machine-learning/supervised-learning/precision-recall-f1-explained

Precision, recall and F1: judging a classifier beyond accuracy

Why a model that is 62% accurate by never predicting anything is useless, and how the confusion matrix, precision, recall and F1 tell you what a classifier really does.

Grokem Editorial

Editorial desk

Published
  • Beginner
  • 4 min read

What you will learn

  • Read a confusion matrix
  • Calculate and explain precision, recall, F1 and accuracy
  • Recognise why accuracy misleads on imbalanced data
  • Choose which error matters more for a given problem

Suppose a model flags transactions as fraud, and it is "98% accurate". Is it good? You cannot tell yet, and the reason is the most important idea in evaluating classifiers: accuracy hides the kind of mistakes a model makes.

The four outcomes

A yes/no classifier can be right or wrong in two ways each, giving four outcomes. Together they form the confusion matrix.

Actually yes Actually no
Model says yes True positive (TP) False positive (FP): a false alarm
Model says no False negative (FN): a miss True negative (TN)

Everything else is arithmetic on those four counts.

The metrics

  • Accuracy = (TP + TN) / everything. The share of all predictions that were right.
  • Precision = TP / (TP + FP). When the model says yes, how often is it right? Low precision means many false alarms.
  • Recall (also called sensitivity) = TP / (TP + FN). Of all the real yeses, how many did the model find? Low recall means many misses.
  • F1 = 2 × precision × recall / (precision + recall). A single number that is high only when both are high. It is the harmonic mean, which punishes a big gap between the two.

A worked example

Sixteen cases: six are really positive and ten are negative. The model finds four of the six, and wrongly flags two of the ten.

python
actual    = [1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
predicted = [1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]

tp = sum(a == 1 and p == 1 for a, p in zip(actual, predicted))
fp = sum(a == 0 and p == 1 for a, p in zip(actual, predicted))
fn = sum(a == 1 and p == 0 for a, p in zip(actual, predicted))
tn = sum(a == 0 and p == 0 for a, p in zip(actual, predicted))

precision = tp / (tp + fp)
recall = tp / (tp + fn)
f1 = 2 * precision * recall / (precision + recall)
accuracy = (tp + tn) / len(actual)

print(f"confusion matrix: TP={tp} FP={fp} FN={fn} TN={tn}")
print(f"precision {precision:.2f}  recall {recall:.2f}  F1 {f1:.2f}  accuracy {accuracy:.2f}")

# the trap: a model that never predicts a positive
lazy = [0] * len(actual)
print("always-negative model: accuracy", sum(a == p for a, p in zip(actual, lazy)) / len(actual), "recall", 0 / sum(actual))
text
confusion matrix: TP=4 FP=2 FN=2 TN=8
precision 0.67  recall 0.67  F1 0.67  accuracy 0.75
always-negative model: accuracy 0.625 recall 0.0

Precision is 4 of the 6 flagged, recall is 4 of the 6 real positives, and accuracy is 12 of 16.

The accuracy trap

The last line is the point. A model that never predicts a positive is 62.5% accurate here, because most cases are negative, and it finds nothing (recall 0). The real thing is more extreme: if 1 in 1,000 transactions is fraud, a model that says "not fraud" every time is 99.9% accurate and completely useless.

Whenever the classes are unbalanced, which is the usual case for the problems worth solving (fraud, disease, defects, churn), accuracy tells you very little. Look at precision and recall.

Choosing what to optimise

The two errors have different costs, and the right balance depends on the problem.

Problem Worse mistake Favour
Screening for a serious disease Missing a sick patient Recall
Spam filter Hiding a real e-mail Precision
Fraud alerts reviewed by people Missing fraud, but reviewers have limited time A balance, often judged by F1 or by cost
Content moderation at scale Depends on the harm of each side Decide explicitly

The threshold

Most models produce a score, and you pick a threshold above which you say "yes". Moving it trades one metric for the other: a lower threshold finds more real cases (higher recall) and raises more false alarms (lower precision). So the model's quality is better described by the precision-recall curve than by one pair of numbers, and the threshold is a business decision, not a technical afterthought.

Good practice

  • Evaluate on data the model has not seen. Metrics on training data flatter the model.
  • Report the confusion matrix with the metrics. The counts show how much evidence the percentages rest on.
  • Compare with a trivial baseline such as always predicting the most common class. If your model barely beats it, it has learned little.
  • Check the metrics per group (by region, device, customer type). A good average can hide a group the model fails.

Glossary

True positive (TP)
The model said yes and the answer was yes.
False positive (FP)
The model said yes but the answer was no: a false alarm.
False negative (FN)
The model said no but the answer was yes: a miss.
True negative (TN)
The model said no and the answer was no.
Precision
Of everything the model flagged, the share that was correct: TP / (TP + FP).
Recall
Of everything that really was positive, the share the model found: TP / (TP + FN).

Frequently asked questions

Which metric should I use?

The one that matches the cost of the mistakes. If a missed case is expensive (a disease, a fraud), favour recall. If a false alarm is expensive (blocking a customer), favour precision. F1 balances the two when both matter.

Do these work for more than two classes?

Yes. Calculate them for each class against the rest and average, choosing how to weight classes. Libraries offer micro, macro and weighted averages.

Next steps

  • Compute these metrics for a model you use, on data it was not trained on
  • Look at how the threshold changes precision and recall

References

From Grokem: https://www.grokem.com/ai-and-machine-learning/machine-learning/supervised-learning/precision-recall-f1-explained

How did this strike you?

Be the first to react – it is anonymous.

Community

Questions about this?

Ask in Questions and help and get help from other readers.

Ask a questionBrowse Questions and help

Comments

No comments yet. Share what worked for you, or ask a question.

Choose what we may use. You can change this at any time with “Privacy choices” at the foot of every page. Details are in the Cookie policy and the Privacy policy.

Strictly necessary

Remembers your theme, the lessons you marked as done and this choice. It stays on your device, tracks nothing and cannot be switched off.

Always on
What exactly is collected, and why

Grokem’s own counts

Collects
The pages you open and how long you stay, how far you scroll, where you came from, your device type, browser, screen size, language and time zone. Your IP address is turned into a code that changes every day; it is never stored as it is.
Why
To see how long lessons are read and where readers leave. Not shared with anyone.
Stored
A random visitor ID, a visit ID and the time of your last page view in your browser’s storage (gr_vid, gr_sid, gr_last). No name or email is attached.
Precision, recall and F1: judging a classifier beyond accuracy | Grokem