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.

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: Positive | Predicted: Negative | |
|---|---|---|
| Actual: Positive | True Positive (TP) | False Negative (FN) |
| Actual: Negative | False Positive (FP) | True Negative (TN) |
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:
Metrics derived from the confusion matrix
Every core metric is a ratio of these four cells:
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))Frequently asked questions
What is the difference between a false positive and a false negative?
Is a confusion matrix only for binary classification?
Which metric should I optimise?
Related guides
- Precision vs Recall โ the trade-off and when to prioritise each.
- F1 Score โ one balanced number from precision and recall.
- ROC Curve & AUC โ evaluating across all thresholds.
- Classification Metrics โ which metric to use and when.