Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Chapter 11: Metrics — Implementation

Open in Colab
  1. A classification report in one call

  2. Choosing a threshold from the costs

  3. Precision at k: when only the top of the list is checked

  4. Calibration: a reliability diagram, and recalibrating a weighted model

  5. Letting scikit-learn tune the threshold

  6. Which constant does each regression metric prefer?

  7. A confidence interval for any metric, by bootstrap

training 9800, validation 4200 (47 frauds), test 6000 (66 frauds)

The chapter’s fraud-like data, split three ways this time: the threshold will be chosen on validation data and reported on the test set.

1. A Classification Report

              precision    recall  f1-score   support

      normal      0.992     0.999     0.996      5934
       fraud      0.842     0.242     0.376        66

    accuracy                          0.991      6000
   macro avg      0.917     0.621     0.686      6000
weighted avg      0.990     0.991     0.989      6000

One call gives precision, recall and F1 per class, their macro and weighted averages, and the support (the number of true examples of each class). The fraud row is the one that matters here; the normal row and the accuracy are almost perfect and say nothing.

2. A Threshold From the Costs

Suppose a missed fraud costs 50 times a false alarm. The best threshold minimises 50⋅FN+FP50 \cdot FN + FP; we search it on the validation data, then apply it once to the test data.

best threshold on validation: 0.02
test: TP 47 FN 19 FP 451; cost 1401 against 2503 at the default 0.5

The default threshold of 0.5 assumes both mistakes cost the same. They rarely do. Choosing the threshold is a business decision made with a metric, not a property of the model.

3. Precision at k

A fraud team can check 100 transactions a day. What matters is how many of the 100 highest-scored are real frauds:

precision@10  1.00   (10 frauds among the top 10)
precision@50  0.56   (28 frauds among the top 50)
precision@100 0.35   (35 frauds among the top 100)
precision@200 0.20   (40 frauds among the top 200)

The model’s ranking is very good at the top of the list and degrades further down. When the number of checks is fixed, precision@k is the metric the users actually feel.

4. Calibration

A reliability diagram bins the predictions by predicted probability and plots, for each bin, the observed share of positives. A calibrated model follows the diagonal.

plain                        mean probability 0.0106 (true 0.0110)   Brier 0.0076
class weights                mean probability 0.2270 (true 0.0110)   Brier 0.1045
class weights, recalibrated  mean probability 0.0110 (true 0.0110)   Brier 0.0088
Source
<Figure size 520x400 with 1 Axes>

Class weights push every probability up; isotonic recalibration (fitted inside cross-validation) brings them back in line, while keeping the weighted model’s ranking.

5. Tuning the Threshold in scikit-learn

TunedThresholdClassifierCV searches the threshold that maximises a metric by cross-validation, and wraps the result as an ordinary classifier.

threshold chosen for F1: 0.211; test precision 0.636, recall 0.424, F1 0.509

6. Which Constant Does Each Metric Prefer?

A regression metric is also a statement about which prediction is “central”. The constant that minimises the mean squared error is the mean; the one that minimises the mean absolute error is the median. On a skewed target the two differ.

constant minimising MSE 152.1 (mean 152.1);  minimising MAE 140.1 (median 140.5)

So a model trained with squared error aims at conditional means, one trained with absolute error at conditional medians: choose the training loss to match the metric you will be judged by.

7. A Confidence Interval for Any Metric

A test-set metric is a measurement on a sample (Chapter 10). Resampling the test set with replacement, and recomputing the metric each time, gives a bootstrap interval.

average precision 0.472, 95% bootstrap interval [0.358, 0.600]

With only 66 frauds in the test set, the average precision could plausibly be anywhere in a wide range. Report the interval, especially when the positives are few.

Summary

  • classification_report gives per-class precision, recall, F1 and support.

  • Choose the threshold from the costs, on validation data.

  • Precision@k measures what a team with a fixed budget of checks will see.

  • Reweighting distorts probabilities; recalibration restores them.

  • TunedThresholdClassifierCV tunes the threshold for a chosen metric.

  • MSE favours means, MAE medians: train with the loss you will be judged by.

  • Bootstrap the test set to put an interval around any metric.