Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 4

NER Performance Metrics with seqeval

Recommended resources

Welcome back! In our previous lessons, we've established a solid foundation for NER. You learned how to formalize entity annotations using the BIO tagging scheme and explored the structure of a classic benchmark dataset, CoNLL-2003. We now have the "what" (entities) and the "how" (data format). The crucial missing piece is: how do we measure success?

Today, we will tackle the evaluation of NER systems. This lesson is designed to equip you with the knowledge to calculate and interpret the standard metrics for NER performance: entity-level precision, recall, and F1-score. We'll first understand why simple accuracy is insufficient for this task and then delve into the specifics of entity-level evaluation. Finally, we'll see how to perform these calculations in practice using seqeval, a widely-used Python library.

The Problem with Accuracy

In many classification problems, accuracy—the percentage of correct predictions—is a common starting point. However, for NER and other sequence labeling tasks, it can be highly misleading. Why? Because most words in a typical sentence are not part of a named entity. They are labeled with the 'O' (Outside) tag.

A model that learns to predict 'O' for every single token can achieve over 90% accuracy on many datasets, yet it would be completely useless as it would fail to identify any entities. We need metrics that focus on the model's ability to identify the rare but important entity tags. The solution lies in using precision, recall, and the F1-score.

Given your background in machine learning, these terms are likely familiar. However, let's ensure we have a shared, intuitive understanding of them. The following video provides an excellent and concise explanation.

Precision, Recall, & F1 Score Intuitively Explained

This video from the channel Scarlett's Log intuitively explains the concepts of precision, recall, and F1-score, and why accuracy can be a flawed metric.

Please watch the following segments: The definitions of true/false positives and negatives. The explanation of why accuracy is problematic for biased datasets, which is directly analogous to the class imbalance in NER. The descriptions of precision, recall, and the F1-score.

As the video explains, these metrics give us a much more nuanced view of performance:

  • Precision: Of all the entities the model predicted, how many were correct? (Measures exactness)
  • Recall: Of all the actual entities in the data, how many did the model find? (Measures completeness)
  • F1-Score: The harmonic mean of precision and recall, providing a single, balanced measure of performance.

Token-Level vs. Entity-Level Evaluation

Now, how do we apply these metrics to NER? A named entity like "European Commission" spans multiple tokens. Do we get partial credit if our model correctly identifies "European" as B-ORG but misses "Commission"?

The standard and most rigorous approach in NER is entity-level evaluation. An entity is considered correctly identified only if two conditions are met:

  1. The span is exact (all tokens of the entity are identified, and no extra tokens are included).
  2. The type is correct (e.g., ORG is predicted, not LOC).

Any deviation—a partial match, an incorrect boundary, or the wrong label—counts as an error. This is much stricter than token-level evaluation, where each token's tag is judged independently.

The following video does an outstanding job of illustrating this crucial difference.

Named entity recognition (NER) model evaluation

This video by "mildlyoverfitted" clearly breaks down the difference between token-level and entity-level evaluation with practical examples.

Please watch the segment from the beginning up to this point. It covers: The core question of how to score different types of mispredictions. A recap of the IOB notation. A concrete example showing how token-level evaluation can be lenient, while entity-level evaluation is strict and aligns better with the real-world goal of extracting complete, correct entities.

To make this concrete, let's analyze what constitutes a True Positive, False Positive, and False Negative in entity-level evaluation.

This table illustrates entity-level evaluation. An entity is a True Positive (TP) only if its span and type are perfectly matched. Any deviation results in False Negatives (FN) for missed entities and False Positives (FP) for incorrect predictions.

Let's break down the image:

  • "Reuters" (ORG): The model predicted ORG for the token "Reuters". The true label agrees perfectly. This is a True Positive (TP).
  • "Foreign Ministry" (ORG): The true label spans two tokens. The model predicted O for both. It completely missed the entity. This is a False Negative (FN).
  • "Shen Guofang" (PER): The true entity is "Shen Guofang". The model only predicted "Shen" as PER and missed "Guofang". Because the span is incorrect, this counts as two errors from the perspective of the evaluation logic:
    • The model failed to find the true entity "Shen Guofang", so this is a False Negative (FN).
    • The model predicted a non-existent entity "Shen", so this is a False Positive (FP).

This strict, all-or-nothing approach at the entity level is the standard for reporting NER performance in academic papers and production systems.


Calculating Metrics with seqeval

Manually calculating these metrics by parsing BIO tags would be tedious and error-prone. Fortunately, there's a standard library for this: seqeval. It implements the CoNLL-2003 evaluation script's logic and is the de facto tool for NER evaluation in Python.

The following article provides a clear walkthrough of using seqeval.

Evaluate sequence models in python - Depends on the definition

This article from "Depends on the definition" demonstrates why accuracy is a poor choice and provides a clear, practical example of using seqeval to get a detailed performance report.

First, quickly read the section The right metrics for another perspective on why accuracy is misleading. Then, focus on the main section, Put it all together. It shows how to use seqeval.metrics.classification_report with a list of true label sequences and a list of predicted label sequences. Pay close attention to the code and the resulting output table.

As you saw in the article, you provide seqeval with lists of lists, where each inner list represents the BIO-tagged labels for one sentence. For example:




# Ground truth labels for two sentences
y_true = [
    ['B-PER', 'I-PER', 'O', 'B-LOC', 'O'],
    ['B-ORG', 'I-ORG', 'I-ORG', 'O']
]




# Model's predicted labels
y_pred = [
    ['B-PER', 'I-PER', 'O', 'B-ORG', 'O'], # Incorrect type: ORG instead of LOC
    ['B-ORG', 'O', 'O', 'O']              # Incorrect span: Missed I-ORG tags
]

Passing these to seqeval.metrics.classification_report would produce a detailed breakdown of precision, recall, and F1-score for each entity type (PER, LOC, ORG), along with overall averages. This is exactly the kind of report you will use to evaluate the models you build in the upcoming lessons.

For those interested in how this works under the hood, the same video from "mildlyoverfitted" that we watched earlier contains a fantastic segment where the author implements the entity-level evaluation logic from scratch in Python. Watching this is optional, but given your CSE background, you might find it very insightful.

Named entity recognition (NER) model evaluation

This is an optional but recommended segment for understanding the internal logic of an entity-level evaluation script.

If you're curious, watch the implementation of the get_report function, starting from this timestamp. The author uses pandas to find entity spans, merge true and predicted entities, and then uses scikit-learn's classification_report to get the final scores. This demystifies what libraries like seqeval are doing internally.


Conclusion

You've now completed the final foundational lesson for NER! Evaluating a model is just as important as building it, and you are now equipped with the standard methodology for doing so.

Key Takeaways:

  • Simple accuracy is a misleading metric for NER due to the high frequency of the 'O' tag.
  • Precision, recall, and F1-score provide a more robust and insightful measure of performance.
  • The standard for NER is entity-level evaluation, which requires both the entity's span and type to be perfectly predicted to count as a true positive.
  • Partial matches are treated as both a false positive (for the incorrect prediction) and a false negative (for the missed true entity).
  • The seqeval library is the standard tool in Python for calculating entity-level precision, recall, and F1-score from sequences of BIO tags.

Preview of the Next Lesson:
With the fundamentals of data representation (BIO), dataset structure (CoNLL), and evaluation (entity-level F1-score) now covered, we are ready to start building models. In the next module, we will begin by exploring a powerful classical model for sequence labeling: the Conditional Random Field (CRF). Understanding CRFs will provide you with a deep appreciation for the challenge of modeling dependencies between labels in a sequence—a concept that remains relevant even in the age of transformers.