Sub topic 14
Error Analysis and Choosing an Approach
Assume you know basic Python, tokens, loops, pip-installed packages, and the purpose of a neural-network classifier. This chapter assumes you can run spaCy and Hugging Face examples and understand entity spans and BIO tags.
Start with entity-level errors
Named entity recognition (NER) output is correct only when both the entity boundaries and the entity label match the annotation. For example, if the correct entity is New York City with label GPE, predicting New York as GPE is a boundary error even though the label is right.
Create an error table from a representative validation set. Do not inspect only examples that look easy.
| Error type | Example | Likely cause |
|---|---|---|
| Missed entity | `Acme` is predicted as `O` | Unseen wording or weak context |
| Spurious entity | `May` is labeled `DATE` in `May joined us` | Ambiguous token |
| Wrong label | `Paris` is labeled `ORG` | Similar contexts between classes |
| Boundary error | `University` instead of `University of Oxford` | Incorrect span learning |
| Inconsistent result | Same product is labeled differently | Domain shift or missing rule |
Compare errors by entity type, document source, length, capitalization, and frequency. A high overall F1 score can hide poor performance on a rare but important type.
A practical error-analysis loop
Use the same process regardless of the model:
- Preserve the original text and gold spans.
- Record predicted spans, labels, and confidence scores.
- Match predictions to gold entities using exact span and label equality.
- Group failures by error type.
- Fix the data, rules, labels, or model.
- Re-run evaluation on an unchanged test set.
Keep training, development, and test documents separate. Looking at test examples while changing the system makes the test score optimistic.
A small script can expose common span mistakes:
gold = {
("Acme", 0, 4, "ORG"),
("Paris", 20, 25, "GPE"),
}
predicted = {
("Acme", 0, 4, "ORG"),
("Paris", 20, 25, "ORG"),
("May", 40, 43, "DATE"),
}
correct = gold & predicted
missed = gold - predicted
spurious = predicted - gold
print("correct:", sorted(correct))
print("missed:", sorted(missed))
print("spurious:", sorted(spurious))In production, add document identifiers and source categories to each record. This lets you find patterns such as failures concentrated in customer emails or scanned text.
Choosing rules, spaCy, or a transformer
Use rules when the entity vocabulary is stable and precision matters. An EntityRuler works well for product codes, legal identifiers, known organization names, and fixed terminology. Rules are easy to inspect, but they do not generalize well to spelling variation or unfamiliar entities.
Use a pretrained spaCy model when your labels resemble its training domain and you need a fast baseline. It provides convenient Doc and Span objects and usually needs little code. Its weaknesses often appear with specialized language, unusual formatting, and labels that differ from its built-in scheme.
Use a fine-tuned transformer when context, variation, or a specialized domain matters. Transformers can distinguish ambiguous words from their surrounding text, but they require labeled data, more computation, careful subword-label alignment, and monitoring for domain drift.
A hybrid system is often the best choice:
Apply rules carefully. A rule should not silently overwrite a statistical prediction without a documented priority. Test overlapping entities, punctuation, case variants, and partial matches.
Make the decision measurable
Before selecting an approach, define the operational constraints:
- Which entity types are required?
- Is a false positive or a missed entity more costly?
- How much labeled text is available?
- Must predictions run on a CPU or within a strict latency limit?
- Will the vocabulary and writing style change over time?
Evaluate the final system with entity-level precision, recall, and F1. Then inspect examples manually. Metrics tell you where performance changed; error analysis tells you what to change next.
Exercise
A system must identify a few thousand exact medical product codes with almost no false positives. Should you begin with a transformer?
Show answer
No. Begin with normalized dictionary or EntityRuler rules, test boundary and case behavior, and add a statistical model only if surrounding context or unseen variants requires it.