Stop 08 · Supervised Learning: ClassificationTopics · 6/9

Probability Calibration & Decision Thresholds

Part of the Machine Learning Roadmap

In short

A loan model says 0.7. Does that mean a 70% chance of default? Only if it's calibrated. And should you reject at 0.5? Only if a missed defaulter costs the same as a lost good customer, which it almost never does.

A calibrated model's 0.8 really means about 80% of such cases are positive, which matters when probabilities drive decisions. The default 0.5 threshold is rarely best, so you pick a threshold based on the cost of false positives and false negatives.

The jaadu stands at a doorway holding a half-open umbrella, looking up at grey monsoon clouds while weighing a phone in its other hand.

Think of it like this

The weather forecaster's 70%

A good forecaster who says '70% chance of rain' on 100 days should see rain on about 70 of them. That's calibration. Whether you carry an umbrella is a separate decision: if getting wet ruins a ₹20,000 phone, you carry it at 30%. If it's just a walk to the bus stop, maybe only at 80%. That personal cut-off is the threshold.

Watch it happen

Moving the threshold on a fraud model

0.1
0
0.2
1
0.5
2
0.8
3

1/5Each value is a possible threshold. Above it, a transaction is held for review. A missed fraud costs ₹2,000, a needless review costs ₹500.

The big ideas

What you need to know

Calibrated means honest

Among all cases scored 0.8, about 80% should be positive. Logistic regression is usually close. Trees, boosting and SVMs often aren't.

Check with a reliability curve

calibration_curve bins predictions and compares the average score to the actual positive rate. A good model hugs the diagonal.

Fix with CalibratedClassifierCV

Wrap the model and use 'sigmoid' (Platt) for small data or 'isotonic' when you have plenty of data.

Threshold from costs

Choose the cut-off that minimises business cost or meets a target like '90% recall', using validation data.

See it in code

Walk through the code

python
1import numpy as np2from sklearn.calibration import CalibratedClassifierCV, calibration_curve3from sklearn.datasets import make_classification4from sklearn.ensemble import RandomForestClassifier5from sklearn.model_selection import train_test_split6 7X, y = make_classification(n_samples=20000, weights=[0.95], random_state=0)8X_tr, X_val, y_tr, y_val = train_test_split(X, y, stratify=y, random_state=0)9 10rf = RandomForestClassifier(n_estimators=300, random_state=0)11model = CalibratedClassifierCV(rf, method="isotonic", cv=5).fit(X_tr, y_tr)12proba = model.predict_proba(X_val)[:, 1]13 14frac_pos, mean_pred = calibration_curve(y_val, proba, n_bins=10)15print(np.round(mean_pred, 2), np.round(frac_pos, 2))16 17cost_fn, cost_fp = 2000, 500          # missed fraud vs needless review, in rupees18best = min(19    np.arange(0.05, 0.95, 0.05),20    key=lambda t: cost_fn * np.sum((proba < t) & (y_val == 1))21                + cost_fp * np.sum((proba >= t) & (y_val == 0)),22)23print("Best threshold:", round(best, 2))   # near 500 / 2500 = 0.2

1/4A rare-positive problem, split with stratify so both sets keep the 5% positive rate.

Calibrate a random forest, check the reliability curve, then choose the threshold by rupee cost.

Save yourself the pain

Common mistakes

Oops

Using model.predict() and accepting the hidden 0.5 threshold.

Do this instead

Use predict_proba and choose a threshold on validation data based on the real costs of each mistake.

Oops

Tuning the threshold on the test set.

Do this instead

Pick it on a validation set or with cross-validation, then report the final score once on the untouched test set.

Oops

Treating a boosting model's raw score as a true probability.

Do this instead

Check a calibration curve first and calibrate if the curve bends away from the diagonal.

Your mission

Price your mistakes

~30 min

Quick check

Missing a defaulter costs ₹1,00,000 and rejecting a good applicant costs ₹5,000. How should the threshold for 'reject' move from 0.5?

Go deeper

  1. 1Draw a calibration curve with CalibrationDisplay for two different models.
  2. 2Use CalibratedClassifierCV and compare the curves again.
  3. 3Choose a threshold that hits a target recall on validation data.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge