NER evaluation
Spacy
model metrics
natural language processing
machine learning evaluation

What is the good metric to evaluate NER model trained in Spacy

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Named Entity Recognition (NER) is a crucial task in Natural Language Processing (NLP) that involves identifying and categorizing key elements from text, such as names, organizations, locations, dates, etc. Evaluating NER models accurately is essential to ensure high performance and utility. When training NER models using Spacy, a popular NLP library, we must select the appropriate metrics to assess their effectiveness. This article explores the key metrics for evaluating a Spacy-trained NER model, providing technical explanations and examples.

Metrics Overview

Evaluating an NER model involves assessing its ability to correctly identify and categorize entities. Commonly used metrics include precision, recall, and F1-score. These metrics provide insights into different aspects of model performance and are calculated at the token or entity level.

1. Precision

Precision measures the proportion of true positive entities among the ones the model predicted. It indicates the accuracy of the entities the model identifies.

Precision=True PositivesTrue Positives+False Positives\text{Precision} = \frac{\text{True Positives}}{\text{True Positives} + \text{False Positives}}

True Positives: Correctly identified entities. • False Positives: Incorrectly identified or extraneous entities.

Example: If an NER model predicts 10 entities correctly out of a total of 12 predicted entities, its precision is 1012=0.83\frac{10}{12} = 0.83.

2. Recall

Recall measures the proportion of true positive entities identified by the model out of the actual entities present in the data. It assesses the model's ability to identify all relevant entities.

Recall=True PositivesTrue Positives+False Negatives\text{Recall} = \frac{\text{True Positives}}{\text{True Positives} + \text{False Negatives}}

False Negatives: Entities present in the data but missed by the model.

Example: If there are 15 entities in the dataset and the model correctly identifies 10, its recall is 1015=0.67\frac{10}{15} = 0.67.

3. F1-Score

The F1-score is the harmonic mean of precision and recall, providing a balance between the two. It is particularly useful when precision and recall are in tension, and a single metric is desired for evaluation.

F1=2×Precision×RecallPrecision+RecallF1 = 2 \times \frac{\text{Precision} \times \text{Recall}}{\text{Precision} + \text{Recall}}

Example: With a precision of 0.83 and recall of 0.67, the F1-score is:

F1=2×0.83×0.670.83+0.67=0.74F1 = 2 \times \frac{0.83 \times 0.67}{0.83 + 0.67} = 0.74

4. Specificity

Specificity measures the model's ability to correctly identify non-entities. In NER, this is less commonly discussed but can be relevant in specific applications.

Specificity=True NegativesTrue Negatives+False Positives\text{Specificity} = \frac{\text{True Negatives}}{\text{True Negatives} + \text{False Positives}}

True Negatives: Tokens correctly identified as not belonging to any entity.

5. Entity-Level vs. Token-Level Evaluation

Metrics can be calculated at the entity level (entire span of text) or token level (individual tokens). Entity-level evaluation is stricter as the entire span and class must be correct. Token-level evaluation can provide more granular insights into performance.

Entity-Level Example

Consider the sentence: "Barack Obama was born in Hawaii."

• Predicted Entities: "Barack Obama" (PERSON), "Hawaii" (LOCATION) • True Entities: "Barack Obama" (PERSON), "Hawaii" (LOCATION)

Entity-level evaluation here is perfect as the spans and classes match.

Token-Level Example

If a model predicts "Barack" (PERSON) and "Obama" (PERSON) separately, token-level precision and recall may still be high, but entity-level evaluation would penalize such errors.

Practical Considerations

Dataset Quality and Size

The quality and size of the evaluation dataset directly affect the reliability of these metrics. A diverse and representative dataset that mirrors real-world applications is crucial.

Application-Specific Needs

Depending on the application, the weight given to precision versus recall may vary. For example, in legal document review, recall might be prioritized to ensure no important entities are missed.

Example with Spacy

To evaluate an NER model in Spacy, you can use the `scorer()` method, which provides precision, recall, and F1-score among other metrics after parsing a test set. Here's a basic example code snippet:


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.