Definition
A confusion matrix is a table that compares a classifier's predictions with the true labels. For two classes it has four counts: true positives, false positives, true negatives and false negatives.
In simple words
Think of a fire alarm. It rings during a fire: true positive. It rings when someone burns toast: false positive. It stays silent on a normal day: true negative. It stays silent during a fire: false negative, the dangerous one. A single accuracy number can't show you this, and this table can.
Why it matters
Different errors have different costs. Missing a cancer is far worse than a false alarm, and blocking a real customer's payment has a different cost from missing a fraud. All the common metrics are calculated from these four numbers.
How it works
| Predicted: Yes | Predicted: No | |
|---|---|---|
| Actually: Yes | True Positive (TP) | False Negative (FN): missed it |
| Actually: No | False Positive (FP): false alarm | True Negative (TN) |
The maths
100 patients: 10 have the disease. The model catches 8, and flags 5 healthy people wrongly.
TP = 8 FN = 2 FP = 5 TN = 85 Accuracy = (8 + 85) / 100 = 93%
93% sounds fine, but the model still missed 2 of 10 patients. A model that says "healthy" to everyone would score 90% and catch nobody. That's why accuracy alone can't be trusted on imbalanced data.
In code
from sklearn.metrics import accuracy_score, confusion_matrix
actual = [1] * 10 + [0] * 90
predicted = [1] * 8 + [0] * 2 + [1] * 5 + [0] * 85
tn, fp, fn, tp = confusion_matrix(actual, predicted).ravel()
print("TP:", tp, " FN:", fn, " FP:", fp, " TN:", tn)
print("Accuracy:", accuracy_score(actual, predicted))
everyone_healthy = [0] * 100
print("Always says healthy:", accuracy_score(actual, everyone_healthy))Output
TP: 8 FN: 2 FP: 5 TN: 85 Accuracy: 0.93 Always says healthy: 0.9
Common mistakes
- Judging a model on accuracy alone, particularly on imbalanced classes.
- Mixing up false positives and false negatives. Think about what each costs in your case.
Check yourself
Summary
- Four counts: TP, FP, TN and FN.
- All common classification metrics come from them.
- Accuracy can mislead when classes are imbalanced.
Finished reading?
Sources: Google Machine Learning Crash Course