The Confusion Matrix
A table of correct and incorrect predictions per class — the source of most metrics.
A confusion matrix lays out predictions against reality. For two classes it has four cells:
| Predicted No | Predicted Yes | |
|---|---|---|
| Actual No | True Negative | False Positive |
| Actual Yes | False Negative | True Positive |
Accuracy, precision and recall are all computed from these four counts.
Example
from sklearn.metrics import confusion_matrix, classification_report
y_true = [1, 0, 1, 1, 0, 0, 1]
y_pred = [1, 0, 0, 1, 0, 1, 1]
print(confusion_matrix(y_true, y_pred))
# [[2 1]
# [1 3]]
print(classification_report(y_true, y_pred))When to use it
- A medical AI team inspects the confusion matrix of a cancer detector to ensure false negatives (missed cancers) are below an acceptable threshold.
- A spam filter developer analyses the confusion matrix to confirm that the false-positive rate (legitimate email flagged as spam) is near zero.
- An ML engineer uses the per-class breakdown in the confusion matrix to detect that a model performs well on majority classes but fails on a minority class.
More examples
Compute and print confusion matrix
Computes the raw 2x2 confusion matrix showing true positives, false positives, false negatives, and true negatives for a binary classifier.
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
X, y = load_breast_cancer(return_X_y=True)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.2, random_state=0)
model = RandomForestClassifier(random_state=0).fit(X_tr, y_tr)
preds = model.predict(X_te)
cm = confusion_matrix(y_te, preds)
print(cm)Plot the confusion matrix
Uses ConfusionMatrixDisplay to plot a colour-coded confusion matrix with class names, making misclassification patterns visually clear.
from sklearn.metrics import ConfusionMatrixDisplay
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
X, y = load_iris(return_X_y=True)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.25)
model = RandomForestClassifier().fit(X_tr, y_tr)
disp = ConfusionMatrixDisplay.from_estimator(
model, X_te, y_te, display_labels=load_iris().target_names)
disp.plot(cmap='Blues')
plt.title('Iris Confusion Matrix')
plt.show()Extract TP, FP, FN, TN directly
Unpacks the four cells of a binary confusion matrix with ravel() and manually computes precision and recall to show how these metrics derive from it.
from sklearn.metrics import confusion_matrix
y_true = [1, 0, 1, 1, 0, 0, 1, 0]
y_pred = [1, 0, 0, 1, 0, 1, 1, 0]
tn, fp, fn, tp = confusion_matrix(y_true, y_pred).ravel()
print(f'TP={tp} FP={fp} FN={fn} TN={tn}')
print(f'Precision={tp/(tp+fp):.2f} Recall={tp/(tp+fn):.2f}')
Discussion