What is a confusion matrix?

In short
A confusion matrix is a table that shows how often a classification model gets each answer right or wrong. Each row holds the true label, each column holds the predicted label, and each cell counts how many samples fall into that pair. It helps you see exactly which mistakes a model makes.
What the table means
When you teach a computer to sort things into groups, such as spam or not spam, you need a way to check how well it works. A confusion matrix is one of the simplest checks. It is a grid of counts. Each row stands for the group a sample really belongs to. Each column stands for the group the model guessed. The number in a cell tells you how many samples had that real group and got that guess.
So the cell in row i and column j counts the samples that truly belong to group i but were predicted as group j. When the model is right, the count lands on the diagonal, where row and column match. Counts off the diagonal are mistakes.
Reading a yes or no problem
The idea is easiest to see with two groups, often called positive and negative. The table then has four cells with names you will meet often:
- True negatives: the real answer was negative and the model said negative.
- False positives: the real answer was negative, but the model said positive.
- False negatives: the real answer was positive, but the model said negative.
- True positives: the real answer was positive and the model said positive.
A false positive is a false alarm. A false negative is a missed case. Which of these matters more depends on your problem. A medical test that misses an illness is usually worse than one that raises a false alarm, so you should look at both cells, not just the total.
A simple example
The scikit-learn library, a popular Python toolkit for machine learning, provides a function called confusion_matrix. You give it the true labels and the predicted labels, and it returns the table as an array:
from sklearn.metrics import confusion_matrix
y_true = [2, 0, 2, 2, 0, 1] y_pred = [0, 0, 2, 2, 0, 2]
print(confusion_matrix(y_true, y_pred))
The output is a three-by-three grid, because there are three classes. The library sorts the labels for you unless you pass your own order with the labels argument. For text labels such as cat and ant, the same function works the same way, and you can choose the row and column order yourself.
For a two-group problem, you can unpack the four counts directly with ravel, which flattens the grid so you can read tn, fp, fn and tp in that order.
Common mistakes
The most common mistake is mixing up rows and columns. Always check which axis holds the true labels before you read a result. In scikit-learn, rows are true labels and columns are predicted labels.
Another mistake is reading only the diagonal and calling the model good. A large diagonal is encouraging, but it can hide a problem if one class is much bigger than the others. A model that always guesses the largest class can look fine on raw counts while missing every rare case.
Finally, make sure the true and predicted labels line up sample by sample. If the two lists are in different orders, the matrix will be wrong without any error message.
Normalizing the counts
Raw counts are hard to compare when groups differ in size. The same function can divide the counts by row totals, by column totals, or by the whole sample count. Dividing by row totals shows, for each real group, the share that was predicted correctly. This makes it easier to compare classes of different sizes. The scikit-learn documentation describes these options under the normalize argument.
Alternatives and next steps
A confusion matrix shows the full picture, but it gives you many numbers. To turn it into a single score, you can compute measures such as accuracy, precision and recall from its four counts. Those scores are useful for reports, while the matrix is best for finding out where the model goes wrong.
FAQ
Is a confusion matrix only for binary classification?
No. It works for any number of groups. With three or more classes, the grid simply grows, with one row and one column per class.
Which axis holds the true labels?
In scikit-learn, rows hold the true labels and columns hold the predicted labels. Always check the convention of the tool you use, because some libraries flip it.
Why is the diagonal important?
The diagonal counts correct predictions, where the true and predicted group are the same. Numbers away from the diagonal show the mistakes the model makes.
Can I use a confusion matrix with text labels?
Yes. The labels can be words such as cat or ant. You can also pass a labels list to set the order of rows and columns.
Sources
This page is based on the official documentation below. Read it for full detail.
Confusion matrix — official documentation ↗Confusion matrix in the news
Anthropic Ports Claude API From Python to Rust Using Production Traffic as the Test
Anthropic says it rewrote its Messages API from Python to Rust without downtime, cutting CPU use by about 70%. The main challenge was verifying the new code, which it did by comparing live traffic against the old service.
