Sub topic 9
Measuring Accuracy and Macro F1 on a Labeled Sample
Assume you know basic Python, understand supervised text classification, and can install Python packages. In this chapter, you will evaluate a zero-shot classifier against a small set of hand-labeled messages. Evaluation shows whether the classifier works for your task instead of only producing plausible-looking scores.
Build a labeled sample
A labeled sample contains input texts and the correct label for each text. The correct labels are called the ground truth. Keep the candidate labels and their spelling consistent between prediction and evaluation.
For a single-label task, each text has exactly one correct category:
| Text | Ground-truth label |
|---|---|
| I was charged twice this month. | billing |
| The app freezes on the settings page. | technical support |
| Do you offer annual-plan discounts? | sales |
The example below uses three messages. A real evaluation should use many examples that represent the messages your system will receive.
Accuracy
Accuracy is the fraction of predictions that exactly match the ground truth:
accuracy = correct predictions / total predictionsIf the classifier gets two of three examples right, its accuracy is 0.667. Accuracy is easy to understand, but it can hide poor performance on smaller classes. For example, if 90 percent of messages are billing messages, a classifier that always predicts billing can achieve 90 percent accuracy while completely failing on sales and technical support.
Macro F1
F1 combines precision and recall for one label.
- Precision asks: among messages predicted as this label, how many really have it?
- Recall asks: among messages that really have this label, how many did the classifier find?
- F1 is the harmonic mean of precision and recall.
Macro F1 calculates F1 separately for every label and then gives every label equal weight. This makes it useful when classes have different sizes.
Run an evaluation
The following program predicts the highest-scoring candidate label for each text. It then computes both metrics.
from sklearn.metrics import accuracy_score, f1_score
from transformers import pipeline
classifier = pipeline(
"zero-shot-classification",
model="facebook/bart-large-mnli",
)
labels = ["billing", "technical support", "sales"]
texts = [
"I was charged twice for my subscription this month.",
"The app freezes every time I open the settings page.",
"Do you offer a discount for annual plans?",
"My invoice has the wrong amount.",
"The mobile application crashes at startup.",
"Can I upgrade to a business plan?",
]
y_true = [
"billing",
"technical support",
"sales",
"billing",
"technical support",
"sales",
]
y_pred = []
for text in texts:
result = classifier(text, candidate_labels=labels)
y_pred.append(result["labels"][0])
print("predictions:", y_pred)
print("accuracy:", accuracy_score(y_true, y_pred))
print("macro F1:", f1_score(y_true, y_pred, labels=labels, average="macro"))The labels in y_true must use the same names as the candidate labels. The labels argument makes the set of classes explicit, including classes that might receive no predictions.
For a trustworthy estimate, separate evaluation data from examples used to design prompts or labels. Expand the sample with difficult, ambiguous, and recent messages. Also inspect incorrect predictions instead of relying only on one number.
Exercise
Exercise
Why can macro F1 be more informative than accuracy when one category is much more common than the others?
Show answer
Macro F1 gives every category equal weight, so poor performance on a rare category lowers the overall score instead of being hidden by the common category.