Evaluation Metrics for NLP
P5.evaluation.01 · Audience: guest, it-ml, language-pro
Precision, recall, F1, ROC/AUC, perplexity, BLEU, ROUGE, calibration, and evaluation methodology — formulae and worked examples, every one computed on the fixed demo corpus or a small hand-checkable toy. Back to the foundations: S5 — Statistics for NLP Evaluation (significance testing for the metrics defined here) and Statistical Laws of Language (perplexity's statistical roots).
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
Computing…
Precision, Recall, F1.
- 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) | 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
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.
| 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 |
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.
🎓 Practice ladder
5 graded rungs · ~47 minNow 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.
Where next?
Later in Evaluation
This module unlocks
Go up a level