← AI Terminology
Confusion Matrix
A confusion matrix is a table that shows the counts of true positives, true negatives, false positives, and false negatives for a classification model — breaking down exactly where and how the model makes mistakes.
It is the diagnostic foundation for every other classification metric: Precision, Recall, F1, and Accuracy are all derived from it.
It is the diagnostic foundation for every other classification metric: Precision, Recall, F1, and Accuracy are all derived from it.
Why It Matters in AI
Accuracy hides the composition of errors. A cancer screening model with 95% accuracy might be classifying nearly all tumours as benign (low recall) — a fatal failure not visible in a single number. The confusion matrix exposes this: you see not just how often the model is right, but what kinds of mistakes it makes and which classes confuse it. This guides both debugging and threshold setting.
Key Points
| Aspect | Description |
|---|---|
| FN | False Negative — model predicted negative, actually positive (Type II error) |
| FP | False Positive — model predicted positive, actually negative (Type I error) |
| TN | True Negative — model predicted negative, actually negative |
| TP | True Positive — model predicted positive, actually positive |
| Multiclass | N×N matrix for N classes — diagonal = correct predictions; off-diagonal = which classes confuse |
| Derived metrics | Precision = TP/(TP+FP), Recall = TP/(TP+FN), F1 = harmonic mean of both, Accuracy = (TP+TN)/total |
Simple Analogy
A spam filter's confusion matrix shows: emails correctly flagged spam (TP), legitimate emails let through (TN), legitimate emails wrongly blocked (FP — annoying), and spam emails not caught (FN — dangerous). Each quadrant represents a different kind of error with different consequences.
Common Usage Examples
sklearn.metrics.confusion_matrix(y_true, y_pred)— returns the raw count matrixsklearn.metrics.ConfusionMatrixDisplay.from_predictions(y_true, y_pred)— plot it- Adjusting classification threshold to shift between FP and FN rate depending on cost of each error
- Multiclass: 10×10 confusion matrix on MNIST reveals which digit pairs are hardest to distinguish
seaborn.heatmap(cm, annot=True, fmt='d')— visual confusion matrix with colour coding
Summary
In short: The confusion matrix shows not just whether a classifier is right, but exactly how and where it goes wrong — the essential diagnostic before reporting any single metric.