TetSharing
AI basics tutorial · 10 chapters
  1. What machine learning is
  2. Features and labels
  3. Train, test and validation data
  4. Overfitting
  5. Accuracy, precision, recall
  6. k-nearest neighbors
  7. LLMs, tokens and context
  8. Prompt basics
  9. Embeddings and similarity
  10. Responsible AI (NIST AI RMF)

Final AI basics quiz

Chapter 5 of 10 · By Ethan Cole · Updated November 21, 2026

Accuracy, Precision and Recall, Explained Simply

"The model is 95% accurate" sounds great until you learn that 95% of the emails were not spam to begin with. Choosing the right measurement is as important as choosing the model.

The confusion matrix

For a yes/no classifier, every prediction falls into one of four boxes. Call the class you care about (spam, fraud, disease) the positive class:

  • True positive (TP): predicted positive, actually positive.
  • False positive (FP): predicted positive, actually negative. A false alarm.
  • False negative (FN): predicted negative, actually positive. A miss.
  • True negative (TN): predicted negative, actually negative.

Four metrics from four counts

  • Accuracy = (TP + TN) / everything. The share of all predictions that were right.
  • Precision = TP / (TP + FP). Of the items flagged positive, how many really were? High precision means few false alarms.
  • Recall = TP / (TP + FN). Of the real positives, how many did we catch? High recall means few misses.
  • F1 = the harmonic mean of precision and recall, 2PR / (P + R). It is high only when both are.

If the model never predicts positive, precision is undefined (0/0); libraries usually report it as 0 and warn you.

Run the numbers

The example scores two classifiers on the same twenty emails, four of which are spam. The first is a real attempt with a few mistakes. The second is a lazy "model" that always answers "not spam".

Python 3 · editable

Expected output

model: TP=3 FP=2 FN=1 TN=14
  accuracy=0.85 precision=0.60 recall=0.75 f1=0.67
lazy : TP=0 FP=0 FN=4 TN=16
  accuracy=0.80 precision=0.00 recall=0.00 f1=0.00

The lazy model reaches 80% accuracy without catching a single spam message: its recall is zero. Accuracy hid that completely, because the positive class is rare. This is why precision and recall exist.

Which metric matters?

It depends on what a mistake costs:

  • Medical screening: a miss (false negative) is dangerous, so favor recall, and confirm positives with a second test.
  • Spam filtering: hiding a real email (false positive) is costly, so favor precision.
  • Balanced costs: F1, or accuracy if the classes are balanced.

Most classifiers output a score, and you choose the cut-off. Lowering it raises recall and usually lowers precision; raising it does the reverse. Picking the threshold is a business decision as much as a technical one.

In scikit-learn

The sklearn.metrics module provides accuracy_score, precision_score, recall_score, f1_score, confusion_matrix and classification_report, which prints all of them per class. They compute exactly the formulas above.

Summary

  • TP, FP, FN, TN describe every prediction.
  • Precision limits false alarms; recall limits misses; F1 balances them.
  • With rare classes, accuracy alone can be badly misleading.

Quiz: Accuracy, precision, recall

1. A model flags 10 emails as spam and 8 really are spam. What is its precision?

Precision = TP / (TP + FP) = 8 / 10.

2. There are 20 fraudulent transactions and the model catches 5. What is its recall?

Recall = TP / (TP + FN) = 5 / 20.

3. Why can accuracy be misleading?

A model that never predicts the rare class can still be "accurate".

Official docs

Written under the pen name Ethan Cole with AI-assisted drafting. Every code example on this page is executed by our test suite before publishing. Found a mistake? Tell us.