Sub topic 5
Building an error taxonomy by hand
An error taxonomy is a short list of named failure categories, plus counts of how many errors fall into each. This chapter assumes you can train and evaluate a text classifier, read a confusion matrix, and use pandas to filter a DataFrame. The previous chapter produced a table of misclassified examples. Here you turn that table into categories you can rank and act on.
Why a taxonomy beats a pile of examples
Reading errors one at a time gives you impressions: "it seems to struggle with negation." An impression is hard to compare against other impressions and hard to hand to a teammate. A taxonomy replaces impressions with counts. If 18 of 60 sampled errors involve negation and 4 involve sarcasm, you know where to look first.
The categories describe why the model failed, not just which label it got wrong. The confusion matrix already tells you that "neutral" was predicted as "positive". The taxonomy tells you that the cause was mild praise in a hedged sentence.
Step 1: Draw a sample
If your model makes 2,000 errors, you do not need to read them all. Reading 50 to 100 gets you a usable first taxonomy. Draw the sample at random so that you do not only see the cases that stand out. If the data has several classes, a stratified sample (a fixed number of errors per confusion-matrix cell) keeps rare confusions visible, but then remember that the counts no longer match the real error rates. Record which sampling method you used.
Sort order matters too. The previous chapter suggested reading high-confidence errors first. That is useful for finding label problems, but it biases your counts toward them. For a first taxonomy, sample randomly. Read the confident errors as a second pass.
import pandas as pd
errors = pd.DataFrame({
"text": [f"example {i}" for i in range(200)],
"label": ["pos"] * 100 + ["neg"] * 100,
"pred": ["neg"] * 100 + ["pos"] * 100,
})
sample = errors.sample(n=50, random_state=0).copy()
sample["note"] = "" # free-text observation, filled in by hand
sample["category"] = "" # filled in after the notes are grouped
print(sample.shape)The random_state makes the sample reproducible, so a colleague can read the same 50 rows.
Step 2: Write a note before choosing a category
On the first pass, write one short free-text note per error. Do not pick from a fixed list yet. Notes like "negation, 'not bad'", "sarcasm", "product name looks negative", "label seems wrong" capture what you actually see. If you start with a predefined list, you will force errors into boxes that do not fit and miss categories you did not think of.
Keep each note about what in the input led to the error, not about what the model should have done. "Model ignores 'not'" is a guess about the model. "Contains negation before a positive word" is an observation you can check.
Step 3: Group notes into categories
After 30 to 50 notes, read them together and cluster similar ones. Good categories have these properties:
- Observable. Someone else can decide from the text alone whether an example belongs.
- Specific enough to suggest a fix. "Hard examples" is not a category. "Negation before a sentiment word" suggests adding negation examples or a rule check.
- Mostly distinct. Overlap is allowed, but if two categories always co-occur, merge them.
- Roughly the same grain. Avoid mixing "sarcasm" with "text longer than 200 tokens" unless you allow multiple tags.
Always keep an other category and a label_problem category. other stops you from forcing a bad fit. label_problem collects cases where the gold label looks wrong or debatable, which the label noise chapter covers in more detail. Treat these as findings, not as failures of the taxonomy.
| Category | Definition | Example |
|---|---|---|
| negation | A negator scopes over a sentiment word | "not bad at all" |
| hedged / mild | Weak sentiment with qualifiers | "fine I guess" |
| sarcasm | Surface sentiment is opposite to intent | "great, it broke on day one" |
| mixed | Both positive and negative content | "late but works well" |
| label_problem | Gold label is wrong or arguable | "okay" labeled neg |
| other | None of the above |
Step 4: Allow multiple tags, but keep a primary one
A single error can fit several categories. "Not great, but the price is fine" has negation and mixed sentiment. Two options work:
- Single primary category. Each error gets one tag. Counts add up to 100%, which makes ranking simple. You must decide on tie-breaking rules, such as "pick the category you would fix first."
- Multi-label tags. Each error can carry several tags. Counts can exceed the sample size, and you read each category as "share of errors that involve this."
For a first pass, use multi-label tags in a boolean table, and add a primary column if you need a single number later.
import pandas as pd
tagged = pd.DataFrame({
"text": [
"not bad at all",
"fine I guess",
"great, it broke on day one",
"late but works well",
"okay",
],
"negation": [True, False, False, False, False],
"hedged": [False, True, False, False, True],
"sarcasm": [False, False, True, False, False],
"mixed": [False, False, False, True, False],
"label_problem": [False, False, False, False, True],
})
tag_cols = ["negation", "hedged", "sarcasm", "mixed", "label_problem"]
counts = tagged[tag_cols].sum().sort_values(ascending=False)
print(counts)
print((counts / len(tagged)).round(2))The second print gives each category's share of the sampled errors. With only five rows these shares are illustrative. With 50 rows, each share has wide uncertainty, which the uncertainty chapter handles.
Step 5: Test the taxonomy for consistency
A taxonomy is only useful if the tags are repeatable. Check this in two ways.
Re-tag after a break. Wait a day, shuffle the sample, hide your previous tags, and tag again. If you disagree with yourself on more than about a fifth of examples, the definitions are too vague. Rewrite them.
Ask a second person. Have a colleague tag the same 30 rows using only the written definitions. Compute agreement per category. Cohen's kappa corrects raw agreement for chance, and scikit-learn provides it.
from sklearn.metrics import cohen_kappa_score
# Primary category assigned by two annotators to the same ten errors.
a = ["negation", "hedged", "sarcasm", "mixed", "negation",
"hedged", "other", "mixed", "negation", "sarcasm"]
b = ["negation", "hedged", "sarcasm", "hedged", "negation",
"hedged", "other", "mixed", "mixed", "sarcasm"]
print(round(cohen_kappa_score(a, b), 2))Kappa near 1 means strong agreement. Values below roughly 0.6 usually mean the category definitions need work. Look at which categories the two of you disagree on and tighten those first. Ten items is too few for a stable estimate, so use at least 30 in practice.
Common mistakes
- Starting with a fixed list from a paper or blog post. Borrowed categories can be a useful checklist after the first pass, but they should not replace your own notes.
- Categories that describe the model, not the data. "Attention failure" cannot be verified from the text. Use categories you can read off the input.
- Letting
othergrow. Ifotheris above 20 to 25 percent of the sample, there is a cluster you have not named. Read those rows again. - Counting only one sample. A taxonomy built from 50 errors can miss rare categories. When the categories stop changing after another 25 reads, you have enough for a first pass.
- Skipping the examples. Store two or three clear examples per category with the definition. They are what lets another person use the taxonomy.
- Mixing sources of error. Data problems (label noise, truncation), text properties (negation, length), and model behavior (overconfidence) are different kinds of causes. Keep them in separate groups so that the fix list stays clear.
What to carry forward
Save the taxonomy as a small document: category name, definition, two examples, and the count. The next chapters use it in three ways. Slicing checks whether a category matches a measurable text property, such as length or presence of a negator. Prioritization combines the counts with how fixable each category is. Behavioral tests turn a category into a test you can rerun after every change.
Exercise
You sampled 40 errors and tagged each with free-text notes. Your draft list has 7 categories, and 14 of the 40 errors landed in other. A colleague tags the same rows and agrees with you on only 55% of them. What should you do next, and why?
Show answer
Do not start counting or fixing yet. First read the 14 other rows together to find clusters that deserve names, since 35% in other means the taxonomy is missing categories. Then tighten the written definitions and add two examples per category. Have your colleague re-tag using the revised document and compute per-category agreement (for example with Cohen's kappa), and revise the categories where you still disagree. Only after agreement is acceptable and other is down to around a fifth of the sample should you count and rank the categories.
Exercise
Why should the first sample of errors be drawn at random instead of sorted by confidence, and when is sorting by confidence still useful?
Show answer
Sorting by confidence puts the confident mistakes first, and these are over-represented among label problems and unusual cases. Tag counts from such a sample would not reflect the typical error. A random sample gives counts closer to the real mix of errors. Sorting by confidence is still useful as a second pass, because confident errors are the best place to look for wrong gold labels and systematic model blind spots.