Sub topic 7
Measuring Performance: Precision, Recall, F1, and the Confusion Matrix
Assume you know basic Python, can install packages with pip, and understand training data, test data, and predictions. This chapter focuses on evaluating a sentiment classifier after it produces labels such as positive, negative, and neutral.
A test score is useful only when the test data represents the cases your model will meet. Keep test examples separate from training examples. Otherwise, the model may appear accurate because it has already seen the answers.
The confusion matrix
A confusion matrix counts how predicted labels compare with the true labels. For binary sentiment classification, the common terms are:
- True positive: the model predicts positive, and the text is positive.
- False positive: the model predicts positive, but the text is not positive.
- True negative: the model predicts negative, and the text is negative.
- False negative: the model predicts negative, but the text is not negative.
For multiple classes, the matrix has one row and column for each class. Rows usually represent true labels, and columns represent predicted labels. Always check the library's convention before interpreting the matrix.
Precision, recall, and F1
Precision answers this question: “When the model predicts a class, how often is it correct?”
\text{precision} = \frac{TP}{TP + FP}Recall answers: “Of all examples that really belong to a class, how many did the model find?”
\text{recall} = \frac{TP}{TP + FN}F1 is the harmonic mean of precision and recall. It is high only when both values are high.
F1 = 2 \times \frac{\text{precision} \times \text{recall}}
{\text{precision} + \text{recall}}| Metric | Main question | Important when |
|---|---|---|
| Precision | Can I trust a predicted class? | False positives are costly |
| Recall | Did the model find most real cases? | False negatives are costly |
| F1 | Is the balance between both acceptable? | You need one balanced score |
For sentiment analysis, the right priority depends on the use case. A system that flags angry customer posts may prioritize recall for negative sentiment. A system that automatically publishes positive testimonials may prioritize precision.
Evaluating a classifier with scikit-learn
classification_report calculates precision, recall, and F1 for each class. confusion_matrix shows which classes the model confuses.
from sklearn.metrics import classification_report, confusion_matrix
true_labels = ["pos", "pos", "neg", "neg", "neutral", "neg"]
predicted_labels = ["pos", "neg", "neg", "neutral", "neutral", "neg"]
class_order = ["neg", "neutral", "pos"]
print(classification_report(
true_labels,
predicted_labels,
labels=class_order,
zero_division=0,
))
matrix = confusion_matrix(
true_labels,
predicted_labels,
labels=class_order,
)
print("Rows: true labels; columns: predicted labels")
print(class_order)
print(matrix)Read the matrix using the declared class order. A value on the diagonal is a correct prediction. An off-diagonal value shows a specific error, such as negative text classified as neutral.
Choosing an average for multiple classes
A multiclass report often includes several averages:
- Macro average gives every class equal weight. It reveals poor performance on a small class.
- Weighted average weights each class by its number of examples. It better reflects overall performance but can hide minority-class failures.
- Micro average combines all decisions. In ordinary single-label classification, it often matches accuracy.
Report per-class results as well as an average. If the neutral class dominates your test set, macro F1 is often more informative than weighted F1.
Exercise
A classifier has precision 0.90 and recall 0.50 for negative sentiment. What does this suggest?
Show answer
Its negative predictions are usually correct, but it misses many genuinely negative texts. It has high precision and low recall.