Sub topic 11
Errors from Distribution Shift
Assume you know how to train and evaluate a text classifier, use pandas and NumPy, and read accuracy, precision, recall, and F1. This chapter focuses on errors caused by a difference between the data used for evaluation and the data encountered after release.
What distribution shift means
A model learns patterns from a training distribution. A distribution is the mix of inputs, labels, and relationships between them. Distribution shift occurs when that mix changes.
For example, a sentiment model trained on product reviews may receive short social media posts after deployment. The vocabulary, spelling, topics, and class balance can all change. A test score from the old review dataset may no longer predict production behavior.
There are three useful forms of shift:
| Shift type | What changes | Example |
|---|---|---|
| Covariate shift | The input text changes | New slang appears in messages |
| Label shift | Class frequencies change | Negative tickets become more common |
| Concept shift | The input-to-label relationship changes | “Sick” changes from negative to positive slang |
These forms can occur together. A new product launch may change both the topics people discuss and the meaning of some words.
Detecting shifted errors
Start by comparing labeled evaluation data with a sample of recent production data. Compare properties that may affect predictions:
- Source, language, region, or time period
- Text length and character patterns
- Topic or product area
- Class frequencies
- Unknown words and spelling variants
- Confidence and error rates
Do not compare only the overall score. A model may keep the same accuracy while failing on a newly common customer segment.
Add a source or time column to an evaluation table, then calculate metrics by group:
import pandas as pd
df = pd.DataFrame({
"source": ["reviews", "reviews", "social", "social", "social", "reviews"],
"label": ["pos", "neg", "pos", "neg", "neg", "pos"],
"pred": ["pos", "neg", "neg", "neg", "pos", "pos"],
})
df["correct"] = df["label"] == df["pred"]
summary = df.groupby("source")["correct"].agg(["mean", "count"])
summary = summary.rename(columns={"mean": "accuracy", "count": "support"})
print(summary)A large gap between sources is a clue, not proof. Check the support count and estimate uncertainty before deciding that the model truly differs by source.
Choosing a response
When shift is confirmed, identify whether the problem is temporary, recurring, or permanent.
- Add representative examples from the new source to training data.
- Update labels if the meaning or policy has changed.
- Retrain or fine-tune on recent data while retaining older examples.
- Add a source-specific model only when sources have stable, meaningful differences.
- Use a low-confidence route to human review for unfamiliar inputs.
- Monitor metrics over time and set alerts for changes in text properties or class rates.
A common mistake is to train only on the latest batch. This can improve current performance while causing regression on older but still valid traffic. Keep a fixed historical test set and a recent-shift test set. Evaluate both after every change.
Failure modes
Production data can be misleading. Recent examples may have incomplete labels, duplicated texts, or a different sampling process. A source gap may also come from annotation inconsistency rather than model behavior.
Avoid treating every unfamiliar word as evidence of shift. Rare words can be harmless. Prefer evidence that connects the changed property to a higher error rate, lower confidence, or a new error category.
Exercise
Exercise
A classifier scores 92% on historical reviews and 78% on recent social posts. What should you check before retraining?
Show answer
Check the sample counts, label definitions, class balance, text properties, confidence, and error categories for each source. Confirm that the social-post labels are reliable and that the gap persists on a representative sample.