Sub topic 14
Reading Your Errors and Using Sentiment Results Responsibly
Prerequisites and purpose
This chapter assumes basic Python, supervised learning, and familiarity with precision, recall, F1, and confusion matrices. You should also know how to train a classifier and make predictions on held-out data.
A single score cannot tell you whether a sentiment system is useful. Error analysis is the practice of examining incorrect predictions to find patterns. Those patterns help you improve data, features, labels, or model choice.
A practical error-analysis loop
Start with predictions from data that the model did not use for training. Store the text, true label, predicted label, and confidence score. Then inspect examples where the prediction is wrong.
from collections import Counter
records = [
{"text": "The camera is sharp, but the battery is terrible",
"true": "negative", "predicted": "positive", "confidence": 0.78},
{"text": "Thanks for another three-hour outage",
"true": "negative", "predicted": "positive", "confidence": 0.64},
{"text": "It is okay, nothing special",
"true": "neutral", "predicted": "negative", "confidence": 0.56},
{"text": "Fast delivery and excellent packaging",
"true": "positive", "predicted": "positive", "confidence": 0.96},
]
errors = [row for row in records if row["true"] != row["predicted"]]
for row in errors:
print(
f'{row["true"]:8} -> {row["predicted"]:8} '
f'({row["confidence"]:.2f}): {row["text"]}'
)
print("Errors by actual label:", Counter(row["true"] for row in errors))Read errors in groups rather than one at a time. For example, a classifier may fail mainly on mixed opinions, sarcasm, or neutral statements. A few representative errors can reveal a problem that the overall F1 score hides.
Build an error taxonomy
Give each error a reason. Use categories that lead to an action.
| Error category | Typical example | Possible response |
|---|---|---|
| Label ambiguity | “It is fine” could be neutral or positive | Clarify annotation rules |
| Negation | “Not bad” | Add examples or suitable features |
| Sarcasm | “Wonderful, another outage” | Add in-domain labeled data |
| Mixed sentiment | “Great camera, awful battery” | Use aspect-based labels |
| Domain shift | “The service is sick” means excellent in slang | Evaluate and train on the target domain |
| Label noise | Annotators disagree | Review the labeling process |
A domain is the setting and language style represented by data, such as hotel reviews or social posts. Domain shift occurs when the deployment text differs from the training text. A model trained on formal reviews may misunderstand abbreviations, emojis, or sarcasm in social media.
Compare performance by useful slices, such as product category, language variety, text length, or presence of emojis. A model can have acceptable overall accuracy while performing poorly for one slice.
Use confidence carefully
A confidence score is a model output, not a guaranteed probability of correctness. Check whether high-confidence predictions are actually more accurate than low-confidence predictions. If they are not, the model may be poorly calibrated.
For uncertain or high-impact cases, route the text to a person. Keep the original text, prediction, model version, and review decision so that you can audit later. Remove private information when it is not needed, and restrict access to stored posts.
Sentiment analysis should not decide employment, housing, education, medical care, or other consequential outcomes by itself. Sentiment can reflect dialect, culture, quoting, or discussion of someone else’s opinion. It may also reproduce bias found in the labels or training data.
A responsible improvement cycle
Repeat the evaluation after each change. Keep a fixed test set when possible, and use a separate recent sample to check whether performance remains stable after deployment.
Exercise
A model labels “The support team finally answered, amazing” as positive. What should you inspect before changing the model?
Show answer
Inspect the surrounding context, the annotator’s label, similar examples, and the text’s domain. The sentence may be sincere or sarcastic, and the label itself may be wrong or ambiguous.