Find a course

Module 2: How Machines LearnLesson 6 of 1013 min

Is the Model Any Good? Accuracy, Precision and Recall

Evaluate AI systems with a confusion matrix, accuracy, precision and recall, and avoid the traps of overfitting, imbalanced data and base rates.

Step 1 of 13Learn

The 99% accurate model that does nothing

A company announces a fraud detector that is 99% accurate. Impressive? Suppose 1 in every 100 transactions is fraudulent. A "model" that simply labels every transaction as legitimate is right 99 times out of 100. It is 99% accurate and catches zero fraud.

This is the first lesson of AI evaluation: a single accuracy number can hide almost anything. To judge a model, you need to know what kinds of mistakes it makes.

For a yes/no prediction, there are four possible outcomes, often laid out in a table called a confusion matrix:

Actually positiveActually negative
Predicted positiveTrue positive (TP)False positive (FP)
Predicted negativeFalse negative (FN)True negative (TN)

Using spam filtering, where "positive" means spam:

  • True positive: a spam email correctly sent to the spam folder.
  • False positive: a real email wrongly sent to spam. Your job offer disappears.
  • False negative: a spam email that slips into your inbox.
  • True negative: a real email correctly delivered.

The two kinds of error rarely cost the same. A false positive in spam filtering, losing an important email, is usually worse than a false negative, deleting one extra spam message. In cancer screening it flips: missing a cancer (false negative) is far worse than a false alarm that leads to another test. Good evaluation starts by asking which mistake hurts more.

Step 2 of 13Practice · Sort into groups

In a medical test where "positive" means the patient has the disease, classify each outcome.

Tap an item, then tap its group (or drag it there).

Step 3 of 13Learn

Precision and recall

Two measures capture what accuracy hides.

Precision answers: when the model says yes, how often is it right?

Precision = TP ÷ (TP + FP)

Recall (also called sensitivity) answers: of all the real positives, how many did the model find?

Recall = TP ÷ (TP + FN)

Accuracy = (TP + TN) ÷ all cases

Worked example: a fraud model reviews 10,000 transactions, 100 of which are fraud. It flags 150 transactions. Of those, 80 are really fraud (TP = 80) and 70 are legitimate (FP = 70). It misses 20 frauds (FN = 20). The remaining 9,830 legitimate transactions are correctly passed (TN = 9,830).

  • Precision = 80 ÷ 150 ≈ 53%: about half of the alerts are real fraud.
  • Recall = 80 ÷ 100 = 80%: the model catches four out of five frauds.
  • Accuracy = (80 + 9,830) ÷ 10,000 ≈ 99.1%, barely better than the do-nothing model's 99%, yet this model is genuinely useful.

Precision and recall pull against each other. Make the model flag more transactions and it catches more fraud (higher recall) but raises more false alarms (lower precision). Make it stricter and precision rises while recall falls. Choosing the balance is a business decision, not a technical one: how expensive is a missed fraud compared with an annoyed customer whose card was wrongly blocked?

Step 4 of 13Practice · Diagram

The confusion matrix

Every prediction lands in one of four cells. Precision and recall are built from them.

A spam filter sends your friend's birthday invitation to the spam folder. Which cell just got one bigger?

Tap the shape that answers the question, then press Check.

The confusion matrixTwo-by-two matrix: True positive (TP), False negative (FN), False positive (FP), True negative (TN).True positive (TP)False negative (FN)False positive (FP)True negative (TN)What the model saysYesNoWhat is really trueActually noActually yes
True positive (TP)

Real fraud, correctly flagged.

False negative (FN)

Real fraud the model missed. Recall suffers.

False positive (FP)

A legitimate case wrongly flagged. Precision suffers.

True negative (TN)

Legitimate, correctly left alone.

Step 5 of 13Try the calculator

Confusion matrix calculator

Enter the four counts from a model's test results. Try making the model stricter (fewer false positives, more false negatives) and see how precision and recall trade off.

Accuracy
99.1 %
Precision
53.3 %
Recall
80 %
Alerts raised
150

Step 6 of 13Practice · Work it out

A spam filter is tested on emails containing 200 spam messages. It sends 190 emails to the spam folder, of which 170 really are spam. What is its recall, in percent?

Step 7 of 13Learn

The base-rate trap

