Evaluation metrics
- Without the right metric, you cannot tell if a model is actually improving on the task that matters
- Classification and regression require fundamentally different measurement frameworks
- Metrics like AUC-ROC separate ranking ability from threshold choices
Evaluation metrics are the yardstick for ML models. Classification metrics (accuracy, precision, recall, F1, AUC) measure different aspects of how well a model separates classes. Regression metrics (MAE, RMSE, R-squared) measure prediction distance from truth.
- Directly drives model selection and hyperparameter tuning
- Connects to the confusion matrix, ROC curves, and calibration
- Business KPIs map to specific metrics - picking the wrong one leads to deployments that fail in production
- Compute precision, recall, F1, accuracy, and AUC by hand from a confusion matrix.
- Decide between PR-AUC and ROC-AUC based on class imbalance.
- Pick the right regression metric (MSE, MAE, R^2) for an asymmetric loss.
- Distinguish discrimination from calibration and explain why both matter.
TL;DR
Choose your metric before you choose your model. Accuracy lies under class imbalance. PR-AUC is more informative than ROC-AUC when positives are rare. Calibration is a separate skill from discrimination and a separate failure mode. For ranking, NDCG and MRR are the defaults.
Intuition
Every metric is a one-dimensional projection of a multidimensional reality. Pick the wrong projection and you will optimise something irrelevant. Three principles cover most cases:
- Match the metric to the cost structure. False positives and false negatives rarely have equal cost.
- Watch for class imbalance. Anything that ignores the prior class rates will look fine while failing on the minority class.
- Discrimination is not calibration. A model can rank perfectly and still output miscalibrated probabilities.
Interactive: confusion matrix and threshold
Move the threshold slider to see precision, recall, F1, and accuracy update live. Notice how a single threshold change can flip you from a high-recall to a high-precision regime.
- precision = TP/(TP+FP) = 0.804
- recall = TP/(TP+FN) = 0.840
- F1 = 2·P·R/(P+R) = 0.822
- accuracy = (TP+TN)/N = 0.818
Interactive Demo
Explore the ROC curve and precision-recall curve for a simulated binary classifier. Drag the threshold slider to see how the operating point shifts, and observe the precision-recall trade-off in real time.
ROC Curve and PR Curve Demo
Move the threshold slider to see how the operating point shifts on the ROC and PR curves.
ROC Curve — FPR vs TPR. The dot marks the current threshold.
Classification metrics
Definitions and identities for classification
The confusion matrix
For a binary classifier with threshold , every prediction falls into one of four buckets:
| Predicted positive | Predicted negative | |
|---|---|---|
| Actual positive | TP (true positive) | FN (false negative) |
| Actual negative | FP (false positive) | TN (true negative) |
Counting metrics
Recall is also called sensitivity or true positive rate (TPR). The complementary false positive rate is
F1 and friends
The F1 score is the harmonic mean of precision and recall:
The generalised weights recall higher than precision when :
ROC-AUC and PR-AUC
The ROC curve plots TPR (recall) against FPR as the threshold sweeps over the score; the area under it is ROC-AUC. It equals the probability that a random positive example scores higher than a random negative example.
The PR curve plots precision against recall as the threshold sweeps; the area under it is PR-AUC. It is sensitive to class imbalance in a way ROC-AUC is not [1].
Log-loss (cross-entropy)
For a probabilistic classifier producing on example with true label ,
This is the training loss for almost every classification model and is also a meaningful evaluation metric: it penalises confident wrong predictions much more harshly than confident correct ones.
Calibration
A classifier is well calibrated if among examples it predicts with probability , the empirical positive rate is . Calibration is measured by reliability diagrams or Brier score:
Niculescu-Mizil and Caruana [3] showed that boosted trees and SVMs systematically miscalibrate (they push probabilities toward 0 and 1) and recommend Platt scaling or isotonic regression as a post-hoc fix.
Regression metrics
MSE, RMSE, MAE, R^2, and asymmetric losses
Squared and absolute errors
MSE/RMSE penalise large errors quadratically; MAE penalises them linearly. The optimal predictor under MSE is ; under MAE it is the conditional median.
Coefficient of determination
measures how much of the target variance the model explains relative to the constant predictor . It can be negative if the model is worse than the mean.
Mean absolute percentage error
Useful when scale matters across orders of magnitude (forecasting demand in different store sizes), but undefined when and unstable when is small.
Asymmetric losses
When the cost of over-prediction differs from under-prediction, use a quantile loss (pinball loss):
Minimising produces the conditional -quantile, which is the right answer when over- and under-predictions cost different amounts (inventory, capacity planning, risk pricing).
Ranking metrics
NDCG and MRR for ranked output
Discounted Cumulative Gain (DCG)
Given relevance scores for the top ranked items,
The numerator gives exponentially more weight to highly relevant items; the denominator discounts by rank.
Normalised DCG (NDCG)
NDCG normalises by the ideal ranking:
NDCG@k lies in and is the default offline metric for search and recommendation.
Mean Reciprocal Rank (MRR)
For queries each of which has one correct answer at rank ,
MRR is the right metric when there is a single correct answer per query (factoid QA, code completion).
Mean Average Precision (MAP)
For queries with multiple relevant items, where is the average of precision at each relevant retrieval. MAP rewards both precision and recall over the full ranking.
When to use what
| Metric | When to use | When NOT to use | Typical pitfall |
|---|---|---|---|
| Accuracy | Balanced classes, simple binary or multiclass tasks. | Imbalanced classes (class prior < 30%). | A constant predictor of the majority class scores the prior. 99% accuracy on a 99% negative dataset is meaningless. |
| Precision | When false positives are expensive (spam filter, ad fraud). | When you need to balance against recall, or as a single number alone. | Trivially maxed by predicting positive only on the most certain example. |
| Recall | When false negatives are expensive (cancer screening, fraud detection). | Alone, without a precision constraint. | Trivially maxed by predicting positive on everything. |
| F1 | Balanced view of precision and recall on the positive class. | Highly imbalanced classes where you care about probability calibration too. | F1 is threshold-dependent; report it at a fixed operating point. |
| ROC-AUC | Comparing rankers; relatively balanced classes. | Highly imbalanced classes (positive prior < 5%). | ROC looks great at 0.9 even when PR is at 0.3 because most negatives are easy. |
| PR-AUC | Imbalanced classes; positive class is the focus. | When you need calibrated probabilities. | PR is not threshold-invariant in the same way; baseline depends on prior. |
| Log-loss | Probabilistic classifiers where calibration matters. | Hard-classifier outputs (no probabilities). | Single confident-but-wrong prediction can dominate the average. |
| Brier score | Calibration assessment. | Pure ranking comparison. | Decomposes into reliability + resolution + uncertainty; report all three for clarity. |
| MSE / RMSE | Continuous targets, symmetric error cost. | Heavy-tailed target distributions where outliers should not dominate. | A few large errors swamp the average; check residual histograms. |
| MAE | Continuous targets where outliers should not dominate. | When you need to optimise the conditional mean. | Optimum is the conditional median, not the mean; can mislead if you needed the mean. |
| R^2 | Comparing across models on the same target distribution. | Comparing across different datasets or transformations of y. | Negative R^2 is possible and common when the model is worse than the mean baseline. |
| NDCG | Ranked-list outputs (search, recommendation). | Single-answer tasks. | NDCG@k changes with k; report multiple cutoffs. |
| MRR | Single correct answer per query (QA, retrieval). | Multi-answer or no-correct-answer tasks. | Insensitive to anything past the first correct answer. |
Three things that lie
A 99% accurate model on a 99%-negative dataset is the constant “negative” predictor. The metric is hiding all the failure on the minority class. Defaults:
- For imbalanced binary classification, report PR-AUC, not accuracy.
- Set the operating threshold by the cost of FP vs FN, not by 0.5 or by accuracy.
- Report metrics on slices, not just averages.
Hastie, Tibshirani, and Friedman [4] chapter 7 give the canonical statistical treatment.
When the positive class is below 5% of the data, ROC-AUC is dominated by easy negatives. A model can have ROC-AUC = 0.95 and PR-AUC = 0.30 on the same data. PR-AUC is more sensitive because the y-axis (precision) is the rate of positives among the model’s positive predictions, which is what users see [2].
Saito and Rehmsmeier give the canonical worked example: two classifiers identical in ROC-AUC but radically different in PR-AUC, with the difference making one usable in production and the other not.
Discrimination measures how well a model ranks positives above negatives. Calibration measures whether the model’s “70%” actually corresponds to 70% empirical positive rate.
These are independent. A boosted tree can have ROC-AUC 0.92 and be wildly miscalibrated [3]. If your downstream system thresholds the probability or sets prices proportional to it, calibration matters as much as discrimination. Fix with Platt scaling (sigmoid transform of scores) or isotonic regression (non-parametric monotonic mapping).
Pop quiz
- 1. A binary classifier on a fraud dataset (1% positive prior) reports ROC-AUC = 0.95 and PR-AUC = 0.30. Which is the more honest assessment?
- 2. You optimise a regression model with MAE instead of MSE. The optimal predictor for each x is now:
- 3. A classifier outputs probabilities. ROC-AUC is 0.90 but the Brier score is high and the reliability diagram shows the predicted 0.7 corresponds to 0.4 empirical positive rate. What is wrong?
- 4. Which metric is appropriate for evaluating a search-ranking model where each query has multiple relevant documents?
- 5. You are predicting demand for inventory planning, where over-stocking costs $1 and under-stocking costs $4. Which loss should you use?
Interview Q&A
A team reports their model has 99% accuracy. What questions do you ask?phone
In order:
- What is the class prior? A 99% negative dataset gives 99% accuracy to a constant predictor. The first question is whether 99% beats the trivial baseline meaningfully.
- What is precision, recall, F1, and PR-AUC on the positive class? Accuracy can hide complete failure on the minority class.
- At what threshold? A single accuracy number assumes a fixed cutoff. The threshold should be set by the cost of FP vs FN.
- On what data? Held-out, time-based, and sliced by demographic / geography to surface localized failures.
- Calibrated probabilities? If the system thresholds, prices, or ranks based on probability, calibration matters as much as accuracy.
A good model report has more than one number; a single 99% should always be challenged.
When would you use PR-AUC instead of ROC-AUC?onsite
When the positive class is rare (typically below 5-10%) and the cost is concentrated on the positive class. The argument is concrete [1][2]:
- ROC-AUC is the probability that a random positive scores higher than a random negative. With many easy negatives, this number is dominated by easy comparisons and looks high even when the model fails on hard cases.
- PR-AUC is sensitive to the prior because precision depends on TP / (TP + FP), and the FP count is sensitive to where you set the threshold relative to the negative score distribution.
Saito and Rehmsmeier 2015 show two classifiers with identical ROC curves but PR-AUCs of 0.3 and 0.7 on the same data. In a production fraud or rare-disease setting, one is usable, the other is not.
The general rule: if your audience cares about precision among predicted positives, report PR. If they care about ranking quality across the full distribution, report ROC. In rare-positive settings, prefer PR.
What does it mean for a probabilistic classifier to be calibrated, and how would you fix miscalibration?research
A classifier is perfectly calibrated if for every predicted probability , the empirical fraction of positives in examples receiving that prediction equals . Mathematically,
Calibration is measured by reliability diagrams (binned vs empirical positive rate) or Brier score (which decomposes into reliability + resolution + uncertainty).
Common causes of miscalibration:
- Boosted trees push probabilities toward 0 and 1 due to the multiplicative ensemble.
- SVMs produce decision-function scores, not probabilities; naive normalisation is biased.
- Class imbalance training tricks (oversampling, class weights) break calibration unless corrected.
Fixes [3]:
- Platt scaling. Fit a sigmoid on the validation set, mapping scores to calibrated probabilities. Two parameters; works well for SVMs and small datasets.
- Isotonic regression. Fit a non-parametric monotonic mapping from scores to probabilities. More flexible, better for large validation sets, no monotonicity assumption beyond rank-preserving.
- Train with a calibration-aware loss. Cross-entropy with no class re-weighting tends to produce calibrated probabilities natively; if you must re-weight, calibrate post hoc.
The general practice: train for discrimination (ROC-AUC, log-loss), calibrate post hoc, evaluate both separately.
Pick a metric for a model that recommends videos to users, and justify the choice.research
Defaults: NDCG@k as the offline ranking metric, paired with calibration on click-through probability if any downstream pricing or filtering uses probabilities.
The reasoning:
- Output is a ranked list, not a single label. Accuracy and F1 are wrong category. Ranking metrics are right category.
- Relevance is graded, not binary (some videos are partial-matches). NDCG handles graded relevance natively; MAP forces a binary cut.
- Position matters with diminishing returns. Users rarely scroll past the first 10 items. NDCG@10 captures both relevance and position.
- Calibration matters if the downstream system thresholds probabilities (e.g., to filter out low-confidence recommendations) or uses them for pricing or A/B-test analysis.
- Slice metrics by user segment (new vs returning), content category, and country. Global NDCG@10 = 0.5 can hide that a 5% segment is at 0.1.
Online, the offline NDCG should track an A/B test of click-through rate or watch time. If they do not correlate, the offline metric is mis-aligned and needs revisiting.
References
- Davis, J. and Goadrich, M. (2006). The Relationship Between Precision-Recall and ROC Curves. ICML 23, 233-240. doi:10.1145/1143844.1143874
- Saito, T. and Rehmsmeier, M. (2015). The Precision-Recall Plot Is More Informative than the ROC Plot When Evaluating Binary Classifiers on Imbalanced Datasets. PLoS ONE 10(3), e0118432. doi:10.1371/journal.pone.0118432
- Niculescu-Mizil, A. and Caruana, R. (2005). Predicting Good Probabilities With Supervised Learning. ICML 22. doi:10.1145/1102351.1102430
- Hastie, T., Tibshirani, R., and Friedman, J. (2009). The Elements of Statistical Learning (2nd ed.). Springer. https://hastie.su.domains/ElemStatLearn/