Sub topic 12
Normalizing by row, column or total
This chapter assumes you can read a raw confusion matrix and know that scikit-learn puts actual labels on the rows and predicted labels on the columns. You should also know precision and recall.
Why raw counts are hard to compare
Raw counts depend on how many examples each class has. A class with 4,000 examples looks "bigger" than one with 40, even if the model handles both equally well. Normalizing divides each cell by a total, so the cells become proportions between 0 and 1. The choice of total decides which question the matrix answers.
Three ways to normalize
| Option in scikit-learn | Divide each cell by | Each... sums to 1 | Diagonal shows |
|---|---|---|---|
| `normalize="true"` | its row total (actual count) | row | recall per class |
| `normalize="pred"` | its column total (predicted count) | column | precision per class |
| `normalize="all"` | the grand total | whole matrix | share of all samples |
Worked example
This uses the three-class animal data. Rows are actual labels and columns are predicted labels.
import numpy as np
from sklearn.metrics import confusion_matrix
np.set_printoptions(precision=2, suppress=True)
labels = ["bird", "cat", "dog"]
y_true = ["cat", "cat", "cat", "dog", "dog", "dog", "bird", "bird", "bird", "bird"]
y_pred = ["cat", "cat", "dog", "dog", "dog", "dog", "bird", "cat", "bird", "bird"]
print(confusion_matrix(y_true, y_pred, labels=labels))
for mode in ("true", "pred", "all"):
print(mode)
print(confusion_matrix(y_true, y_pred, labels=labels, normalize=mode))The raw matrix is [[3 1 0], [0 2 1], [0 0 3]]. The normalized versions are:
true:[[0.75 0.25 0], [0 0.67 0.33], [0 0 1]]. Each row is one actual class. The diagonal gives recall: 75% of birds, 67% of cats and 100% of dogs were found. The off-diagonal0.25says a quarter of birds were called cats.pred:[[1 0.33 0], [0 0.67 0.25], [0 0 0.75]]. Each column is one predicted class. The diagonal gives precision. When the model says "dog", it is right 75% of the time. The remaining 25% of dog predictions were really cats.all:[[0.3 0.1 0], [0 0.2 0.1], [0 0 0.3]]. All nine cells add up to 1. This shows the overall mix of outcomes but hides per-class quality, so it is the least often used.
Common errors
- Reading the wrong axis. Row normalization is only "recall" if rows are actual labels. If your tool puts predictions on the rows,
trueandpredswap meaning. Check the orientation first. - Expecting rows to sum to 1 after
pred. With column normalization, the columns sum to 1. The rows usually do not. - Dividing by zero. If a class is never predicted, its column total is 0. Scikit-learn returns 0 for that column instead of raising an error, so a column of zeros can mean "never predicted" as well as "always wrong". Check the raw counts.
- Losing sample size. A proportion of
1.0from 2 samples is much weaker evidence than0.95from 2,000. Keep the raw counts nearby.
Exercise
A binary matrix with actual labels on rows has raw counts [[40, 10], [5, 45]] in the order [negative, positive]. After normalize="pred", what is the value in the bottom-right cell, and which metric is it?
Show answer
The positive column total is 10 + 45 = 55, so the cell is 45 / 55, about 0.82. That is the precision of the positive class. Row normalization would give 45 / 50 = 0.90 instead, which is recall.