Sub topic 1
Why aggregate scores are not enough
A single accuracy number tells you how often a model is right overall. It does not tell you which inputs it gets wrong, or whether the errors fall on one class, one kind of text, or one group of users. This chapter shows how to look past the headline score, read the per-class breakdown, and collect errors into a table you can inspect.
Prerequisites
You should be comfortable with:
- Basic Python and the pandas and NumPy libraries
- Having trained or evaluated a text classifier at least once
- Accuracy, precision, recall, and F1
- Train, validation, and test splits
Choosing metrics and setting a baseline
Pick metrics that match the task. For a balanced multi-class task, accuracy and macro-F1 are a reasonable pair. For a rare class, precision and recall for that class matter more than accuracy.
Always compare against a baseline. A majority-class predictor or a simple keyword rule shows what a trivial system scores. If your model barely beats that baseline, the aggregate score is hiding a weak model.
Reading confusion matrices and per-class metrics
A confusion matrix counts how often each true label received each predicted label. Rows are true labels and columns are predicted labels. The diagonal holds correct predictions.
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"]
print(confusion_matrix(y_true, y_pred, labels=labels))
print(classification_report(y_true, y_pred, labels=labels, zero_division=0))The matrix shows that "neu" is often predicted as "pos", a pattern that one accuracy number would not reveal. Precision answers "of the items predicted as this class, how many were right?" Recall answers "of the items truly in this class, how many did we find?" Read both per class before you trust the model.
Collecting errors into an inspectable table
Put each misclassified example in a table with its text, true label, predicted label, and model confidence. Sorting by confidence puts the confident mistakes first, which often teach the most.
import pandas as pd
df = pd.DataFrame({
"text": ["great phone", "not bad at all", "battery died in a day",
"fine I guess", "love it"],
"label": ["pos", "pos", "neg", "neu", "pos"],
"pred": ["pos", "neg", "neg", "pos", "pos"],
"conf": [0.97, 0.81, 0.90, 0.64, 0.99],
})
errors = df[df["label"] != df["pred"]]
print(errors.sort_values("conf", ascending=False).to_string(index=False))> Note: Keep the full example table, not only the errors. Comparing correct and incorrect rows later makes slice analysis easier.
Exercise
In the confusion matrix above, why might a model that predicts "pos" for every input still reach a decent accuracy on this data?
Show answer
Because "pos" is the most frequent true label in the sample, so the model gets all the "pos" rows right. Accuracy rewards the majority class, while per-class recall for "neg" and "neu" would be zero.