Precision depends heavily on how common the positive cases are, which is called the base rate. This catches out even experts.

Imagine an AI screening test for a rare condition that affects 1 in 1,000 people. The test has 99% recall (it catches 99% of real cases) and wrongly flags only 1% of healthy people. Screen 100,000 people:

  • About 100 actually have the condition, and the test flags about 99 of them.
  • About 99,900 are healthy, and 1% of them, about 999, are wrongly flagged.

So roughly 1,098 people get a positive result, and only 99 of them are truly ill. Precision is about 9%. More than 9 out of 10 positive results are false alarms, from a test that sounds 99% reliable.

This is not a flaw unique to AI; it applies to any test for a rare event, from airport security to fraud detection. The practical lessons:

  • When a rare event is predicted, ask about precision, not just accuracy or recall.
  • Positive results for rare events usually need a second, more specific check, often a person.
  • Be wary of claims like "detects lies with 90% accuracy" or "spots future criminals." When the thing being detected is rare, even a good model produces many false accusations.

Many real AI deployments, including face recognition in policing and automated fraud blocking, have run into exactly this problem.

Step 8 of 13Note

Step 9 of 13Practice · Chart

Who gets flagged when 100,000 people are screened

The same numbers as above: a rare condition (1 in 1,000), 99% recall and a 1% false alarm rate.

Tap the bigger group of flagged people.

Tap the bar that answers the question, then press Check.

Who gets flagged when 100,000 people are screenedBar chart with 2 categories: Flagged and sick: 99; Flagged but healthy: 999.02004006008001,000Flagged and sickFlagged but healthy99999
Worked example from the lesson; numbers rounded.
Who gets flagged when 100,000 people are screened
ItemPeople flagged
Flagged and sick99
Flagged but healthy999

Step 10 of 13Learn

Overfitting and underfitting

A model can fail in two opposite ways.

Underfitting means the model is too simple to capture the real pattern. Predicting house prices from square footage alone, with a single straight line, misses the effects of location, condition and everything else. Its errors are large on both the training data and the test data.

Overfitting means the model is so flexible that it memorizes the training data, including random noise and quirks. Picture a student who memorizes that "question 7's answer is C" instead of learning the material. On the training data, an overfit model looks brilliant. On new data, it falls apart.

The telltale sign is the gap: low training error with much higher test error points to overfitting; high error on both points to underfitting.

Common cures for overfitting:

  • More data, so random quirks average out.
  • A simpler model or fewer features.
  • Regularization, techniques that penalize overly complex models.
  • Early stopping: stop training when performance on the validation set stops improving, even if training error is still falling.

Common cures for underfitting:

  • A more flexible model, such as more layers or more features.
  • Better features that carry the signal the model is missing.
  • Training longer.

Finding the sweet spot between the two is one of the central skills in machine learning. The test set, kept separate from training, is how you know whether you've found it.

Step 11 of 13Practice · Chart

Underfitting vs overfitting

As a model gets more flexible, training error keeps falling. Test error falls, bottoms out, then rises again as the model starts memorizing noise.

Tap the complexity level that would work best on new data.

Tap the point that answers the question, then press Check.

Underfitting vs overfittingLine chart with 8 categories: 1: Training error 30%, Test error 32%; 2: Training error 22%, Test error 25%; 3: Training error 16%, Test error 19%; 4: Training error 12%, Test error 16%; 5: Training error 9%, Test error 15%; 6: Training error 6%, Test error 17%; 7: Training error 4%, Test error 21%; 8: Training error 2%, Test error 27%.0%10%20%30%40%Error12345678Model complexity (illustrative scale)
Illustrative curves, not from a real model.
Underfitting vs overfitting
Model complexity (illustrative scale)Training errorTest error
130%32%
222%25%
316%19%
412%16%
59%15%
66%17%
74%21%
82%27%

Step 12 of 13Practice · What would you do?

What would you do?

A model that predicts which loan applicants will repay scores 97% on its training data and 96% on the test set. It has been tested only on applicants from one large city. The bank now wants to use it nationwide. What should it do first?

Step 13 of 13Practice · Choose one

An airport face-matching system flags travelers on a watch list. Watch-list travelers are extremely rare. The system has 99% recall and a 0.5% false positive rate. What is the most likely result?