Sub topic 13
Reading a multiclass matrix
This chapter assumes you can read a binary confusion matrix and compute precision and recall from TP, FP, FN and TN. It also assumes you know that scikit-learn puts actual labels on rows and predicted labels on columns. The Python snippets are short.
From two classes to many
A binary matrix is 2 x 2. With K classes the matrix is K x K. Each row is an actual class, each column is a predicted class, and each cell counts the samples with that actual and predicted pair.
The diagonal holds correct predictions. Every off-diagonal cell is an error, and its position tells you which kind. The cell in row "bird" and column "cat" counts birds that the model called cats.
A worked example
from sklearn.metrics import confusion_matrix
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"]
cm = confusion_matrix(y_true, y_pred, labels=labels)
print(cm)
# [[3 1 0]
# [0 2 1]
# [0 0 3]]Passing labels= fixes the row and column order. Without it, scikit-learn sorts the labels that appear in the data, which can shift the layout between runs.
| Actual \ Predicted | bird | cat | dog |
|---|---|---|---|
| bird | 3 | 1 | 0 |
| cat | 0 | 2 | 1 |
| dog | 0 | 0 | 3 |
Reading it row by row:
- One bird was mistaken for a cat.
- One cat was mistaken for a dog.
- No dog was misclassified, and nothing was ever predicted as "bird" wrongly.
Per-class metrics from one matrix
For any class, treat it as the positive class and lump all other classes together as negative. This is called one-vs-rest. Then:
- Recall for a class is its diagonal cell divided by its row sum.
- Precision for a class is its diagonal cell divided by its column sum.
For "cat", TP is 2. The row sum is 3, so recall is 2/3. The column sum is 3 (one bird plus two cats), so precision is 2/3. For "dog", precision is 3/4 because one cat landed in the dog column, and recall is 3/3.
# Each row sums to 1, so the diagonal holds per-class recall.
print(confusion_matrix(y_true, y_pred, labels=labels, normalize="true"))
# [[0.75 0.25 0. ]
# [0. 0.66666667 0.33333333]
# [0. 0. 1. ]]With normalize="true" each row is divided by its total. With normalize="pred" each column is divided by its total, and the diagonal then holds per-class precision.
A reading routine
Step 4 is where the matrix helps most. A large off-diagonal cell names a specific confusion, such as two classes with similar features or overlapping labeling rules. That points to a concrete fix, like more training examples of one class or clearer label guidelines. A single accuracy number cannot do that.
Common mistakes
- Reading rows as predictions when the tool puts predictions on rows. Some libraries and plotting tools transpose the matrix.
- Comparing raw counts across classes of different sizes. A class with 1,000 samples will have bigger numbers than one with 20. Normalize by row first.
- Ignoring rare classes. Overall accuracy can stay high while a small class has near-zero recall.
Once you have per-class precision, recall and F1, you still need to combine them into one number. That is the job of macro, micro and weighted averaging, covered in the next chapter.
Exercise
A 3-class matrix (rows actual, columns predicted) is [[8, 1, 1], [2, 6, 2], [0, 0, 10]] for classes A, B, C. What are the recall and precision of class B?
Show answer
Recall for B is the diagonal cell over the row sum: 6 / (2 + 6 + 2) = 0.6. Precision for B is the diagonal cell over the column sum: 6 / (1 + 6 + 0) = 6/7, about 0.857.