Sub topic 12
Measuring detection quality
This chapter assumes you can write the regex and checksum detectors from earlier chapters and that each detector returns spans as (start, end, label) tuples. You also need basic Python. Here you learn to score a detector against labelled examples, so you can say whether a change helped.
Why you need numbers
A detector that "looks fine" on five sample messages can still miss names in real traffic. A detector can also flag so much harmless text that users stop trusting the app. Measuring replaces opinion with counts.
Every detected span falls into one of three groups:
- True positive (TP): the detector found a span that is really personal data.
- False positive (FP): the detector flagged text that is not personal data.
- False negative (FN): the detector missed personal data that was there.
From these counts you compute two scores:
- Precision = TP / (TP + FP). Of everything flagged, how much was correct.
- Recall = TP / (TP + FN). Of everything that exists, how much was found.
For privacy work, recall usually matters more. A missed email leaks. A false alarm only costs a little usefulness. Still, low precision makes the redacted text hard for the model to use, so you track both. The F1 score, 2 × P × R / (P + R), combines them into one number.
Build a labelled test set
You need examples where a person has marked the true spans. Include:
- Messages that contain each label you detect.
- Messages with look-alikes, such as order numbers that resemble card numbers.
- Messages with no personal data at all.
- Text that looks like your real traffic, not only clean samples.
Store the expected spans in the same (start, end, label) shape the detector returns.
Scoring spans
Compare predicted spans to expected spans. This version uses exact matching: the start, end, and label must all agree.
def score(expected, predicted):
"""expected, predicted: lists of (start, end, label) per document."""
tp = fp = fn = 0
for exp, pred in zip(expected, predicted):
exp_set, pred_set = set(exp), set(pred)
tp += len(exp_set & pred_set)
fp += len(pred_set - exp_set)
fn += len(exp_set - pred_set)
precision = tp / (tp + fp) if tp + fp else 0.0
recall = tp / (tp + fn) if tp + fn else 0.0
f1 = (2 * precision * recall / (precision + recall)
if precision + recall else 0.0)
return {"tp": tp, "fp": fp, "fn": fn,
"precision": round(precision, 2),
"recall": round(recall, 2),
"f1": round(f1, 2)}
expected = [
[(8, 23, "EMAIL")],
[(5, 24, "CREDIT_CARD")],
[],
]
predicted = [
[(8, 23, "EMAIL")],
[(5, 24, "CREDIT_CARD"), (30, 43, "CREDIT_CARD")],
[],
]
print(score(expected, predicted))This prints tp: 2, fp: 1, fn: 0, precision 0.67 and recall 1.0. The extra card-like span in the second document is a false positive.
Score each label separately
One overall number hides weak spots. Emails may reach recall 0.99 while person names sit at 0.70. Run score once per label by filtering the span lists, and report a row per label.
| Label | Precision | Recall | Note |
|---|---|---|---|
| 0.98 | 0.99 | Regex works well here | |
| CREDIT_CARD | 0.95 | 0.90 | Luhn removed most false hits |
| PERSON | 0.80 | 0.70 | NER misses unusual names |
Error analysis
The scores tell you how much is wrong. The errors tell you why. Print every false positive and false negative with surrounding text, then group them by cause. Common causes:
- Names missing from the model's training data, such as uncommon or non-English names.
- Formats the regex did not expect, such as phone numbers with dots.
- Identifiers split by line breaks or typos.
- Ordinary words flagged as places or organizations.
Fix the largest group first, then re-run the same test set. Keep the test set fixed between runs so changes in score come from the detector.
Common mistakes
- Tuning on the test set. If you keep adjusting patterns until the test set passes, the score overstates real performance. Hold back a second set you only check occasionally.
- Testing only clean data. Real messages contain typos, pasted tables, and mixed languages.
- Reporting one number. Always give recall per label.
- Treating a good score as proof of safety. A score describes your test set only. New traffic can behave differently, so re-measure periodically.
Exercise
A detector finds 8 of the 10 real emails in a test set and also flags 2 strings that are not emails. What are its precision and recall?
Show answer
TP = 8, FN = 2, FP = 2. Precision = 8 / (8 + 2) = 0.80. Recall = 8 / (8 + 2) = 0.80.
Exercise
Your team must choose between a detector with precision 0.99 and recall 0.80, and one with precision 0.85 and recall 0.97. Which suits redaction before a model call, and why?
Show answer
The second. Missed personal data (false negatives) leaks to the model provider, so high recall matters most. The lower precision means some harmless text gets redacted, which costs usefulness but not privacy.