Confusion Matrix Explained: How to Read It and Find Model Errors

Confusion Matrix cover with four envelope trays illustrating correct and incorrect classifications.

An email filter gets 95 out of 100 messages right. That sounds reassuring until you learn it missed three of the ten spam messages and moved two legitimate messages to junk. A confusion matrix shows both mistakes, alongside the predictions the filter got right.

A confusion matrix is a table that counts how often each actual class was assigned to each predicted class. You can use it to see which labels a model confuses, calculate metrics such as precision and recall, and decide which examples to inspect first.

Start with the row and column labels

In this guide, rows are actual labels and columns are predicted labels, following scikit-learn’s convention. Some charts swap these axes, so read the axis labels before interpreting a cell. When both axes use the same class order, the main diagonal shows correct predictions. The other cells show mistakes. Scikit-learn confusion matrix reference.

A worked example with 100 emails

Actual labelPredicted legitimatePredicted spam
Legitimate88 true negatives2 false positives
Spam3 false negatives7 true positives

Here, spam is the positive class. A true positive is spam correctly flagged. A false positive is a legitimate email incorrectly flagged. A false negative is spam that slips through. A true negative is a legitimate email correctly delivered.

Read across the spam row to answer “What happened to all actual spam messages?” Read down the predicted-spam column to answer “What was really in the junk folder?” The two questions use different totals, which is why recall and precision can tell different stories.

Calculate the metrics from the same four counts

Accuracy is correct predictions divided by all predictions. Here, (88 + 7) ÷ 100 = 95%.

Precision is the share of predicted spam that was actually spam. Here, 7 ÷ (7 + 2) ≈ 77.8%.

Recall is the share of actual spam the filter caught. Here, 7 ÷ (7 + 3) = 70%.

F1 is the harmonic mean of precision and recall. Using the counts, it is 2 × 7 ÷ (2 × 7 + 2 + 3) ≈ 73.7%. Report precision and recall alongside it when the cost of the two errors differs. Scikit-learn classification metrics.

Because 90 of these 100 emails were legitimate, a filter that delivered everything would already score 90% accuracy. It would catch zero spam. Compare your model with a simple baseline and report how well it handles the class people care about.

Use counts and percentages together

In a multiclass confusion matrix, every class gets a row and a column. In the iris example below, compare the off-diagonal cells to see which flower species the model mixes up. The direction matters. Misclassifying class A as B can be more frequent than misclassifying B as A.

Iris classification counts show 13 versicolor flowers predicted as virginica and three correctly classified.
The count matrix shows 13 of 16 versicolor examples were predicted as virginica. Chart by the scikit-learn developers.

Raw counts tell you how many errors occurred. Normalize by actual class to divide each cell by its row total and see the fraction of that class assigned to each prediction. With a nonempty row, those fractions sum to one. The diagonal then gives recall for each class.

The normalized iris matrix shows approximately 81 percent of actual versicolor predicted as virginica.
The same error accounts for about 81% of the versicolor row. Chart by the scikit-learn developers.

Normalization helps compare a large class with a small one, but it also hides how many examples support the rate. Four mistakes among five examples and 400 among 500 both produce an 80% error rate. Show class counts alongside rates so readers can judge how much evidence each number represents.

Turn the errors into your next evaluation step

Inspect one off-diagonal cell first. Pull a sample of the underlying examples and check whether labels are consistent. In the email example, review the legitimate messages in the junk folder and look for a shared pattern, such as messages from a mailing list.

Check whether a threshold change fits the cost of errors. If your model uses a spam score, raising the threshold can keep more legitimate messages out of junk while allowing more spam through. Compare candidate thresholds on validation data and report the chosen setting on separate test data.

Compare like with like. Use the same held-out examples, class order, and normalization method when comparing two models. If you see a large training-to-validation gap, also investigate whether the model learned patterns that fail on new examples. Unlimited AI guide to overfitting.

Common questions

Can a confusion matrix have more than two classes?

Yes. A three-class classifier has a 3 × 3 matrix when each example has one actual and one predicted class. For multilabel tasks, where an item can have several labels, a separate binary matrix for each label can be more informative.

Does a confusion matrix show how confident the model was?

A matrix summarizes the decisions at the chosen threshold or decision rule. Two models can produce the same decisions with very different confidence scores. Review probabilities separately if the quality of those scores matters.

What if a class has no predicted positives?

Precision for that class has a zero denominator. Report the count and document how your evaluation software handles undefined values. It is also worth checking whether the class appeared in the test set and whether the model can predict it under the current rule.

When you report a confusion matrix, include the axis labels, class counts, evaluation split, and threshold if one was used. Then name the error that matters most and show the examples you would inspect to understand it.

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top