Evaluation Metrics Guide: Which Metric to Use and When
Accuracy is rarely the right metric. This guide explains every major ML evaluation metric - classification, regression, ranking, and generation - with clear guidance on when to use each one.
The wrong metric ruins a model. A fraud detection model with 99.9% accuracy might catch zero fraud cases if 0.1% of transactions are fraudulent. Choosing the right metric is a product decision, not just a statistics one. This guide explains what each metric measures, when it applies, and what it misses.
Classification Metrics
Confusion Matrix
Every classification metric derives from the confusion matrix:
Predicted Positive Predicted Negative
Actual Positive TP FN
Actual Negative FP TN
- TP (True Positive): correctly predicted positive
- TN (True Negative): correctly predicted negative
- FP (False Positive): predicted positive, actually negative (Type I error)
- FN (False Negative): predicted negative, actually positive (Type II error)
pythonfrom sklearn.metrics import confusion_matrix cm = confusion_matrix(y_true, y_pred)
Accuracy
Accuracy = (TP + TN) / (TP + TN + FP + FN)
Use when: classes are balanced and all errors cost equally. Avoid when: class imbalance exists (99% negative → predict all negative → 99% accuracy, zero usefulness).
Precision
Precision = TP / (TP + FP)
Of all the positive predictions, how many were actually positive?
Use when: false positives are costly. Example: spam detection - you don't want to mislabel legitimate email as spam.
Recall (Sensitivity, True Positive Rate)
Recall = TP / (TP + FN)
Of all actual positives, how many did the model find?
Use when: false negatives are costly. Example: cancer screening - missing a cancer case is worse than a false alarm.
F1 Score
F1 = 2 · (Precision · Recall) / (Precision + Recall)
Harmonic mean of precision and recall. Punishes extreme imbalances between the two.
Use when: you need a single metric that balances precision and recall. Default choice for imbalanced binary classification.
F-beta: Fβ = (1 + β²) · (P · R) / (β² · P + R). β > 1 weights recall higher; β < 1 weights precision higher.
AUC-ROC
The ROC curve plots True Positive Rate (recall) vs. False Positive Rate at every classification threshold.
AUC-ROC = area under the ROC curve range: [0.5, 1.0]
- 1.0 = perfect classifier
- 0.5 = no better than random
- < 0.5 = worse than random (flip predictions)
Use when: you need threshold-independent evaluation. Good for comparing models on binary classification. Avoid when: class imbalance is severe - use Precision-Recall AUC instead.
pythonfrom sklearn.metrics import roc_auc_score auc = roc_auc_score(y_true, y_pred_proba)
Precision-Recall AUC
Area under the Precision-Recall curve. Better than ROC-AUC for heavily imbalanced datasets.
Use when: positive class is rare (fraud, disease, rare events).
Log Loss (Cross-Entropy Loss)
Log Loss = -1/n · Σ [yᵢ log(ŷᵢ) + (1-yᵢ) log(1-ŷᵢ)]
Penalizes confident wrong predictions heavily.
Use when: you care about calibration - the model's probability estimates should reflect true probabilities.
Multi-class Metrics
pythonfrom sklearn.metrics import f1_score f1_score(y_true, y_pred, average='macro') # unweighted mean per class f1_score(y_true, y_pred, average='weighted') # weighted by class support f1_score(y_true, y_pred, average='micro') # global TP/FP/FN
- Macro: treats all classes equally. Use when you care equally about rare and common classes.
- Weighted: weights by class frequency. Use when class frequency matches real-world distribution.
- Micro: aggregates globally. Dominated by majority class.
Regression Metrics
MAE (Mean Absolute Error)
MAE = 1/n · Σ |yᵢ - ŷᵢ|
Average absolute deviation. Easy to interpret: "on average, predictions are off by X units."
Use when: outliers should not dominate. Robust to extreme values.
RMSE (Root Mean Squared Error)
RMSE = √(1/n · Σ (yᵢ - ŷᵢ)²)
Penalizes large errors more heavily than MAE. In the same units as the target.
Use when: large errors are disproportionately bad (e.g., predicting delivery times - being off by 2 hours is much worse than being off by 30 minutes twice).
R² (Coefficient of Determination)
R² = 1 - (Σ (yᵢ - ŷᵢ)²) / (Σ (yᵢ - ȳ)²)
Fraction of variance explained by the model. R² = 1 is perfect; R² = 0 means the model does no better than predicting the mean.
Caution: R² can be negative (model is worse than just predicting the mean). It always increases with more features - use Adjusted R² for feature selection.
MAPE (Mean Absolute Percentage Error)
MAPE = 1/n · Σ |yᵢ - ŷᵢ| / |yᵢ| · 100%
Avoid when: true values can be zero or near zero (division by zero).
Ranking and Recommendation Metrics
Precision@K and Recall@K
For recommendation systems: of the top K items recommended, how many are relevant?
Precision@K = (relevant items in top K) / K
Recall@K = (relevant items in top K) / (total relevant items)
NDCG (Normalized Discounted Cumulative Gain)
Measures ranking quality, with higher-ranked items contributing more to the score.
DCG@K = Σᵢ₌₁ᴷ (2^relᵢ - 1) / log₂(i + 1)
NDCG@K = DCG@K / IDCG@K (IDCG = ideal ordering)
Use when: order matters (search results, feed ranking). Items ranked first should be the most relevant.
MAP (Mean Average Precision)
Average of Precision@K at each position where a relevant item is found, averaged across queries.
Use when: evaluating information retrieval systems across many queries.
Hit Rate@K
Fraction of users for whom at least one relevant item appears in top K recommendations.
Simple but useful for cold-start evaluation and A/B testing.
Language Model Metrics
Perplexity
Perplexity = exp(cross-entropy loss)
Measures how surprised the model is by the test data. Lower = better. A perplexity of 100 means the model is as uncertain as choosing uniformly over 100 options.
Use for: comparing language models on the same test set. Not useful for comparing across datasets.
BLEU (Bilingual Evaluation Understudy)
Measures n-gram overlap between generated and reference text.
Limitation: does not capture semantic similarity. "The cat sat" and "A feline rested" have zero BLEU overlap.
ROUGE
Recall-oriented BLEU variant. Standard for summarization evaluation.
- ROUGE-N: n-gram recall
- ROUGE-L: longest common subsequence
Limitation: same problem as BLEU - surface-level overlap, not semantic quality.
Human Evaluation
For LLM outputs, task-specific human evaluation (correctness, helpfulness, harmlessness) remains the gold standard. Use automatic metrics for iteration speed; use human evaluation before shipping.
Quick Decision Guide
| Situation | Recommended Metric |
|---|---|
| Binary classification, balanced classes | F1 + AUC-ROC |
| Binary classification, imbalanced | PR-AUC + Recall@threshold |
| Fraud, disease detection (FN costly) | Recall, Sensitivity |
| Spam, content moderation (FP costly) | Precision |
| Multi-class classification | Macro F1 or Weighted F1 |
| Regression, outliers exist | MAE |
| Regression, large errors critical | RMSE |
| Search and ranking | NDCG@10, MAP |
| Recommendation systems | NDCG@K, Hit Rate@K |
| Language generation | Human eval + ROUGE/BLEU for CI |
| Probability calibration matters | Log Loss + Calibration curve |
Always report multiple metrics. A model that maximizes one metric often degrades another. Present results at a fixed operating point (e.g., threshold = 0.5 or threshold for 90% precision) so comparisons are meaningful.
Common Mistakes
Using accuracy on imbalanced datasets. A model that always predicts "no fraud" on a dataset that is 99.9% non-fraudulent will score 99.9% accuracy while being completely useless. Precision, recall, and F1 score reveal how well the model performs on the minority class that actually matters.
Optimizing for the wrong metric during training. Maximizing AUC during training does not guarantee the model performs well at the specific decision threshold you will use in production. Always evaluate precision and recall at the operating threshold your business actually requires, not just the aggregate curve.
Not committing to a decision threshold before evaluation. Reporting only AUC sidesteps the real deployment question: at what score do you take action? A threshold must be chosen before you can meaningfully compare models on a classification task, and that choice should be driven by the relative cost of false positives versus false negatives.
Stay in the loop
Get new ML/AI lessons in your inbox.
No account needed. We will send curriculum updates, launch notes, and practical learning resources.
Related Posts
More postsModel Selection Guide: When to Use Which ML Algorithm
A practical decision framework for choosing the right machine learning algorithm - from linear models to gradient boosting to neural networks - based on your data, constraints, and goals.
Python ML Quick Reference
The NumPy, Pandas, and scikit-learn one-liners you reach for every day - organized by task so you spend less time searching and more time building.
Math for ML: The Cheat Sheet Every Practitioner Needs
The math you actually use in ML - vectors, matrices, gradients, probability, and the key calculus rules - distilled into a single reference you can return to again and again.