Skip to content
Lambdian

Evaluating models: metrics that matter

Confusion matrices, precision and recall, F1, ROC and AUC, and regression errors.

Animated explanation

The idea

"95% accurate" can be meaningless. If 95% of transactions are legitimate, a model that always says "legitimate" is 95% accurate and catches zero fraud.

Good evaluation starts from the cost of each kind of mistake. Missing a tumour (false negative) is far worse than an extra scan (false positive); blocking a real customer may be worse than letting a small fraud through. The confusion matrix counts each kind of mistake, and metrics like precision, recall and AUC summarise it for the decision you actually face.

Classification: beyond accuracy

  • Precision answers "when the model raises an alarm, how often is it right?"
  • Recall (sensitivity, true positive rate) answers "of all real positives, how many did it catch?"
  • Specificity is recall for the negative class; false positive rate = 1 − specificity.
  • F1 combines precision and recall; use FβF_\beta to weight recall β times more.

Most classifiers output a score; the threshold is a business decision. Pick it on validation data to meet a target (e.g. recall ≥ 95%) or to minimise expected cost.

Threshold-free views: ROC and PR

The ROC curve plots true positive rate against false positive rate as the threshold sweeps from high to low; its area (AUC) is the probability that a random positive gets a higher score than a random negative. With heavy class imbalance, the precision–recall curve (and average precision) is more informative, because it ignores the huge pool of easy negatives.

For multi-class problems, compute per-class metrics and average them: macro (each class equal) or weighted (by class size).

Regression and calibration

For numbers, report RMSE (same units as the target, punishes large errors), MAE (robust to outliers) and R2R^2. For probabilities, check calibration: among cases given a 70% probability, about 70% should be positive. Log-loss and the Brier score reward calibrated probabilities.

The algorithm, step by step

  1. 1
    Fix the metric from the problem's costs before modelling.
  2. 2
    Compute scores on validation data; build the confusion matrix at candidate thresholds.
  3. 3
    Choose the threshold that meets the operating target (cost, recall or precision).
  4. 4
    Report the chosen metrics on the test set, with confidence intervals (bootstrap) where possible.

The maths

Precision, recall, F1

P=TPTP+FP,R=TPTP+FN,F1=2PRP+RP = \frac{TP}{TP+FP},\quad R = \frac{TP}{TP+FN},\quad F_1 = \frac{2PR}{P+R}

ROC axes

TPR=TPTP+FN,FPR=FPFP+TN\mathrm{TPR} = \frac{TP}{TP+FN},\qquad \mathrm{FPR} = \frac{FP}{FP+TN}

AUC as a ranking probability

AUC=1n+n−∑i∈+∑j∈−(1[si>sj]+121[si=sj])\mathrm{AUC} = \frac{1}{n_+n_-}\sum_{i\in +}\sum_{j\in -}\Big(\mathbb{1}[s_i > s_j] + \tfrac12\mathbb{1}[s_i = s_j]\Big)

Log-loss

−1n∑i[yilog⁡pi+(1−yi)log⁡(1−pi)]-\frac1n\sum_i \big[y_i\log p_i + (1-y_i)\log(1-p_i)\big]

Strengths and weaknesses

Strengths

  • Precision/recall map directly onto real costs
  • AUC compares rankers independent of threshold
  • Confusion matrices make error types visible

Weaknesses

  • Accuracy misleads on imbalanced data
  • ROC can look optimistic when positives are rare
  • A single number never tells the whole story

Reach for it when…

Every classifier and regressor you report on. Choose the metric from the decision the model supports.

Check your understanding

Question 1

Q11% of emails are phishing. A model flags none of them. Its accuracy is…

Question 2

Q2For cancer screening, which metric would you insist stays very high?

Question 3

Q3AUC = 0.5 means…

Lambdian Lab

Metrics from scratch

Implement the core metrics for binary labels in {0, 1} and compare with scikit-learn.

  • confusion(y, y_pred) → (tp, fp, fn, tn)
  • precision_recall_f1(y, y_pred) → (p, r, f1); return 0.0 where a denominator is 0.
  • roc_auc(y, scores): the rank formula (count pairs where a positive outscores a negative; ties count ½).
  • best_threshold(y, scores, min_recall): the highest threshold among the scores whose recall is at least min_recall.
Open the labPython + NumPy + scikit-learn runs in your browser. Sign-in keeps your code and attempts.