Sub topic 3
Reading Confusion Matrices and Per-Class Metrics
Assume you know basic Python, pandas, NumPy, accuracy, precision, recall, F1, and train/validation/test splits. This chapter uses a multiclass text classifier, but the same ideas apply to binary classification.
Why one score is not enough
An aggregate score combines every prediction into one number. It can hide systematic failures. For example, a sentiment model might classify positive reviews well while often confusing neutral reviews with negative ones.
A confusion matrix records those mistakes. Each row represents the true class, and each column represents the predicted class.
Predicted
neg neu pos
True neg 2 1 0
True neu 0 1 2
True pos 1 0 3The diagonal contains correct predictions. Every off-diagonal cell represents a particular kind of confusion. In this example, two neutral items were predicted as positive, while one positive item was predicted as negative.
Precision, recall, F1, and support
For one class, treat that class as positive and all other classes as negative.
- Precision is the fraction of items predicted as a class that truly belong to it.
- Recall is the fraction of items from a class that the model finds.
- F1 is the harmonic mean of precision and recall. It is high only when both are high.
- Support is the number of true examples for that class.
| Metric | Question it answers | Sensitive to |
|---|---|---|
| Precision | When the model predicts this class, how often is it right? | False positives |
| Recall | Of the real examples of this class, how many did it find? | False negatives |
| F1 | How well does it balance precision and recall? | Both |
| Support | How many real examples are in this class? | Sample size |
A class with low recall is often being missed. A class with low precision is often receiving items that belong elsewhere. Always read support beside the metric. A score based on five examples is less stable than one based on thousands.
Compute and read the report
The labels argument fixes the order of rows and columns. Use the same order when interpreting the matrix and report.
from sklearn.metrics import classification_report, confusion_matrix
y_true = ["pos", "neg", "neu", "pos", "neg", "neu", "pos", "neg"]
y_pred = ["pos", "neg", "pos", "pos", "neu", "neu", "neg", "neg"]
labels = ["neg", "neu", "pos"]
matrix = confusion_matrix(y_true, y_pred, labels=labels)
print(matrix)
report = classification_report(
y_true,
y_pred,
labels=labels,
zero_division=0,
)
print(report)The report also includes macro and weighted averages. Macro F1 gives every class equal weight, so it exposes poor performance on rare classes. Weighted F1 weights each class by support, so common classes influence it more. Weighted F1 can look strong while a small but important class performs poorly.
> callout note > Do not infer the error direction from a metric alone. Use the confusion matrix to see which classes are confused, then inspect examples from those cells.
Common interpretation errors
- Reversing rows and columns. Confirm the library's convention before explaining a cell.
- Reporting only accuracy. Include per-class metrics and support.
- Comparing macro and weighted averages without checking class imbalance.
- Treating a low-support score as a certain pattern.
- Using a different label order for different evaluations.
- Setting
zero_division=0and forgetting that an undefined metric may indicate no predictions for a class.
Exercise
A class has precision 0.90 and recall 0.40. Is the model better at avoiding false positives or at finding all real examples? What should you inspect next?
Show answer
It is better at avoiding false positives than finding all real examples. Inspect the confusion-matrix row for that class to see which real examples are being missed.