Evaluation Metrics for NLP

P5.evaluation.01 · Audience: guest, it-ml, language-pro

Real LLM grading for this pageLLM grading (this page):

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.

Step 1 / 4 — Classification Metrics — P/R/F1 and ROC/AUC

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#SentenceGround truthClassifierOutcome
S0Lions are the most dangerous of all wild animals.✗ negative✗ negativeTN
S1Humans have always hunted animals as a source of food.✗ negative✗ negativeTN
S2The king is a representative of a population.✗ negative✓ positiveFP
S3The human population of the world has rapidly increased.✗ negative✗ negativeTN
S4A naïve algorithm often fails when the morphology of a language is complex.✓ positive✓ positiveTP
S5The representation of a word captures the semantic patterns in text.✗ negative✗ negativeTN
S6A café is a place where the community uses language as a form of identity.✓ positive✓ positiveTP
S7The tokenization of a word splits it into meaningful subword units.✗ negative✗ negativeTN

Every prediction lands in exactly one of four cells — the confusion matrix:

Actually positiveActually negative
Predicted positiveTP = 2FP = 1
Predicted negativeFN = 0TN = 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

Computing…

Precision, Recall, F1.

Precision=TPTP+FPRecall=TPTP+FNF1=2⋅P⋅RP+R\text{Precision} = \frac{TP}{TP + FP} \qquad \text{Recall} = \frac{TP}{TP + FN} \qquad F_1 = \frac{2 \cdot P \cdot R}{P + R}
  • Precision — of everything we labelled positive, what fraction was correct?
  • Recall — of everything actually positive, what fraction did we find?
  • F1 — harmonic mean of P and R; punishes extreme imbalances between the two.

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.

  • High Recall matters when missing a positive is costly (medical diagnosis, fraud detection, content moderation).
  • High Precision matters when false positives are costly (spam filters that delete legitimate emails, triggering expensive downstream processes).
  • F1 is the default when neither error dominates — but Fβ (β > 1 for Recall, β < 1 for Precision) lets you tune the trade-off explicitly.

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)ConfidenceTrue labelTPRFPR
10.90+1/2 = 0.500/3 = 0.00
20.80−1/2 = 0.501/3 = 0.33
30.70+2/2 = 1.001/3 = 0.33
40.40−2/2 = 1.002/3 = 0.67
50.30−2/2 = 1.003/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

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.

📚 Go Further

Where these metrics connect to the rest of the curriculum.

TypeResource
In-appP5.ml-methods.06 — Evaluation, Selection & Interpretability, for the model-selection view of these metrics (bias–variance, information criteria, interpretability)
In-appStatistics for NLP Evaluation (P1 statistics pillar), for significance testing of F1 and BLEU differences with the paired bootstrap
In-appStatistical Laws of Language (P1 statistics pillar), for the n-gram language-model perplexity this module's perplexity metric builds on
In-appStatistical Inference and Resampling Methods (P1 statistics pillar), the hypothesis-testing and bootstrap machinery behind every comparison in this module
Ask the mentor about this module

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.

Images, PDF or text. Kept on this device only.
Keeping your files on this device

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.

Ctrl/Cmd + Enter to send

🎓 Practice ladder

5 graded rungs · ~47 min

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.

Rung 1 — Precision, recall and F1 from a confusion matrix

Loading exercise…

Rung 2 — ROC points by threshold sweep

Loading exercise…

Rung 3 — Pick the operating point

Loading exercise…

Explorer rung — Raise the bar: trade recall for precision

Loading exercise…

Senior rung — Pick the metric for the deployment and defend it

Loading exercise…

Try it yourself

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.

My notes on this module

Loading your notes...

Evaluation Metrics for NLP — TransformerLab