Sub topic 16
Why the probabilities are poorly calibrated, and how to fix it
This chapter assumes you know the naive Bayes decision rule, log-space scoring, and how to train a model in scikit-learn. It also assumes you know what a train/test split is.
What calibration means
A classifier is calibrated if its predicted probabilities match observed frequencies. Take all the test examples where the model says 0.8. About 80% of them should belong to the positive class.
Naive Bayes often ranks examples correctly, so its accuracy can be good. Its probabilities, though, tend to be pushed toward 0 and 1.
Why naive Bayes is overconfident
The model multiplies one likelihood per feature. That is only correct if the features are independent given the class. When features are correlated, the same evidence is counted several times.
Suppose two features are exact copies of each other. Each one favors class A over class B by a factor of 4. The true evidence is a factor of 4, but the model multiplies them and reports 16. With many correlated features, such as related words in a document, the posterior moves to nearly 0 or 1 even when the real evidence is weak.
The ranking of classes usually survives this distortion. The decision rule only needs the larger score, so double counting often does not change which class wins. This is why accuracy can stay high while probabilities are poor.
Measuring calibration
Two common checks:
- A reliability curve. Bin the predicted probabilities and plot the observed positive rate in each bin. A calibrated model lies on the diagonal. In scikit-learn, use
sklearn.calibration.calibration_curve. - The Brier score. This is the mean squared difference between the predicted probability and the 0/1 outcome. Lower is better.
Fixing it with a calibrator
You fit a second, small model that maps raw scores to better probabilities. Two methods are standard:
| Method | What it fits | Best when |
|---|---|---|
| `"sigmoid"` (Platt scaling) | A logistic curve | Little data, or distortion that is roughly S-shaped |
| `"isotonic"` | A monotone step function | Plenty of data (about 1,000 or more calibration samples) |
CalibratedClassifierCV handles the cross-validation for you. It trains the calibrator on folds that the base model did not see.
from sklearn.calibration import CalibratedClassifierCV
from sklearn.datasets import make_classification
from sklearn.metrics import brier_score_loss
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
X, y = make_classification(
n_samples=5000,
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.4, random_state=0
)
raw = GaussianNB().fit(X_train, y_train)
calibrated = CalibratedClassifierCV(
GaussianNB(), method="isotonic", cv=5
).fit(X_train, y_train)
for name, model in [("raw", raw), ("calibrated", calibrated)]:
p = model.predict_proba(X_test)[:, 1]
print(name, "Brier score:", round(brier_score_loss(y_test, p), 4))The redundant features here are linear combinations of the informative ones, so they break the independence assumption on purpose. The calibrated Brier score is typically lower than the raw one. Check both numbers on your own data before relying on this.
Calibration changes the probabilities, not the underlying model. Accuracy may change slightly, but its main effect is to make predict_proba trustworthy for thresholds, costs, and risk estimates.
Exercise
Two features are exact copies of each other, and each has a likelihood ratio of 3 in favor of class A. What combined ratio does naive Bayes use, and what is the true ratio?
Show answer
Naive Bayes multiplies the ratios and uses 3 × 3 = 9. The true evidence is only 3, because the second feature adds no new information. This double counting is why the predicted probabilities are too extreme.