Computing Library › Machine Learning
Machine Learning

Confusion Matrix

A confusion matrix tabulates predicted against actual classes, exposing exactly which errors a classifier makes.

The full breakdown

A confusion matrix is a table whose rows are actual classes and columns are predicted classes (or the reverse). Each cell counts how many examples of one actual class were predicted as another. It shows not just how often a classifier is wrong but precisely how it is wrong, which classes it confuses for which.

The binary case

Kronos motion — lego machine

For two classes the matrix has four cells: true positives (TP), false positives (FP), false negatives (FN), and true negatives (TN). Nearly every classification metric is a ratio of these four numbers, so the confusion matrix is the source from which the rest are derived.

Binary Confusion Matrix
TPFNFPTN

Metrics from the matrix

python
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_true, y_pred)
# rows = actual, columns = predicted

Reading the multiclass version

For K classes the matrix is K by K. A well-performing classifier has large numbers on the diagonal (correct) and small off-diagonal entries (confusions). Bright off-diagonal cells reveal systematic mistakes, for example two visually similar categories being swapped, which points directly at where more data or better features are needed. Normalizing each row to sum to 1 turns counts into per-class recall, easier to compare across imbalanced classes.

The confusion matrix is the first thing to inspect after training, before summarizing into precision, recall, and F1.