Redirect Machine Learning Evaluation - AI & ML Evaluation Roadmap 2026
← Back to Tutorials

2. Machine Learning Evaluation

Part 2

Classic ML evaluation is the foundation everything else builds on. This part covers the standard metrics for classification, regression, and ranking/retrieval tasks, plus the analysis practices — error analysis, slice evaluation, and robustness testing — that turn raw numbers into insight.

Classification Metrics

For binary classification, each example is either positive or negative, and each prediction is right or wrong. That gives four outcomes: true positive (TP), true negative (TN), false positive (FP), and false negative (FN).

Accuracy

Accuracy is the fraction of predictions that were correct:

accuracy = (TP + TN) / (TP + TN + FP + FN)

It is the easiest metric to understand, but it hides how the errors are distributed. If 95% of your data is negative, a model that always predicts "negative" scores 95% accuracy while learning nothing.

Precision & Recall

Precision answers "of everything the model called positive, how much was actually positive?"

precision = TP / (TP + FP)

Recall answers "of all the real positives, how many did the model find?"

recall = TP / (TP + FN)

They trade off against each other. Raising the decision threshold usually increases precision but lowers recall, and vice versa. Which one matters depends on the cost of each error type. For spam filters, false positives (deleting good mail) are worse than false negatives, so precision matters more. For cancer screening, false negatives are far more dangerous, so recall dominates.

F1 Score

F1 is the harmonic mean of precision and recall, giving a single number when you care about both:

F1 = 2 * (precision * recall) / (precision + recall)

Because it is a harmonic mean, F1 is pulled down hard when either metric is low — a model at precision 0.9 and recall 0.1 gets F1 ≈ 0.18, not 0.5.

ROC-AUC

ROC-AUC measures how well the model ranks examples across every decision threshold. It plots the true positive rate against the false positive rate; the area under that curve is the probability that a random positive is ranked above a random negative. A coin flip scores 0.5; a perfect ranker scores 1.0.

Strength: threshold-independent. Weakness: it weights both classes evenly, so it can look great on imbalanced data while the rare class is ignored.

PR-AUC

Precision-recall curves focus on the positive class. The area under the PR curve is much more informative than ROC-AUC when positives are rare, because precision reflects how often a positive prediction is actually right. When class balance is extreme, prefer PR-AUC.

Regression Metrics

Regression models predict continuous numbers. Let the true value be y and the prediction be y_hat; the error is y - y_hat.

MAE

Mean Absolute Error is the average absolute distance between predictions and truth. It is expressed in the same units as the target, so it is easy to explain — "on average we are off by 3 units."

MAE = mean(|y - y_hat|)

RMSE

Root Mean Squared Error squares the errors before averaging, which penalizes large mistakes far more heavily than small ones.

RMSE = sqrt(mean((y - y_hat)^2))

RMSE is always ≥ MAE; the gap grows as large errors appear. If RMSE is much larger than MAE, a few outliers are dominating your loss — decide whether that is the behavior you want.

R² Score

R² measures how much of the variance in the target the model explains, relative to simply predicting the mean.

R² = 1 - SS_residual / SS_total

An R² of 1 is a perfect fit, 0 means the model is no better than predicting the mean, and negative values mean it is worse. R² is convenient but can be inflated by the range of the data; it does not tell you whether the model is accurate in absolute terms.

Ranking & Retrieval Metrics

For search and recommendation systems, what matters is where the good items appear in an ordered list of K results.

MetricWhat it measuresWhen to use it
Recall@KFraction of relevant items found inside the top KHow complete the shortlist is
Precision@KFraction of the top K that are relevantHow clean the shortlist is
MRRReciprocal rank of the first relevant itemWhen the user only needs one good hit (Q&A)
MAPAverage precision across queries, averaged again over queriesOverall ranking quality across the whole set
NDCGRanked usefulness with discounting for lower ranksWhen relevance is graded, not binary

Recall@K & Precision@K

Recall@K checks whether the important items made it into the top K: divide the number of relevant items in the top K by the total number of relevant items. Precision@K divides the relevant items in the top K by K. A high-precision shortlist with low recall means you found only a few of the good items; high recall with low precision means you returned lots of junk to be thorough.

MRR

Mean Reciprocal Rank looks only at the first correct answer. If the first correct result is at position 2, the reciprocal rank is 1/2. It is ideal for "single-answer" tasks where the user is satisfied by the first relevant hit.

MAP

Mean Average Precision computes average precision for each query (precision at every relevant position, averaged), then averages across all queries. It rewards systems that put relevant items both early and densely.

NDCG

Normalized Discounted Cumulative Gain supports graded relevance. Relevant results get less credit the further down the list they sit (the discount), and the total is normalized against the ideal ordering so different queries are comparable. NDCG is the default choice for search evaluation with relevance grades.

Quick implementation tip: most of these metrics are one function call in scikit-learn or in the ranx library. Write a small helper script to compute the full metric family on a held-out query set, and log the output with each experiment.

Analysis

Error Analysis

Metrics tell you how much you are failing; error analysis tells you why. Sample misclassified examples, group them by pattern (e.g., "all failures involve negation," "all failures are very short texts"), and rank the groups by how many errors they explain. Fix the biggest groups first. Doing this before reaching for a bigger model often saves days of work.

Slice Evaluation

Overall accuracy hides uneven performance. Split the data into slices — by language, geography, device, text length, customer segment — and compute metrics per slice. A model can average 95% accuracy while failing badly for one demographic. If a slice underperforms, that is usually a data problem in that slice, not a general modeling problem.

Robustness Testing

Real-world input is messy: typos, slang, rephrasing, adversarial formatting. Robustness testing feeds perturbed versions of the same inputs — misspelled words, shuffled tokens, synonyms, added noise — and checks that predictions stay stable. If accuracy collapses under mild perturbation, the model is memorizing surface patterns instead of understanding.

Practice Task: Build a tiny classifier on a public dataset you know (e.g., spam or sentiment). Report accuracy, precision, recall, F1, and ROC-AUC. Then split your test set into two slices (e.g., by message length) and show how each metric changes per slice. Write one paragraph on what the slice results reveal that the overall numbers hid.