Comparing Baseline and Trained Classifiers
Suppose a classifier reports an accuracy score and you are asked whether it is good.
One Number Hides Too Much
Suppose a classifier reports an accuracy score and you are asked whether it is good. A single number can tell you how often the model was right overall, but it cannot tell you which kinds of mistakes it makes. Two models with similar accuracy can fail in very different ways, and one of them may be missing the cases that matter most. A confusion matrix opens that single number up so you can see the individual outcomes that produced it.
In this article we start from a real evaluation. A logistic regression model has already produced predictions on a held-out test set, and those predictions are stored in a variable named pred_logreg. The true labels for the same test samples are in y_test. Passing those two arrays to confusion_matrix turns them into a grid of counts. Before reading the grid, it helps to know what it is organized around, so we will work through that next.
y_testholds the true labels for the test samplespred_logregholds the model's predicted labels for the same samples, in the same order- The result is a two-by-two array of counts, one entry per combination of true class and predicted class
Reading the Grid's Rows and Columns
The orientation of the matrix is the key to reading it. Each row is tied to the true class of a group of samples, and each column is tied to the class the model assigned to them. Every entry is a count of how many samples with a given true class received a given predicted class. The first row therefore describes everything that truly belonged to the not-nine group, split by what the model guessed. The second row describes everything that truly belonged to the nine group, split the same way.
The diagram above places the four cells in their matrix positions. The top row is the true not-nine class and the bottom row is the true nine class. The left column is the predicted not-nine class and the right column is the predicted nine class. Once you hold that grid in mind, reading any count is just a question of asking which true class and which predicted class meet in a given cell.
This layout gives the matrix a useful property. The two cells on the main diagonal, running from top-left to bottom-right, are the cases where the true class and the predicted class agree. Those are the correct classifications. The two off-diagonal cells are the mistakes, and each one names both the class a sample really belonged to and the class the model wrongly chose for it.
What do you think happens?
Before reading further: which two pieces of information is the grid counting against each other?
Reveal answer
The true class of each sample (the rows) against the class the model predicted for it (the columns).
Each row is tied to a true class and each column is tied to a predicted class, so every cell counts samples that had a specific true class and received a specific predicted class.
Naming the Four Cells
Binary classification becomes easier to discuss once one class is designated as the positive class. In the nine-versus-rest task, declaring a nine to be the positive class lets the four cells receive standard names. The names describe whether the model was right or wrong, and which class it predicted.
Notice that each name is built from two independent judgments. The first word says whether the prediction was correct, true or false. The second word says what the model output, positive or negative. A false negative is therefore a mistake in which the model output negative, and the mistake is that the sample was actually positive. A false positive is the reverse: the model output positive, and the sample was actually negative.
- 1The first word answers whether the prediction matched the true class, so it is true when the cell is on the main diagonal and false when it is off the diagonal
- 2The second word answers what the model output, so it reads positive for the right-hand column and negative for the left-hand column
- 3Combining the two judgments gives the name: a false negative is a wrong prediction that output negative
- 4Applied to the four cells: TN is correct and negative, FP is wrong and positive, FN is wrong and negative, TP is correct and positive
Finding Where the Model Diverges
Now consider the question the matrix is meant to answer: where did the model's output diverge from the true labels? Suppose you want to know how many nine-images the classifier missed entirely. You do not need to recount the test set. You only need the row for true nine samples and the column for predicted not-nine samples, which is the bottom-left cell. That cell is the false negative count, and it is the one location that directly answers the question.
Locating the Missed Nines
A classifier is evaluated on a test set. Using the nine-versus-rest setup, find the cell that counts nine-images the model missed.
- 1Identify the positive class
Nine is the positive class, so the question is about samples whose true label is positive that the model failed to flag.
- 2Pick the true-class row
The samples that truly are nines sit in the bottom row, the row for the true nine class.
- 3Pick the predicted-class column
The model missed them, which means it predicted not-nine. That is the left column.
- 4Read the intersection
The bottom-left cell is where true nine meets predicted not-nine, so it counts exactly the missed nines. Its name is false negative.
The bottom-left cell, the false negative count, gives the number of nine-images the classifier missed, without recounting the test set.
Comparing models side by side is where the matrix becomes most valuable. In the source comparison, a confusion matrix was computed for each of four fitted models: two dummy baselines, a decision tree, and the logistic regression. Each call used the same true labels, so the matrices could be read against a common reference. Reading across the results exposed behavior that a single score would not hide.
The most frequent class baseline never produces a positive prediction, so its true positive count is zero and its 47 positive samples all land in the false negative cell. The other dummy model does predict positives, but it produces only 4 true positives against 42 false positives, so most of its positive predictions are wrong. The decision tree is much more sensible, with 23 true positives. Logistic regression is better than the tree on every count, with more true positives and true negatives and fewer false positives and false negatives.
Inspecting full grids is enough to tell which model behaves reasonably in a small comparison like this one. Doing the same inspection for many models and many test sets is slow, which is why the outcomes are later condensed into summary measures. Accuracy is the simplest of those summaries.
Computing Accuracy From the Cells
The matrix gives a direct route to accuracy. Accuracy is the number of correct predictions divided by the number of all samples. In terms of the four outcomes, the correct predictions are the true positives and true negatives, which are the diagonal cells. The number of all samples is the sum of every cell in the matrix. Equivalently, accuracy is the diagonal sum divided by the sum of all entries.
- 1The numerator adds the two diagonal cells, the correct classifications: true positives and true negatives
- 2The denominator adds all four cells, which is the total number of samples in the test set
- 3The fraction is therefore the share of samples the model classified correctly, and it is one number that summarizes the whole matrix
Accuracy That Hides a Dominant Error
Illustrative matrix for 100 test samples, with nine as the positive class: TN = 50, FP = 5, FN = 10, TP = 35. Compute accuracy and identify which error dominates.
- 1Sum the diagonal
The correct predictions are TN plus TP, which is 50 plus 35, giving 85.
- 2Sum all entries
The total is 50 plus 5 plus 10 plus 35, which is 100, matching the number of samples.
- 3Divide
Accuracy is 85 divided by 100, which is 0.85.
- 4Compare the off-diagonal cells
FN is 10 and FP is 5, so the model misses nines twice as often as it raises false alarms. The single accuracy value does not show this.
Accuracy is 0.85, which looks strong, yet the false negative cell holds 10 samples, so the model found only 35 of the 45 true nines.
Mistakes Beginners Make With Matrices
Common mistake Reading the rows as predicted classes and the columns as true classes
What it looks likeLooking at the bottom-left cell and concluding it counts predicted nines that were actually not-nine
Why it failsRows are tied to the true class and columns are tied to the predicted class, so the bottom-left cell is true nine and predicted not-nine
Do this insteadRead the row label first to find the true class, then read the column label to find the predicted class
Common mistake Swapping the names of false positives and false negatives
What it looks likeCalling the top-right cell a false negative
Why it failsThe second word of the name records what the model output. The top-right cell is where the true class is not-nine and the model predicted nine, so the model output positive
Do this insteadName the cell by asking whether the prediction was correct and what the model output
Common mistake Judging a model by accuracy alone
What it looks likeAccepting a high accuracy without looking at the false negative cell
Why it failsAccuracy averages the diagonal against the whole grid, so a large count in one error cell can disappear into the total
Do this insteadRead every cell of the matrix, and check the error cell that matters most for the task
Common mistake Treating a zero in the true positive cell as a minor detail rather than a sign that the model never predicts the positive class
Practice Reading a Matrix
Using the illustrative matrix from the accuracy example, with TN = 50, FP = 5, FN = 10, and TP = 35, answer these questions: How many true nines did the model predict as not-nine? Which cell would you check to find false alarms, and what is the count? If a second model had TN = 52, FP = 3, FN = 10, TP = 35, would its accuracy be higher, and would it fix the missed nines?
Hints
- The missed nines are in the row for true nine, in the column for predicted not-nine
- False alarms are the cell where the true class is not-nine and the model predicted nine
- Compute accuracy for the second model by adding its diagonal and dividing by the sum of all four cells
Key Takeaways
- A confusion matrix is a two-by-two array in which rows correspond to true classes and columns correspond to predicted classes, so each cell counts one specific kind of outcome
- The diagonal cells hold correct classifications (true positives and true negatives), while the off-diagonal cells hold false positives and false negatives
- Each cell name combines two judgments: whether the prediction was correct, and whether the model output positive or negative
- Accuracy equals the diagonal sum divided by the sum of all entries, but two models with similar accuracy can make very different mistakes that only the individual cells reveal