Confusion Matrix: Read TP, FP, TN, and FN
A confusion matrix shows how a classification model’s predictions compare with the correct labels: true positives (TP), false positives (FP), true negatives (TN), and false negatives (FN). To read it, first identify which class is treated as “positive,” then match actual labels against predicted labels; the four cells reveal what the model got right and which errors it made.
What a Confusion Matrix Shows
A confusion matrix is a table for evaluating a classifier, such as a spam detector, fraud flagger, or image model that labels items as cat or not cat. It compares predictions with known outcomes in a labeled evaluation dataset. “Positive” and “negative” are names for the two classes in a binary task; they do not mean good and bad, or that a numerical value is above or below zero.
For a binary classifier, the usual layout puts actual classes in rows and predicted classes in columns:
- True positive (TP): The actual class is positive, and the model predicts positive.
- False positive (FP): The actual class is negative, but the model predicts positive.
- True negative (TN): The actual class is negative, and the model predicts negative.
- False negative (FN): The actual class is positive, but the model predicts negative.
“True” means the prediction agrees with the reference label; “false” means it does not. A positive prediction alone does not make a result a true positive: the actual class must also be positive.
How to Read the Four Cells
Imagine a spam classifier where “spam” is the positive class. If a message truly is spam and the classifier flags it as spam, that is a TP. If a legitimate message is flagged as spam, that is an FP. If a legitimate message is correctly left in the inbox, that is a TN. If spam gets through as ordinary mail, that is an FN.
One reliable way to interpret any matrix is to ask two questions for each cell: what was the actual label, and what did the model predict? Do not try to infer TP, FP, TN, and FN just from cell position. Some libraries display actual labels as rows, while others use columns or let you select label order. Check the axis labels and class ordering before reading the counts.
For example, with actual labels in rows and predictions in columns, the binary matrix might look like this:
- Actual positive, predicted positive: TP = 42
- Actual positive, predicted negative: FN = 8
- Actual negative, predicted positive: FP = 10
- Actual negative, predicted negative: TN = 140
The classifier correctly identifies 42 positive cases and 140 negative cases. It misses 8 positives and raises 10 false alarms. These counts have meaning only alongside the class definition, dataset, and evaluation setup.
Metrics You Can Derive
The matrix supports several metrics, each answering a different question. For the example above, there are 50 actual positive cases (TP + FN) and 150 actual negative cases (TN + FP).
- Accuracy: (TP + TN) / (TP + FP + TN + FN). Here, 182 / 200 = 91%. It measures the share of all predictions that are correct.
- Precision: TP / (TP + FP). Here, 42 / 52 is about 80.8%. Among items predicted positive, it measures the share that truly are positive.
- Recall (sensitivity): TP / (TP + FN). Here, 42 / 50 = 84%. Among actual positives, it measures the share the model finds.
- Specificity: TN / (TN + FP). Here, 140 / 150 is about 93.3%. Among actual negatives, it measures the share correctly identified.
- F1 score: 2 × precision × recall / (precision + recall). It combines precision and recall using their harmonic mean.
Accuracy can be misleading when classes are imbalanced. If only 1 in 100 transactions is fraudulent, a model that always predicts “not fraud” has 99% accuracy but finds no fraud at all. In that situation, inspect the confusion matrix and choose metrics based on the costs of missed positives and false alarms.
Choosing a Threshold and Positive Class
Many classifiers output a score or probability, then apply a threshold to turn it into a class prediction. Lowering the threshold often catches more actual positives, increasing recall, but can also produce more false positives and lower precision. Raising it often has the opposite effect. The exact result depends on the model and data, so compare thresholds on validation data rather than assuming one is universally best.
The positive class should be chosen to make the important outcome clear. For a defect detector, “defective” is often the useful positive class; for a spam filter, “spam” may be. If you reverse the positive class, the underlying predictions do not change, but the labels TP, FP, TN, and FN—and metrics such as precision and recall—are interpreted relative to the new choice.
For model selection, consider the real consequences of each error. A false negative could mean a missed defect; a false positive could mean an unnecessary manual inspection. There is no universally correct balance. Record the threshold, class mapping, and evaluation data so comparisons are reproducible.
Common Developer Mistakes
- Assuming a fixed layout: Read row and column labels, and verify the class order used by the library.
- Forgetting the positive label: State explicitly which class counts as positive, especially when labels are strings or numbers.
- Using training results as a final score: Evaluate on data kept separate from model fitting. Reusing training data can make performance look better than it generalizes.
- Reporting accuracy alone: Include metrics that reflect class imbalance and the practical cost of errors.
- Comparing raw counts across different datasets: A larger test set naturally tends to produce larger counts. Compare normalized rates as well, while keeping the sample sizes visible.
- Ignoring zero denominators: Precision is undefined when TP + FP is zero; recall is undefined when TP + FN is zero. Libraries may report a warning, a configured substitute, or a special value, so check the metric’s behavior.
A confusion matrix also measures agreement with the labels supplied to the evaluation, not necessarily with reality. Incorrect labels, a test set that does not represent deployment data, or data leakage can make its apparent results unreliable.
Multiclass Confusion Matrices
With more than two classes, the matrix has one row and column per class. Each diagonal cell counts correct predictions for that class. An off-diagonal cell counts cases from the row’s actual class that the model predicted as the column’s class, assuming actual labels are rows.
For example, a three-class image model may often confuse “wolf” with “dog” but rarely with “car.” The matrix can expose that pattern even when an overall score hides it. Per-class precision and recall can be calculated by treating each class as positive in turn and the others as negative; aggregate scores may then use macro, micro, or support-weighted averaging. These averaging methods are not interchangeable, so name the method when reporting a multiclass metric.
Frequently Asked Questions
What is the difference between a false positive and a false negative?
A false positive is a negative case incorrectly predicted positive. A false negative is a positive case incorrectly predicted negative. The difference is whether the model raises a false alarm or misses a positive.
Does “positive” mean the prediction is correct?
No. Positive names one class in the classification task. A positive prediction can be true or false depending on the actual label.
Which is more important, precision or recall?
It depends on the task. Precision matters when false alarms are costly; recall matters when missing positive cases is costly. Examine both, along with the costs and context of the errors.
Can a model have high accuracy but perform poorly?
Yes. On imbalanced data, a model can mostly predict the common class and still achieve high accuracy while failing to detect the rare class. Check the class counts and per-class metrics.
Why does my confusion matrix look transposed?
Libraries can place actual labels on rows or columns, and may order classes differently. Read the displayed axes and the API’s documented label order rather than relying on a memorized cell position.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.