Sub topic 5
Reading a Reliability Diagram
This chapter assumes you know how to train a binary classifier in scikit-learn, what a predicted probability is, and how to split data into train and test sets. It covers how to build a reliability diagram and how to read the common shapes.
What the diagram shows
A calibrated model's predictions of 0.8 should be right about 80% of the time. A reliability diagram checks this directly. You sort the test predictions into bins by predicted probability. For each bin you plot two numbers:
- The x value is the mean predicted probability in the bin.
- The y value is the observed fraction of positives in the bin.
A perfectly calibrated model puts every point on the diagonal, where y equals x.
Building one in scikit-learn
The code below trains a naive Bayes classifier. This model is known to give overconfident probabilities. It then prints the points of the diagram.
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn.calibration import calibration_curve
X, y = make_classification(
n_samples=20000, n_features=20, n_informative=5, n_redundant=10, random_state=0
)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.5, random_state=0
)
model = GaussianNB().fit(X_train, y_train)
p = model.predict_proba(X_test)[:, 1]
frac_pos, mean_pred = calibration_curve(y_test, p, n_bins=10, strategy="quantile")
for m, f in zip(mean_pred, frac_pos):
print(f"mean predicted {m:.2f} -> observed fraction positive {f:.2f}")Always compute the curve on data the model did not train on. Training data makes most models look better than they are.
Choosing the binning strategy
calibration_curve supports two strategies.
| Strategy | Bin edges | Strength | Weakness |
|---|---|---|---|
| `uniform` | Equal width, such as 0.0 to 0.1 | Easy to read on the x axis | Sparse bins where few predictions fall, so noisy points |
| `quantile` | Equal count in each bin | Every point rests on the same amount of data | Bins can be very wide where predictions are rare |
Models that push most predictions toward 0 or 1 leave the middle bins nearly empty under uniform. Use quantile in that case. The code above uses it for that reason.
Reading the shapes
Compare the points with the diagonal.
- Points below the diagonal mean the model is overconfident on the high side. The model says 0.9, but only 70% of those cases are positive. Naive Bayes often looks like this.
- Points above the diagonal mean the model is underconfident. The model says 0.6, but 80% are positive.
- An S shape, with low values too low and high values too high, means the model is overconfident at both ends. In the opposite arrangement, an inverted S, the model is underconfident at both ends. Boosted trees and max-margin models such as SVMs often show the inverted S.
- A jagged line around the diagonal is usually noise from small bins, not miscalibration.
Limits of the diagram
The diagram shows calibration only. A model that always predicts the base rate, such as 0.3 when 30% of cases are positive, sits exactly on the diagonal at one point. It is calibrated but has no ability to separate the classes. The next chapters cover scores that combine both properties. The bin count also matters. Too few bins hide problems, and too many make each point noisy. Ten bins is a common starting point for tens of thousands of samples. Use fewer for small test sets.
Exercise
A reliability diagram built with strategy="uniform" shows the point at predicted 0.95 far below the diagonal, at 0.80. The bin holds 9 samples. Should you conclude the model is overconfident at high probabilities? What would you check or change?
Show answer
Not yet. Nine samples is too few for a reliable estimate, so the gap may be noise. Check the bin counts, try strategy="quantile" or fewer bins so each point has more data, and see whether the gap persists. If it does, the model is likely overconfident at the high end.