A confusion matrix breaks a classifier's predictions into four exact categories — correct and incorrect predictions for each class — and is the raw material every other classification metric in this section is built from.
The Four Categories
Diagonal cells (TP, TN) are correct; off-diagonal cells (FP, FN) are the two distinct ways to be wrong.
| Term | Meaning |
|---|---|
| True Positive (TP) | Predicted positive, actually positive — correct |
| False Positive (FP) | Predicted positive, actually negative — a "false alarm" |
| False Negative (FN) | Predicted negative, actually positive — a "miss" |
| True Negative (TN) | Predicted negative, actually negative — correct |
Worked Example
A fraud classifier tested on 100 transactions: 20 are actually fraudulent, 80 are legitimate. The model catches 15 of the 20 fraud cases (missing 5), and incorrectly flags 10 of the legitimate transactions.
This exact confusion matrix is reused throughout Accuracy, Precision, Recall and F1-Score to compute each metric consistently from the same numbers.
from sklearn.metrics import confusion_matrix
import numpy as np
# 20 actual positives (1), 80 actual negatives (0)
y_true = [1]*20 + [0]*80
y_pred = [1]*15 + [0]*5 + [1]*10 + [0]*70 # 15 correct positives, 5 missed, 10 false alarms, 70 correct negatives
cm = confusion_matrix(y_true, y_pred)
print(cm)
# [[70 10]
# [ 5 15]] -- scikit-learn's default row/column order: [TN, FP], [FN, TP]
Watch the row/column convention: scikit-learn's confusion_matrix() outputs \([[TN,FP],[FN,TP]]\) by default (negatives first) — different from the diagram above (positives first), which follows the more common textbook convention. Always check labels= and the axis order before reading off values.
Visualizing With a Heatmap
from sklearn.metrics import ConfusionMatrixDisplay
import matplotlib.pyplot as plt
ConfusionMatrixDisplay.from_predictions(y_true, y_pred)
plt.show()
Multi-Class Confusion Matrices
For more than 2 classes, the matrix expands to \(C \times C\) — each row is an actual class, each column a predicted class, and the diagonal represents correct predictions per class. Off-diagonal cells reveal exactly which classes get confused with which others, information a single accuracy number completely hides.
Practical Use Cases
- Understanding how a classifier is wrong, not just how often — critical for deciding whether false positives or false negatives are the bigger business problem
- The direct source data for every classification metric on this page's related notes
Common Mistakes
- Misreading scikit-learn's row/column order — always explicitly check, rather than assuming a particular convention.
- Looking only at overall accuracy from the matrix without examining which specific error type (FP vs FN) dominates.
Interview Relevance
Q: "Why is a confusion matrix more informative than accuracy alone?" It separates the two distinct ways a classifier can be wrong — false positives and false negatives — which usually carry very different real-world costs; accuracy collapses both into a single number that hides which specific error type is actually happening.
Practice Question
A medical test's confusion matrix shows FN=2, FP=40, out of 100 actual negatives and 10 actual positives. Which error type is the model making far more often, and would that be more or less acceptable for a screening test?