Dashboard

What Is a Confusion Matrix? Why 97% Accuracy Can Fool You

A confusion matrix counts what a classifier got right and wrong, split by kind of mistake. Here is a spam filter that scores 97 percent and still disappoints.

Carlo Zuercher
Carlo Zuercher
Staff Engineer, Platform
28 October 20261 min read

A confusion matrix is a table that counts how often a classifier was right and wrong, split by the kind of mistake. It has one row for each true class and one column for each predicted class. For a yes-or-no question, it is a two-by-two grid with four numbers, and those four numbers tell you far more than a single accuracy score.

It is called "confusion" because it shows exactly which classes the model mixes up.

The four cells

Take a spam filter. Each email is either spam or not spam, and the filter guesses one of the two. That gives four possible outcomes:

Predicted not spam

Predicted spam

Actually not spam

True negative (TN)

False positive (FP)

Actually spam

False negative (FN)

True positive (TP)

  • True positive: it said spam, and it was spam.

  • True negative: it said not spam, and it was not.

  • False positive: it said spam, but it was a real email. A good message got buried.

  • False negative: it said not spam, but it was spam. Junk reached the inbox.

One warning about orientation. The scikit-learn documentation defines the matrix so that rows are true labels and columns are predicted labels, and it points out that other references, including Wikipedia, may transpose the axes. Always check which way a chart is drawn before you read it.

A worked example: 1,000 emails

Suppose you test a filter on 1,000 emails. Fifty are spam and 950 are real. The filter gets these results:

Predicted not spam

Predicted spam

Actually not spam

940

10

Actually spam

20

30

Now compute the usual measures.

Accuracy is the share it got right: (30 + 940) divided by 1,000, which is 97 percent.

That sounds excellent, and it is a trap. A filter that never flags anything, ever, would get 950 of 1,000 right, which is 95 percent. The 97 percent is only two points better than doing nothing.

The matrix exposes why. Of the 50 spam emails, the filter caught only 30.

Recall is how many of the real spam emails it caught: 30 divided by (30 + 20), which is 60 percent. Forty percent of spam walked straight past it.

Precision is how trustworthy a spam flag is: 30 divided by (30 + 10), which is 75 percent. One in four flagged emails was actually fine.

F1 combines the two into one number: 2 times 30 divided by (2 times 30 + 10 + 20), which is about 0.67.

The useful lesson is that accuracy is dominated by the large class. When one outcome is rare, like fraud, spam, or a disease, you need precision and recall, which are read straight from the matrix.

Which mistake costs more?

Precision and recall pull against each other, and the right balance depends on what a mistake costs.

Situation

Worse mistake

Favor

Spam filter for a work inbox

Burying a real client email (FP)

Precision

Screening for a fraud team that reviews alerts

Missing real fraud (FN)

Recall

Flagging abusive messages for human review

Missing abuse (FN)

Recall

Auto-deleting messages

Deleting a good one (FP)

Precision

If a human reviews the flags afterward, you can afford false positives and should chase recall. If the system acts on its own, false positives hurt, so chase precision.

Moving the threshold

Most classifiers output a confidence number, and you pick the cutoff above which something counts as "spam". Changing the cutoff moves counts between cells.

Lower the cutoff on our filter so it flags more. Imagine the new counts are 40 true positives, 10 false negatives, 40 false positives, and 910 true negatives.

  • Recall rises to 80 percent, since 40 of 50 spam emails are caught.

  • Precision falls to 50 percent, since half the flags are wrong.

  • Accuracy drops to 95 percent, back to the do-nothing baseline.

Which version is better depends entirely on your costs. That is the reason to read the matrix and not accuracy alone. The guide on setting a confidence threshold for an AI classifier walks through choosing the cutoff on your own data.

More than two classes

The idea scales. A classifier that sorts support tickets into billing, bug, and feature request produces a three-by-three table. The diagonal, top left to bottom right, holds correct answers. Everything off the diagonal is a confusion, and the biggest off-diagonal number tells you which two categories the model mixes up most. That is often a sign the categories themselves are fuzzy, not that the model is bad.

Using a confusion matrix when you build with AI

If you add an AI classifier to your own app, such as a ticket router or a content filter, build the matrix from a set of examples you labeled by hand. Do not reuse the data the model was trained on, or you will measure memory instead of skill. That mistake is a version of overfitting. Be aware of benchmark contamination too, where test examples leak into training data and inflate scores. And remember where the labels came from: the quality of any score is capped by the quality of training data. For the larger picture of how models are built and judged, see how AI models work.

FAQ

What is a confusion matrix in simple terms?

A table that counts a model's correct and incorrect predictions, split by type, so you can see exactly which mistakes it makes.

What is the difference between precision and recall?

Precision asks how many of the model's positive calls were right. Recall asks how many of the real positives the model found.

Why is accuracy misleading?

When one class is much bigger than the other, a model can score high accuracy by mostly guessing the big class. The matrix shows whether it finds the rare class at all.

How do you read a confusion matrix?

Check which axis is true and which is predicted. The diagonal is correct answers. Each off-diagonal cell is a specific kind of mistake.

What is a good F1 score?

It depends on the task and the cost of errors. Compare it with a simple baseline and with what your use case needs, not a universal target.

How did this land?

About the author

Carlo Zuercher
Carlo Zuercher

Staff Engineer, Platform

Carlo works on the platform that turns prompts into running apps. He writes the engineering deep dives and the changelog notes worth reading.

Share

Get the next post in your inbox

One email a month. Product updates, engineering posts, and the best of Built with Swarmz.

I agree to receive emails about AI building tips and Swarmz product news. Unsubscribe any time.