Sub topic 4
Measuring extraction quality with span-level F1
This chapter assumes you know basic Python, JSON, language-model concepts, and introductory precision, recall, and F1. We will focus on extraction tasks where the system returns spans: a start position, an end position, and a label.
Why span-level evaluation matters
Information extraction can return named entities, relations, or fields such as vendor and total. To compare a prompted model with a fine-tuned model, both systems need the same evaluation rule.
A gold span is the correct answer in an annotated example. A predicted span is the answer produced by the system. Represent each span as:
(start, end, label)Use a half-open interval. The start index is included, and the end index is excluded. For example, in Angela Merkel, the span for the person might be (0, 13, "PER").
An exact span match requires all three parts to match:
- The start position is correct.
- The end position is correct.
- The label is correct.
A prediction for Angela when the gold span is Angela Merkel is therefore incorrect, even though the prediction overlaps the correct text.
Precision, recall, and F1
Let:
- True positives (TP) be predicted spans that exactly match gold spans.
- False positives (FP) be predicted spans with no matching gold span.
- False negatives (FN) be gold spans that the system missed.
The metrics are:
| Metric | Formula | Meaning |
|---|---|---|
| Precision | TP / (TP + FP) | How many predictions were correct |
| Recall | TP / (TP + FN) | How many gold spans were found |
| F1 | 2 × precision × recall / (precision + recall) | Balance between precision and recall |
F1 is zero when both precision and recall are zero. If a document has no gold spans, decide on a policy before evaluation. Some projects exclude such documents from span-level averages. Others count an empty prediction as correct. The important point is to use one policy consistently.
Converting BIO tags into spans
Token-classification models often output BIO tags. B-PER begins a person span, I-PER continues it, and O means that the token belongs to no entity. Convert these tags into spans before scoring.
def bio_to_spans(tags):
spans = []
start = None
label = None
for i, tag in enumerate(tags + ["O"]):
same_span = (
start is not None
and tag.startswith("I-")
and tag[2:] == label
)
if start is not None and not same_span:
spans.append((start, i, label))
start = None
label = None
if tag.startswith("B-"):
start = i
label = tag[2:]
elif tag.startswith("I-") and start is None:
# Repair an invalid I-tag by treating it as a new span.
start = i
label = tag[2:]
return spans
def precision_recall_f1(gold, predicted):
gold = set(gold)
predicted = set(predicted)
true_positives = len(gold & predicted)
precision = (
true_positives / len(predicted) if predicted else 0.0
)
recall = true_positives / len(gold) if gold else 0.0
f1 = (
2 * precision * recall / (precision + recall)
if precision + recall
else 0.0
)
return precision, recall, f1
gold = bio_to_spans(["B-PER", "I-PER", "O", "B-LOC"])
predicted = bio_to_spans(["B-PER", "O", "O", "B-LOC"])
print(gold)
print(predicted)
print(precision_recall_f1(gold, predicted))The first person prediction is too short, so it does not match the gold person span. The location prediction matches. The result is one true positive, one false positive, and one false negative.
Micro and macro F1
For a dataset, micro F1 combines all true positives, false positives, and false negatives before calculating the score. Large documents or frequent labels have more influence.
Macro F1 calculates a score for each label or document and then averages those scores. It gives rare labels more influence, but it can be unstable when a label has very few examples.
For model comparisons, report the averaging method and the matching rule. Also preserve the same train, validation, and test split. Otherwise, a change in the split can look like a model improvement.
Exercise
The gold spans are {(0, 2, "PER"), (4, 5, "LOC")}. The model predicts {(0, 2, "PER"), (7, 8, "ORG")}. What are precision, recall, and F1?
Show answer
TP = 1, FP = 1, and FN = 1. Precision = 1/2 = 0.5, recall = 1/2 = 0.5, and F1 = 0.5.