Evaluation Metrics for NLP
P5.evaluation.01 · Audience: Guest, IT/ML learner, Language professional
Where next?
Later in Evaluation
This module unlocks
Go up a level
P5.evaluation.01 · Audience: Guest, IT/ML learner, Language professional
Later in Evaluation
This module unlocks
Go up a level
A model's quality is invisible until you can measure it: output that looks fluent and confident can still fail on exactly the cases that matter, and staring at examples will never tell you how often. What turns "it seems to work" into a number you can defend is a metric, an agreed formula that scores a model's predictions against a known ground truth — and every metric sees some failures while staying blind to others, so you also need to know which one to trust in which situation. In this module you work the standard metrics out by hand, from classification through generated text to a model's own confidence, and finish with the discipline of holding data back and correcting for repeated comparisons that keeps the resulting numbers honest; every computation runs on the fixed demo corpus or a toy small enough to check on paper. The statistical machinery underneath — significance testing in Statistics for NLP Evaluation, and the statistical laws of language behind language-model scoring — lives in the P1 statistics pillar whenever you want the deeper roots.
Why it matters to you. A translator or reviser judges a translation on meaning, register and terminology. BLEU and ROUGE judge it on something else: how many of its words also appear in a reference translation. BLEU counts the candidate's words found in the reference; ROUGE counts the reference's words found in the candidate. Neither reads meaning, so a candidate that drops one small word can score high, and a faithful one in other words can score low. ("Candidate" is what this module calls the hypothesis.)
Worked example. One reference, two candidates, scored with this module's BLEU-1 (single-word matches, each counted at most as often as in the reference, times a penalty for being shorter than the reference) and its ROUGE-1 recall (the share of the reference's words found in the candidate). Words also found in the reference are highlighted. A word counts only in exactly the same form: "patients" does not match "patient".
| Sentence | Text | BLEU-1 | ROUGE-1 recall | Reviser's mark |
|---|---|---|---|---|
| Reference | the patient must not take this medicine with food | — | — | — |
| Candidate A | the patient must take this medicine with food | 0.882 | 0.889 | Meaning ✗ ("not" is lost: the instruction is reversed) · register ✓ · terminology ✓ |
| Candidate B | patients should never take this medicine while eating | 0.331 | 0.333 | Meaning ✓ · register ✓ · terminology ✓ |
Candidate A matches 8 of its 8 words; Candidate B matches 3 of its 8. Both are one word shorter than the reference, so both take the same shortness penalty, about 0.88. A reviser would reject A and accept B; BLEU-1 and ROUGE-1 both rank them the other way round. That is why these scores are read across hundreds of sentences, beside human review, and never as the verdict on one sentence. Readers who code can check the BLEU-1 figures with the three sentences in "Problem 2 — Implement BLEU-1", in this module's last step, "Exercises & Self-Test".
Task: classify each demo-corpus sentence as “discusses language” (positive) or “does not” (negative). Ground truth: a sentence is positive when it contains the token language — sentences S4 and S6. A toy keyword classifier fires on any sentence containing the token representative or language — so it also fires on S2, where “representative of a population” triggers it.
| S# | Sentence | Ground truth | Classifier | Outcome |
|---|---|---|---|---|
| S0 | Lions are the most dangerous of all wild animals. | ✗ negative | ✗ negative | TN |
| S1 | Humans have always hunted animals as a source of food. | ✗ negative | ✗ negative | TN |
| S2 | The king is a representative of a population. | ✗ negative | ✓ positive | FP |
| S3 | The human population of the world has rapidly increased. | ✗ negative | ✗ negative | TN |
| S4 | A naïve algorithm often fails when the morphology of a language is complex. | ✓ positive | ✓ positive | TP |
| S5 | The representation of a word captures the semantic patterns in text. | ✗ negative | ✗ negative | TN |
| S6 | A café is a place where the community uses language as a form of identity. | ✓ positive | ✓ positive | TP |
| S7 | The tokenization of a word splits it into meaningful subword units. | ✗ negative | ✗ negative | TN |
Every prediction lands in exactly one of four cells — the confusion matrix:
| Actually positive | Actually negative | |
|---|---|---|
| Predicted positive | TP = 2 | FP = 1 |
| Predicted negative | FN = 0 | TN = 5 |
Explore this live — the panel below is the interactive version of exactly this worked example. Move predictions between cells and watch precision, recall, and F1 respond.
Confusion-matrix explorer — the Ev1 demo classifier by default
Reads /api/ml-eval/confusion-metrics — loading
Computing…
Precision, Recall, F1.
On the demo corpus: Precision = 2 / (2 + 1) = 0.6667, Recall = 2 / (2 + 0) = 1.0000, F1 = 2 × 0.667 × 1.000 / (0.667 + 1.000) = 0.8000.
Precision
0.6667
Recall
1.0000
F1
0.8000
Precision = 0.667: 2 of the 3 positive predictions were correct. Recall = 1.000: both actually-positive sentences were found. F1 = 0.800: one false positive penalises precision more than recall here.
When to prioritise Precision vs Recall.
ROC Curve and AUC. P/R/F1 depend on a fixed decision threshold (≥ 0.5 → positive). The ROC curve sweeps the threshold from 0 to 1 and plots TPR (sensitivity/recall) vs FPR (1 − specificity) at every point. AUC (Area Under the Curve) summarises the entire curve in one number: AUC = 1.0 is perfect; AUC = 0.5 is a random classifier.
A toy 5-example ROC: sort the examples by descending confidence and record TPR and FPR after each one (2 positives, 3 negatives):
| k (threshold rank) | Confidence | True label | TPR | FPR |
|---|---|---|---|---|
| 1 | 0.90 | + | 1/2 = 0.50 | 0/3 = 0.00 |
| 2 | 0.80 | − | 1/2 = 0.50 | 1/3 = 0.33 |
| 3 | 0.70 | + | 2/2 = 1.00 | 1/3 = 0.33 |
| 4 | 0.40 | − | 2/2 = 1.00 | 2/3 = 0.67 |
| 5 | 0.30 | − | 2/2 = 1.00 | 3/3 = 1.00 |
The trapezoidal rule over these FPR/TPR points gives AUC = 5/6 ≈ 0.833 — the model ranks a randomly chosen positive above a randomly chosen negative 83.3 % of the time. (Computing from the table's two-decimal roundings gives 0.835; the exact fractions 1/3 and 2/3 give 5/6.)
Sweep the threshold yourself in the lab below — it reproduces this table point by point and reports the exact AUC.
ROC / threshold explorer — the Ev1 5-example toy classifier
Reads /api/ml-eval/roc · /api/ml-eval/threshold-metrics — loading
Computing…
Slide the threshold through the five scores (0.90, 0.80, 0.70, 0.40, 0.30) to reproduce each row of the worked ROC table above.
Where these metrics connect to the rest of the curriculum.
| Type | Resource |
|---|---|
| In-app | P5.ml-methods.06 — Evaluation, Selection & Interpretability, for the model-selection view of these metrics (bias–variance, information criteria, interpretability) |
| In-app | Statistics for NLP Evaluation (P1 statistics pillar), for significance testing of F1 and BLEU differences with the paired bootstrap |
| In-app | Statistical Laws of Language (P1 statistics pillar), for the n-gram language-model perplexity this module's perplexity metric builds on |
| In-app | Statistical Inference and Resampling Methods (P1 statistics pillar), the hypothesis-testing and bootstrap machinery behind every comparison in this module |
Practise this lesson's 8 self-checks
One per screen: you reveal each answer and mark it yourself. Nothing is recorded.
Ask a question about this content. The mentor explains and grounds its answer in what you are studying; asking is recorded as a learning signal, not a grade.
Off by default. The mentor always gets your file; this only decides whether your own copy stays here. Copies live in this browser only - they do not follow you to another device, and clearing site data removes them.
Now grade a classifier yourself. Each rung is a three-panel workspace: instructions on the left, a code editor in the middle, and output + test results on the right. Run checks the visible tests; Submit grades against hidden edge cases; the reference solution unlocks once you pass. The explorer rung is one knob — trade recall for precision on a spam filter; the senior rung picks the metric for a fraud deployment and defends it.
Loading exercise…
Loading exercise…
Loading exercise…
Loading exercise…
Loading exercise…
A scratch console for this page's ideas — ungraded, nothing you run here is recorded.
Scratch console
A scratch console with the scientific stack (pandas, numpy, scikit-learn). Runs on the server — no network, resource-limited and measured.
Output appears here.
Loading your notes...