Sub topic 13
Evaluating NER with Entity-Level Metrics
Assume you know Python, BIO tags, and the difference between a model's prediction and a labelled answer. This chapter uses seqeval, a package that evaluates sequence labels such as B-PER, I-PER, and O.
Why token accuracy is misleading
Named entity recognition (NER) predicts both an entity's label and its complete span. A span is the start and end position of an entity in a sentence.
For example:
Gold: B-PER I-PER O B-LOC
Prediction: B-PER I-PER O OThe prediction gets three of four token labels right. However, it misses the LOC entity completely. Token accuracy hides this failure.
Entity-level evaluation extracts complete entities from both sequences. A predicted entity counts as correct only when:
- Its start and end positions match the gold entity.
- Its entity type matches the gold type.
This is called an exact match.
Precision, recall, and F1
Let true positives be correctly predicted entities, false positives be predicted entities that are wrong or extra, and false negatives be gold entities that the model missed.
| Metric | Formula | Meaning |
|---|---|---|
| Precision | true positives / predicted entities | How often predictions are correct |
| Recall | true positives / gold entities | How many gold entities were found |
| F1 | 2 × precision × recall / (precision + recall) | Balance between precision and recall |
A model that labels every token as an entity may have high recall but poor precision. A model that predicts only highly certain entities may have high precision but poor recall.
seqeval converts BIO sequences into entities before calculating these metrics:
from seqeval.metrics import classification_report, f1_score
y_true = [
["B-PER", "I-PER", "O", "B-LOC"],
["O", "B-ORG", "O"],
]
y_pred = [
["B-PER", "I-PER", "O", "O"],
["O", "B-ORG", "O"],
]
print("F1:", round(f1_score(y_true, y_pred), 3))
print(classification_report(y_true, y_pred))Install the package before running the example:
pip install seqevalThe first sentence has one correct PER entity and one missed LOC entity. The second sentence has one correct ORG entity. Therefore, the model has two true positives, no false positives, and one false negative.
Exact spans matter
Suppose the gold entity is:
New York City → B-LOC I-LOC I-LOCIf the model predicts only New York, the type may be correct, but the span is incomplete. Under strict entity-level scoring, this is not a true positive. It contributes to both a false positive and a false negative.
A wrong type also fails. Predicting ORG for a gold LOC span is not partially correct.
Reading evaluation results
Run evaluation on data that was not used for training. A training score measures memorization more than generalization. Keep a development set for model choices and a test set for the final report.
Inspect per-class results rather than relying only on one overall F1 score. A model might perform well on PERSON and poorly on rare labels such as PRODUCT. The overall score can hide that imbalance.
Error analysis and model choice
After finding a low score, inspect the actual errors. Group them into boundary errors, wrong entity types, missed entities, and spurious entities. Also check whether errors come from unusual spelling, long entities, nested entities, or domain-specific terms.
Use exact entity-level F1 when complete entities are required, such as extracting company names for a database. For search or highlighting, partial-overlap metrics may also be useful, but report them separately because they answer a different question.
Exercise
The gold entities are three spans. A model predicts two of them correctly and adds one incorrect span. What are precision, recall, and F1?
Show answer
Precision is 2/3, recall is 2/3, and F1 is 2/3, or about 0.667.