Confusion Matrix: Read It Right in 4 Simple Steps

A confusion matrix is the single most useful table in classification. It lays out exactly where your model was right, where it was wrong, and โ€” crucially โ€” what kind of mistakes it made. Almost every metric you have heard of (accuracy, precision, recall, F1, ROC-AUC) is calculated directly from its four numbers.

confusion matrix metrics chart

๐Ÿ“Œ
In one line. A confusion matrix compares predicted vs actual labels in a grid of four counts โ€” TP, FP, TN, FN โ€” and every classification metric is just a ratio of those four numbers.

Try it: the interactive confusion matrix calculator

Enter your four counts and every metric updates instantly. The example is pre-filled with a disease test on 100 patients.

What is a confusion matrix?

A confusion matrix compares your model's predicted labels against the actual labels. For a binary classifier โ€” spam vs not spam โ€” it is a 2×2 grid:

Predicted: PositivePredicted: Negative
Actual: PositiveTrue Positive (TP)False Negative (FN)
Actual: NegativeFalse Positive (FP)True Negative (TN)
๐Ÿ’ก
Reading trick. The second word is what the model predicted; the first word says whether it was correct. So a "False Positive" = predicted Positive, and it was false (wrong).

The four outcomes explained

  • True Positive (TP) โ€” predicted Positive, actually Positive. (Flagged real spam.)
  • True Negative (TN) โ€” predicted Negative, actually Negative. (Let a real email through.)
  • False Positive (FP) โ€” predicted Positive, actually Negative. A false alarm (Type I error).
  • False Negative (FN) โ€” predicted Negative, actually Positive. A miss (Type II error).

A worked example

A model detects a disease in 100 patients โ€” 20 are truly sick, 80 are not. It produces TP = 18, FN = 2, FP = 5, TN = 75 (the values pre-filled in the calculator above), giving these headline metrics:

93%Accuracy
78%Precision
90%Recall
84%F1 Score
โš ๏ธ
The accuracy trap. This model is 93% accurate โ€” but for a disease affecting 1 in 100 people, a lazy model that predicts "healthy" for everyone would score 99% while catching zero sick patients. That is the accuracy paradox, and it is exactly why the confusion matrix (and the metrics below) matter.

Metrics derived from the confusion matrix

Every core metric is a ratio of these four cells:

Accuracy = (TP + TN) / total  โ€ข  Precision = TP / (TP + FP)  โ€ข  Recall = TP / (TP + FN)  โ€ข  F1 = 2ยท(PยทR)/(P+R)

For the example: precision = 78%, recall = 90%. In a medical setting you care most about recall โ€” you must not miss sick patients โ€” which the raw 93% accuracy completely hid.

Multi-class confusion matrices

For more than two classes (cat / dog / bird) the matrix grows to N×N, with actual classes as rows and predicted as columns. The diagonal holds correct predictions; each off-diagonal cell shows a specific confusion. You then compute precision and recall per class and average them. Learn more at the Wikipedia confusion matrix article.

Computing it in Python

from sklearn.metrics import confusion_matrix, classification_report

y_true = [1, 0, 1, 1, 0, 1, 0, 0]
y_pred = [1, 0, 1, 0, 0, 1, 1, 0]

print(confusion_matrix(y_true, y_pred))
print(classification_report(y_true, y_pred))
๐Ÿšซ
Common mistakes. Don't swap rows/columns (scikit-learn uses actual = rows); don't judge by accuracy alone on imbalanced data; and always choose the metric that matches the real cost of each error.

Frequently asked questions

What is the difference between a false positive and a false negative?
A false positive is a false alarm (predicted Positive, truth Negative). A false negative is a miss (predicted Negative, truth Positive). Which is worse for your problem drives your whole evaluation strategy.
Is a confusion matrix only for binary classification?
No โ€” it extends to any number of classes as an N×N grid, with the diagonal holding correct predictions.
Which metric should I optimise?
It depends on error costs: recall when misses are costly, precision when false alarms are costly, and F1 for a single balanced number. See the full classification metrics guide.

Related guides

Scroll to Top