Evaluating a Text Classifier and Retriever: Metrics and Reporting
Machine LearningModel answerLast updated
Work through agreement, classification and ranking metrics, TF-IDF, reproducibility and model comparison for a document classification and search system, showing the arithmetic.
You are reviewing the evaluation of a system that classifies documents as relevant or not relevant and also ranks documents for search queries. Walk through each area below, state your assumptions, and show the arithmetic.
Assumptions: offline evaluation, a held-out test set, binary labels unless stated otherwise, and no special infrastructure.
-
Label agreement. Two annotators label 100 documents. Both say "relevant" on 40, both say "not relevant" on 45, A says relevant and B says not on 10, and A says not and B says relevant on 5. Compute Cohen's kappa and explain what it tells you that raw agreement does not.
-
Classification metrics. On 1,000 test documents the confusion matrix is TP = 80, FP = 20, FN = 40, TN = 860. Compute precision, recall, F1 and accuracy. Compare accuracy to a trivial baseline, and say which metrics you would report if positives are rare and missed positives are costly. Explain macro versus micro averaging for multi-class problems.
-
Reproducibility. List what you would pin down and log so that another engineer can rerun the experiment and obtain the same numbers, and how you would report run-to-run variance.
-
TF-IDF. In a corpus of 4 documents, the term "arbitration" appears in exactly 1 document, 3 times among 100 tokens. Compute its TF-IDF weight (natural log) and explain what happens to a term present in every document.
-
Ranking metrics.
- Binary relevance: the top 5 results are [relevant, not, relevant, not, not], and 3 relevant documents exist in total. Compute Precision@5, Recall@5, reciprocal rank and average precision.
- Graded relevance: grades at ranks 1 to 5 are [2, 0, 3, 0, 1]. Compute NDCG@5 using gain = rel and discount = log2(rank + 1).
- Say when you would prefer MRR, NDCG, or MAP/Recall@k.
-
Comparing models. Model A scores 0.805 and Model B scores 0.812 accuracy on the same 2,000 test items. Can you claim B is better? Describe an appropriate test and what a good experiment report contains.
Close with how you would validate your answers and which pitfalls you would watch for.
Hints
Hint 1
Kappa is (p_o - p_e) / (1 - p_e). Get p_e from each annotator's marginal yes and no rates, not from the diagonal of the table.
Hint 2
Compare accuracy to the all-negative baseline before judging it. For ranking, average precision divides by all relevant documents, not only the retrieved ones, and NDCG divides DCG by the DCG of the ideal ordering.
Hint 3
For the model comparison, compute the standard error of an accuracy and compare it to the gap. Because both models scored the same items, use a paired method such as McNemar's test or a paired bootstrap.
Model answer
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Assumptions
Document classification plus search, offline evaluation, a held-out test set, binary labels unless stated. I state these first because the question is broad, and metric choice depends on them.
1. Annotation agreement (Cohen's kappa)
- Observed agreement: p_o = (40 + 45) / 100 = 0.85.
- A says "yes" 50% of the time and B says "yes" 45% of the time.
- Chance agreement: p_e = 0.50·0.45 + 0.50·0.55 = 0.50.
- κ = (0.85 − 0.50) / (1 − 0.50) = 0.70, which is substantial agreement.
Raw agreement overstates quality because annotators agree by chance. Common mistakes: reporting 85% as if it were kappa; using Cohen's kappa with more than two annotators (use Fleiss' kappa or Krippendorff's alpha); ignoring that kappa falls under heavy class imbalance (the prevalence paradox). Label reliability also caps how meaningfully any model can be evaluated.
2. Classification metrics
- Precision = 80/100 = 0.80
- Recall = 80/120 = 0.667
- F1 = 2·0.80·0.667 / (0.80 + 0.667) = 0.727
- Accuracy = 940/1000 = 0.94
The all-negative baseline already gets 880/1000 = 0.88, so 0.94 is much less impressive than it looks. With rare positives, report precision, recall, F1 and PR-AUC (more informative than ROC-AUC when positives are rare), and state the operating threshold. If a missed document costs more than an extra review, choose the threshold with a recall-weighted F-beta (β > 1).
For multi-class: macro-F1 averages per-class F1 equally and exposes weak rare classes; micro-F1 pools counts, is dominated by large classes, and equals accuracy for single-label problems.
3. Reproducibility
- Randomness: fix seeds (Python, NumPy, framework), use deterministic kernels where possible, and acknowledge residual GPU and data-loader nondeterminism.
- Data: version the dataset with a hash, freeze splits, and split by group (document, author, time) so near-duplicates do not leak across train and test.
- Environment: pin library versions, log the git commit, configuration and hyperparameters.
- Variance: run several seeds (about 5) and report mean ± std rather than the best run.
Validate by rerunning from the logged artifacts and checking that the metrics match within seed variance.
4. TF-IDF
- tf = 3/100 = 0.03
- idf = ln(4/1) = 1.386
- tf-idf ≈ 0.03 × 1.386 ≈ 0.0416
A term in all 4 documents has idf = ln(1) = 0, so it carries no discriminative weight. Related points: subword tokenization (BPE, WordPiece) handles rare terms; stop-word removal helps bag-of-words models but can hurt transformers; TF-IDF and BM25 are strong cheap baselines that dense embeddings should be compared against.
5. Ranking metrics
Binary relevance, top 5 = [R, N, R, N, N], 3 relevant in total:
- Precision@5 = 2/5 = 0.40
- Recall@5 = 2/3 = 0.667
- Reciprocal rank = 1/1 = 1.0
- Average precision: precision at the relevant hits is 1 (rank 1) and 2/3 (rank 3). Divide by all 3 relevant documents: (1 + 0.667)/3 = 0.556.
Graded relevance, grades [2, 0, 3, 0, 1]:
- DCG = 2/1 + 3/2 + 1/2.585 ≈ 3.887
- Ideal order [3, 2, 1, 0, 0]: IDCG = 3 + 2/1.585 + 1/2 ≈ 4.762
- NDCG@5 ≈ 3.887 / 4.762 ≈ 0.816
Choosing: MRR when only the first good result matters; NDCG for graded relevance where position matters; MAP or Recall@k for exhaustive review tasks.
Mistakes to avoid: dividing AP by the number of retrieved relevant items instead of all relevant items; mixing the linear gain (rel) with the exponential gain (2^rel − 1) without saying which; averaging NDCG over queries that have no relevant documents.
6. Comparing models
The standard error of an accuracy near 0.81 on 2,000 items is sqrt(0.81·0.19/2000) ≈ 0.0088. The 0.007 gap is below one standard error, so you cannot claim B is better. Since both models were scored on the same items, use a paired approach: McNemar's test on the disagreement counts, or a paired bootstrap over items giving a 95% CI for the difference.
A good report includes:
- Majority-class and BM25/TF-IDF baselines.
- Mean ± std over seeds.
- A confidence interval on the difference.
- Per-slice results (document type, length, language).
- Error analysis with examples.
- A test set used once, after tuning on validation data only.
Distinguish statistical significance from practical significance: with large test sets tiny differences become significant but may not matter.
Validation and pitfalls
Check each number against a hand-worked example like those above, unit-test metric code on tiny fixtures with known answers, and compare against baselines and chance. The main risks are weak labels, leakage between splits, a threshold that ignores error costs, and reporting a single lucky run.
Follow-ups
- At larger scale: double-label only a stratified sample to monitor kappa; compute metrics in batch jobs; use pooled judgments for retrieval; with narrow CIs, focus on effect size and slice regressions.
- In production: monitor input drift (token distribution, out-of-vocabulary rate, length), prediction drift (class shares over time), and sampled human-labeled quality checks.