In plain words
Saying a model is 90% accurate hides a lot. Which 10% did it get wrong? Did it mistake cats for dogs, or dogs for cats? Did it miss the rare cases that matter most?
A confusion matrix answers that with a grid. Each row is what something really was. Each column is what the model said it was. Every test example lands in one cell. The cells running diagonally, where the real answer and the guess match, are the correct ones. Every cell off the diagonal is a particular kind of mistake, and its number tells you how often it happened.
For a yes or no question, the grid has four cells: correct yeses, correct nos, false alarms (false positives) and misses (false negatives). Measures like precision and recall are worked out straight from those four numbers.
Everyday example
A fruit sorting machine
A market tests a machine on 30 fruits: 10 mangoes, 10 oranges, 10 lemons. It sorts 23 correctly. The grid shows where the other 7 went: one mango was called an orange, one orange a mango, two oranges were called lemons, and three lemons were called oranges. Now the fix is obvious: the machine struggles to tell lemons from oranges, so that is where it needs more examples.
Where the example breaks: The fruit machine's mistakes are easy to see on a stall. For a real model, the grid only counts mistakes; it does not explain why they happen, and it only describes the test examples it was given.
How it actually works
Google's glossary defines it as "an NxN table that summarizes the number of correct and incorrect predictions that a classification model made." N is the number of categories.
Rows and columns
Here rows are the true category and columns the predicted one, as in scikit-learn. Some textbooks and courses swap them, so always check the axis labels before reading one.
The four binary cells
True positive (TP): said yes, really yes. False positive (FP): said yes, really no, a false alarm. False negative (FN): said no, really yes, a miss. True negative (TN): said no, really no. In statistics, FP and FN are called type 1 and type 2 errors.
Metrics from the cells
Accuracy = (TP + TN) ÷ all. Precision = TP ÷ (TP + FP). Recall = TP ÷ (TP + FN). The matrix holds everything needed to compute them, which is why it is the starting point for judging a classification model.
Spotting patterns
With many categories, large off-diagonal numbers show which pairs the model confuses, pointing to where it needs better features or more labelled examples.
Always on unseen data
Build it from a test set the model did not train on. A matrix from training data looks far better than the model really is.
- Why the name
- It shows whether the system is confusing two classes
- Also called
- Error matrix; in unsupervised learning, a matching matrix
- Size
- One row and one column per category: 2 × 2 for yes or no questions
- Google's example
- A tumour model: 18 true positives, 1 false negative, 6 false positives, 452 true negatives
Questions people ask
What is a confusion matrix in simple terms?
A grid comparing what things really were with what a model said they were. The diagonal holds the correct answers, and every other cell counts one specific kind of mistake.
What is the difference between a false positive and a false negative?
A false positive is a false alarm: the model says yes but the answer is no, like a real email sent to spam. A false negative is a miss: the model says no but the answer is yes, like spam reaching your inbox.
How do you read a confusion matrix?
Check which axis is actual and which is predicted. Then read along the diagonal for correct predictions. For any off-diagonal cell, its row tells you what the item really was and its column what the model wrongly called it.
How do you calculate precision and recall from a confusion matrix?
Precision is true positives divided by everything predicted positive (TP + FP). Recall is true positives divided by everything actually positive (TP + FN). See precision and recall.