Sub topic 1
From scores to probabilities
Prerequisites
This chapter assumes you know the following:
- Binary and multiclass classification basics.
- Train, validation and test splits.
- The sigmoid and softmax functions, and logistic regression.
- Basic Python with NumPy and scikit-learn.
Scores are not probabilities
A classifier usually returns a number before it returns a label. That number is a score. Scores come in different forms:
- A margin from a linear SVM, which can be any real number.
- A logit from a neural network, which is the value before sigmoid or softmax.
- A vote fraction from a random forest.
- A value from
predict_proba, which lies between 0 and 1.
Only the last form looks like a probability. Looking like one is not the same as being one. A value between 0 and 1 that sums to 1 across classes satisfies the format of a probability. It does not guarantee that the number means anything about how often the model is right.
A predicted probability is a number p in [0, 1] that the model reports as its belief that an example belongs to the positive class. We want it to be honest. If the model says 0.8 for many different examples, about 80% of them should be positive. That property is called calibration, and the next chapter defines it precisely.
How scores become probabilities
Models squash raw scores into the unit interval with a fixed function:
- Binary logistic models apply the sigmoid,
p = 1 / (1 + exp(-z)). - Multiclass models apply softmax to a vector of logits.
These functions guarantee valid probabilities. They do not guarantee honest ones. The logit z comes from training, and training may reward confident outputs without rewarding accurate confidence.
A quick look at the problem
The following code trains a naive Bayes classifier and prints its probabilities next to the observed positive rate in each bin. Bins are built from quantiles, so each one holds the same number of examples.
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}")Each line compares what the model claimed with what happened. Naive Bayes tends to push probabilities toward 0 and 1, because it treats correlated features as independent. This dataset has 10 redundant features, so you should see the extremes drift away from the observed rates. The exact numbers depend on your library version.
Common errors
- Treating any
predict_probaoutput as a probability. Some estimators, such asSVCwithprobability=True, fit an extra calibration step internally. Others, such as random forests, return vote fractions that are often too timid near 0 and 1. - Assuming a good ranking means good probabilities. A model can order examples well and still report wrong numbers. This is covered in a later chapter.
- Using probabilities for decisions without checking them. Expected-cost calculations and thresholds assume the numbers are honest.
Exercise
A linear SVM returns a decision-function value of 2.3 for an example. Is that a probability? What would you need to do to get one you could trust?
Show answer
No. It is a margin, an unbounded score. You could map it to [0, 1] with a sigmoid, but the result is only a trustworthy probability if it is checked and corrected against held-out data, which is what calibration does.