Sub topic 1
From Scores to Probabilities
Prerequisites
This chapter assumes binary and multiclass classification basics, train/validation/test splits, cross-validation, and basic Python with NumPy and scikit-learn.
From scores to probabilities
A classifier often produces a score, which is a value used to rank examples. A score of 3.2 may rank an example above one with a score of 1.1, but it does not necessarily mean “there is a 3.2 probability of class 1.”
A probability estimate is a value from 0 to 1 that represents the model's predicted chance of an event. Models can convert scores into probabilities, but the conversion may be inaccurate. A model that predicts 0.8 should be treated differently from one that only produces an arbitrary score.
from sklearn.calibration import calibration_curve
import numpy as np
y_true = np.array([0, 1, 1, 0, 1, 1, 0, 0])
y_prob = np.array([0.2, 0.7, 0.6, 0.4, 0.9, 0.8, 0.3, 0.1])
fraction, mean_probability = calibration_curve(
y_true, y_prob, n_bins=4, strategy="uniform"
)
for predicted, observed in zip(mean_probability, fraction):
print(f"predicted={predicted:.2f}, observed={observed:.2f}")What it means for probabilities to be calibrated
A classifier is calibrated when predictions near probability \(p\) are correct about \(p\) of the time. Among all cases receiving a probability of 0.8, about 80% should be positive.
Calibration describes the reliability of probability values, not whether every individual prediction is correct.
Calibration is not accuracy or AUC
Accuracy measures the fraction of correct class labels after choosing a threshold. AUC measures how well the model ranks positive examples above negative examples. Neither metric checks whether a predicted 0.8 corresponds to an 80% event rate.
| Metric | Main question |
|---|---|
| Accuracy | How often are thresholded labels correct? |
| AUC | How well are examples ranked? |
| Calibration | Do predicted probabilities match observed frequencies? |
Why classifiers end up miscalibrated
A model may be miscalibrated because of model assumptions, regularization, class imbalance, overfitting, or limited training data. Tree ensembles and neural networks can produce highly useful rankings while producing probabilities that are too extreme. Changes in the data distribution can also invalidate calibration learned from older data.
Reading a reliability diagram
A reliability diagram groups predictions into probability bins. It plots each bin's mean predicted probability against its observed fraction of positives. Points near the diagonal indicate good calibration. A point above the diagonal means the model predicted probabilities that were too low. A point below it means they were too high.
Exercise
A bin has mean predicted probability 0.7, but 0.55 of its examples are positive. Is the model overconfident or underconfident?
Show answer
It is overconfident in that bin because its predicted probability is higher than the observed positive fraction.