Accuracy as a Classification Metric
Accuracy gives you one number summarizing how often a classifier is right.
Why One Accuracy Number Falls Short
Accuracy gives you one number summarizing how often a classifier is right. That number is useful, but it compresses everything the model did into a single value. Two classifiers can earn the same accuracy while making very different kinds of mistakes, and the single number cannot tell them apart. A confusion matrix opens that number up so you can see which kinds of mistakes a classifier makes.
We start from a realistic situation. 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 matrix of counts.
The output is a grid of four numbers. Before reading further, try to guess what the grid is organized around. Which two pieces of information is it counting against each other? Keep your guess in mind, because the next section answers it directly.
Reading the Grid's Orientation
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 places the four cells in their matrix positions. The top row is the not-nine true class and the bottom row is the nine true class. The left column is the not-nine prediction and the right column is the nine prediction. Once you hold that grid in mind, reading the counts becomes a single question: 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.
Naming the Four Cells
Binary classification is easier to discuss once one class is designated as the positive class. In the nine-versus-rest task, declaring a nine 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.
The names are 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 said negative, and the mistake is that the sample was actually positive. Read this way, the two correct cells are the true positives and true negatives on the diagonal, and the two mistake cells are the false positives and false negatives off the diagonal.
The first word of a cell name tells you whether the model was right. The second word tells you what the model predicted. The true class is given by the row, and it is the row, not the name, that tells you what the sample actually was.
Finding Where the Model Diverges
Now consider the question the matrix is meant to answer: where does 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 need only the row for true nine samples and the column for predicted not-nine samples, which is the bottom-left cell. That cell is a false negative count, and it is the single place where a nine was reported as something else.
Locating the divergent cell in an illustrative test set
An illustrative test set has 100 samples: 10 true nines and 90 true not-nines. The classifier's predictions are summarized by the matrix rows [85, 5] for true not-nines and [4, 6] for true nines. Which cell is the divergent cell for missed nines, and what does it count?
- 1Identify the row
Missed nines all have a true class of nine, so we use the bottom row, the true nine row, which reads [4, 6].
- 2Identify the column
A missed nine was predicted as not-nine, so we use the left column, the predicted not-nine column.
- 3Read the cell
The bottom-left cell is 4. This is the false negative count.
- 4Interpret the count
Four samples that truly were nines were classified as not-nine. These are the missed nines.
The bottom-left cell, with value 4, counts the nine-images the classifier missed.
The worked example shows a useful habit. Each off-diagonal cell answers one specific question. The top-right cell counts the not-nines the model called nines, and the bottom-left cell counts the nines the model called not-nines. Before trusting any summary, check which of these cells is largest, because the largest mistake often matters most for the task.
Computing Accuracy from the Matrix
The matrix gives you 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, and the number of all samples is the sum of every cell in the matrix. The correct predictions sit on the main diagonal, so accuracy is the diagonal sum divided by the total of all entries.
- 1The numerator is TP + TN, the two diagonal cells where the prediction matched the true class.
- 2The denominator is the sum of all four cells, which counts every sample in the test set exactly once.
- 3The fraction is therefore the share of samples that landed on the diagonal.
Computing accuracy for the illustrative matrix
Using the illustrative matrix with TN = 85, FP = 5, FN = 4, and TP = 6, what is the accuracy?
- 1Sum the diagonal
TP + TN = 6 + 85 = 91 correct predictions.
- 2Sum all entries
85 + 5 + 4 + 6 = 100 samples in total.
- 3Divide
Accuracy = 91 / 100 = 0.91.
The accuracy is 0.91, which is a single number that hides the fact that 4 of the 10 nines were missed.
Why Accuracy Alone Can Mislead
The matrix becomes most valuable when you compare models side by side. A useful comparison computes a confusion matrix for several fitted models using the same true labels, so each matrix can be read against a common reference. The source example compares two dummy baselines, a decision tree, and a logistic regression.
Reading across those matrices exposes behavior that a single score would not. The most frequent class model never produces a positive prediction, so its true positive count is zero and all 47 of its positive samples become false negatives. 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: more true positives and true negatives, and fewer false positives and false negatives.
Common mistake Reading rows as predicted classes and columns as true classes
What it looks likeTreating the bottom-left cell as predicted nine but actually not-nine.
Why it failsRows are tied to the true class and columns to the predicted class, so the labels run the other way.
Do this insteadRead the row first to find the true class, then the column to find the prediction.
Common mistake Assuming the diagonal always means the model is good
Why it failsThe diagonal counts correct predictions in total, but a model can have a large diagonal and still miss an entire class.
Do this insteadCheck each off-diagonal cell separately to see which kind of mistake dominates.
Common mistake Confusing a false positive with a false negative because the first word of the name looks similar
Common mistake Reporting accuracy without the matrix when the classes are imbalanced
Checking Your Reading of a Matrix
Using the illustrative matrix with rows [85, 5] for true not-nines and [4, 6] for true nines, identify the cell that counts nines the model called not-nine, state its name, and compute the accuracy. Then explain why the accuracy alone does not reveal this error.
Hints
- Find the true nine row first, then the predicted not-nine column.
- The name starts with False or True depending on whether the model was right, and ends with Negative or Positive for what it predicted.
- Add the diagonal values and divide by the sum of all four cells.
Key Takeaways
- A confusion matrix is a two-by-two array where rows are the true classes and columns are the predicted classes, so each entry counts one specific kind of outcome.
- The main diagonal holds the true positives and true negatives, which are correct classifications, while the off-diagonal cells hold the false positives and false negatives.
- Each cell name combines two judgments: whether the prediction was correct (true or false) and what the model output (positive or negative).
- Accuracy equals the diagonal sum divided by the sum of all entries, but two models with the same accuracy can make very different errors, which the individual cells reveal.