Find a course

Module 2: Inside Machine LearningLesson 5 of 1112 min

Measuring AI Mistakes

Calculate accuracy, understand false positives and false negatives, and see why the best balance between them depends on what is at stake.

Step 1 of 10Learn

Every model makes mistakes

No AI model is perfect, so a key question is: how often is it wrong, and in what way?

The simplest measure is accuracy: the fraction of predictions that are correct.

accuracy = correct predictions ÷ total predictions

A spam filter that checks 1,000 emails and gets 950 right has 95% accuracy.

But accuracy alone can be badly misleading. Suppose only 1 in 100 emails is spam. A lazy "filter" that labels every email as not spam would be right 99% of the time, and completely useless, because it never catches a single spam message.

So data scientists look at the kinds of mistakes. For a yes-or-no prediction, there are four possible outcomes:

Model says yesModel says no
Really yesTrue positiveFalse negative
Really noFalse positiveTrue negative
  • A false positive is a false alarm: the model says yes when the answer is no, such as an important email from your teacher landing in spam.
  • A false negative is a miss: the model says no when the answer is yes, such as a scam email reaching your inbox.

This table is often called a confusion matrix.

Step 2 of 10Practice · Diagram

The confusion matrix for a spam filter

Your teacher's homework email lands in the spam folder. Which box is that?

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

The confusion matrix for a spam filterTwo-by-two matrix: True positive, False positive, False negative, True negative.True positiveFalse positiveFalse negativeTrue negativeWhat the email really isSpamNot spamWhat the model saysNot spamSpam
True positive

Spam correctly caught.

False positive

A real email, such as one from your teacher, sent to spam.

False negative

A scam email that reaches your inbox.

True negative

A normal email delivered normally.

Step 3 of 10Practice · Sort into groups

A model checks emails for spam, where "positive" means the model says spam. Classify each outcome.

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

Step 4 of 10Learn

Which mistake is worse?

Most models can be tuned to be more or less cautious. Make a spam filter stricter and it catches more spam (fewer false negatives) but also flags more real emails (more false positives). Make it looser and the reverse happens. You usually can't reduce both to zero at the same time, so you have to decide which mistake costs more.

That depends entirely on the situation:

  • Medical screening for a serious disease: a false negative means a sick person is told they're healthy and may miss treatment. A false positive means a healthy person gets a follow-up test. Doctors usually prefer to accept more false positives.
  • Spam filtering: a false positive might hide an important message, like a job offer or a school notice. Many email services lean toward letting some spam through rather than losing real mail.
  • Face recognition used by police: a false positive could lead to an innocent person being suspected. The cost is so serious that many people argue such systems need very high standards, human review or limits on use.

This is why "Is the model accurate?" is never the full question. Ask instead: "Which mistakes does it make, how often, for whom, and what happens to people when it's wrong?" Error rates can also differ between groups of people, which connects directly to fairness.

Step 5 of 10Compare

Which mistake costs more?

AspectCost of a false positiveCost of a false negativeUsually lean toward
Screening for a serious diseaseWorry and an extra testA sick person misses treatmentCatching more cases
Spam filterAn important email is missedOne annoying spam messageFewer false alarms
Flagging essays as AI-writtenAn honest student is accusedSome AI use goes unnoticedFewer false accusations, plus human review

Step 6 of 10Try the calculator

Model results calculator

Enter the four outcome counts from testing a yes-or-no model to see its accuracy and how many real yes cases it caught.

Accuracy
98.5 %
Share of real yes cases caught
88.9 %
Share of yes predictions that were right
80 %

Step 7 of 10Practice · Work it out

A model is tested on 200 photos and gets 170 right. What is its accuracy as a percentage?

Step 8 of 10Note

Step 9 of 10Practice · Code challenge

Write accuracy(predictions, labels) that returns the fraction (from 0 to 1) of positions where the prediction equals the label. Both arguments are arrays of the same length. Return 0 for empty arrays.

Runs in a sandbox in your browser.
  • accuracy(["spam", "ok", "ok", "spam"], ["spam", "ok", "spam", "spam"])
  • accuracy([1, 1, 0], [1, 1, 0])
  • accuracy([0, 0], [1, 1])
  • accuracy([], [])

Step 10 of 10Practice · What would you do?

What would you do?

A school tests an AI tool that flags essays as possibly AI-written. In testing, it flags 1 out of every 20 essays that students wrote entirely themselves. The principal wants to automatically give a zero to every flagged essay. What is the strongest concern?