Interview's MateData Science
Interview's Mate

Data Science

Python → ML → MLOps

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Data Science topics
Lesson 118/ 160
06. Feature Engineering & Model Evaluation

Model Calibration (Platt Scaling & Isotonic)

Calibrate predicted probabilities so 70% really means ~70% with reliability diagrams.

AdvancedPlatt · Isotonic3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

Calibration checks whether a model's 70% really means 70%, and fixes it if the numbers are too confident or not confident enough.

Think of it like…

A weather forecaster whose "70% chance of rain" days actually see rain only 55% of the time: their ranking of wet versus dry days may be excellent, but the numbers can't be trusted for deciding whether to carry an umbrella or cancel an event.

Where you’ve seen it

A classifier's predicted probabilities fall into four bins (counts, mean predicted, observed rate): 400 cases at 0.1 with 5% positive, 300 at 0.3 with 22%, 200 at 0.5 with 41%, 100 at 0.7 with 60%. ECE = Σ (n_b/N) |observed − predicted| = (400 × 0.05 + 300 × 0.08 + 200 × 0.09 + 100 × 0.10)/1,000 = (20 + 24 + 18 + 10)/1,000 = 0.072: the model is systematically over-confident. For four cases with predictions 0.9, 0.2, 0.7, 0.4 and outcomes 1, 0, 1, 1, the Brier score is (0.01 + 0.04 + 0.09 + 0.36)/4 = 0.125. Platt scaling fitted on a validation set learns p = σ(2.0 s − 1.0): a raw score of 0.8 becomes σ(0.6) = 0.646. Isotonic regression on scores 0.1, 0.3, 0.4, 0.6, 0.8 with outcomes 0, 1, 0, 1, 1 pools the violating pair (1, 0) to 0.5 and produces the monotone fit [0, 0.5, 0.5, 1, 1]. For a neural network with a logit of 3.0 (p = 0.953), temperature scaling with T = 1.5 gives logit 2.0 (p = 0.881). After isotonic calibration with 5-fold CalibratedClassifierCV, ECE falls from 0.072 to 0.015 and the Brier score from 0.141 to 0.126, while AUC stays at 0.84.

Words you’ll hear

Reliability diagram
A plot of observed frequency against predicted probability across bins.
Brier score / log-loss
Proper scoring rules that reward accurate probabilities.
ECE
Weighted average gap between predicted probability and observed frequency across bins.
Platt / isotonic / temperature scaling
Post-hoc mappings that recalibrate scores or logits.

Visual diagram

Loading interactive diagram

Theory & deep dive

Measuring calibration

A reliability diagram (sklearn.calibration.calibration_curve) groups predictions into bins (uniform or quantile) and plots the observed positive rate against the mean predicted probability; a calibrated model follows the diagonal, points below it indicate over-confidence and above under-confidence. ECE summarises it: ECE = Σ_b (n_b/N) |acc_b − conf_b|. For the four bins in the example it equals (400 × 0.05 + 300 × 0.08 + 200 × 0.09 + 100 × 0.10)/1,000 = 0.072. ECE depends on the binning and has estimation noise with small samples, so also use proper scoring rules: Brier score = (1/N) Σ (pᵢ − yᵢ)² (0.125 for the four-case example) and log-loss = −(1/N) Σ [y ln p + (1 − y) ln(1 − p)], which penalise both poor ranking and poor calibration and can be decomposed into reliability, resolution and uncertainty.

Calibration and discrimination are separate: AUC depends only on the ordering of scores, so any strictly monotone transformation leaves it unchanged. A model can rank perfectly but report 0.99 for everything. Typical patterns: SVMs and boosted trees with margin losses push probabilities away from 0 and 1 less than they should (sigmoid-shaped distortion); random forests average votes and are under-confident near the extremes; naive Bayes is over-confident because it double-counts correlated features; neural networks are often over-confident; logistic regression is usually well calibrated if the model is correctly specified. Class weights, undersampling and SMOTE shift the base rate and break calibration.

ECE and Brier by hand

Bins (n, predicted, observed): (400, 0.1, 0.05), (300, 0.3, 0.22), (200, 0.5, 0.41), (100, 0.7, 0.60). ECE = 0.072. Brier for predictions 0.9, 0.2, 0.7, 0.4 and outcomes 1, 0, 1, 1 = (0.01 + 0.04 + 0.09 + 0.36)/4 = 0.125.

Key takeaway: Check reliability diagrams, ECE and proper scoring rules; calibration is distinct from ranking and common models need repair.

Platt, isotonic and temperature scaling in practice

Platt scaling fits p = σ(a·s + b) to the model's scores on a calibration set by logistic regression: two parameters, so it works with small samples and fixes sigmoid-shaped distortions (the typical SVM or boosting pattern). With a = 2.0 and b = −1.0, a score of 0.8 maps to σ(0.6) = 0.646. Isotonic regression fits a non-decreasing step function with the pool-adjacent-violators algorithm: for sorted scores 0.1, 0.3, 0.4, 0.6, 0.8 with outcomes 0, 1, 0, 1, 1 it merges the violating pair (1, 0) into 0.5, giving [0, 0.5, 0.5, 1, 1]. It's more flexible (any monotone shape) but needs more data (about 1,000 or more) to avoid overfitting and produces ties. Temperature scaling divides logits by a single scalar T fitted on validation data (T > 1 softens over-confidence: a logit of 3.0 with T = 1.5 becomes 2.0, p from 0.953 to 0.881) and preserves the ranking and accuracy; multiclass versions (vector or matrix scaling) exist.

Procedure: split off a calibration set (or use cross-validation with CalibratedClassifierCV(base, method="sigmoid" or "isotonic", cv=5), which fits the calibrator on out-of-fold predictions); never calibrate on the data the model trained on, which is over-confident by construction, and evaluate on a further untouched set. Compare methods by Brier score, log-loss and ECE: in the example, isotonic calibration reduced ECE from 0.072 to 0.015 and Brier from 0.141 to 0.126 with AUC unchanged at 0.84. Re-calibrate when the base rate shifts, after class weighting or resampling, and periodically in production (monitor reliability curves by segment, since calibration can fail in subgroups). Calibrated probabilities enable expected-cost thresholds, risk-based pricing, combining models, and clear communication ("about 3 in 10").

Using CalibratedClassifierCV

calibrated = CalibratedClassifierCV(GradientBoostingClassifier(), method="isotonic", cv=5).fit(X_train, y_train) then evaluate brier_score_loss(y_test, calibrated.predict_proba(X_test)[:, 1]) and plot calibration_curve on the untouched test data.

Key takeaway: Calibrate on held-out data with Platt for small samples, isotonic for large ones and temperature scaling for networks, then re-check and monitor.

How it works, step by step

Step 1 of 4
Analyst

1. Diagnose on held-out data

Plot reliability diagrams and compute ECE, Brier score and log-loss by segment.

ECE 0.072 · over-confident
Key numbers at this step
Bins
quantile for sparse tails

When to use it & trade-offs

What it is

A classifier is calibrated when its predicted probabilities match observed frequencies: among cases scored 0.7, about 70% are positive. Calibration is separate from discrimination (ranking): a model can have a perfect AUC and terrible probabilities. It is assessed with reliability diagrams (observed frequency versus predicted probability by bin), Brier score (mean squared error of probabilities), log-loss and expected calibration error (ECE). Miscalibrated models are repaired with post-hoc calibration on held-out data: Platt scaling (fit a logistic function p = σ(a·s + b) to the scores), isotonic regression (fit a monotone, piecewise-constant mapping) and, for neural networks, temperature scaling (divide logits by a learned T).

Why it matters: Any decision that uses the probability itself (expected cost, risk scores, pricing, triage, combining models) needs calibrated values. A fraud model that says 0.9 for cases that are fraudulent 55% of the time will over-trigger costly actions; a threshold derived from costs assumes true probabilities. Many popular models (SVMs, naive Bayes, random forests, boosted trees with some losses, modern neural networks) output scores that rank well but aren't calibrated, and class weighting or resampling distorts them further.

When to reach for it

  • Risk scores and expected-cost decisions
  • Combining or comparing model probabilities
  • Models trained with class weights or resampling

When to avoid or be careful

  • Calibrating on the training data
  • Judging calibration by AUC
  • Using isotonic regression on tiny calibration sets

Calibration methods

Platt scaling (sigmoid)
Best for: Small calibration sets and sigmoid-shaped distortion

Strengths: Two parameters; stable.

Watch out: Can't fit non-sigmoid shapes.

Isotonic regression
Best for: Large calibration sets and arbitrary monotone distortions

Strengths: Flexible; no shape assumption.

Watch out: Overfits small data; creates ties.

Temperature scaling
Best for: Neural network classifiers

Strengths: One parameter; preserves accuracy and ranking.

Watch out: Global correction only.

Interview signals & numbers to know

When this topic comes up

  • “When it says 90%, it's right 60% of the time”
  • Reliability diagram
  • Brier score
  • Platt scaling
  • Temperature scaling

Benchmarks & rules of thumb

0.072 → 0.015 -
ECE before and after isotonic calibration
σ(2.0×0.8 − 1.0) = 0.646 -
Platt-scaled probability for a score of 0.8
3.0 / 1.5 = 2.0 -
logit after temperature scaling with T = 1.5

Common mistakes & follow-ups

Common interview mistakes

  • Assuming good AUC implies good probabilities.
  • Using the same data for training and calibration.
  • Forgetting to recalibrate after changing the base rate.

Likely interviewer follow-ups

  • How do you check whether a model is calibrated?
  • When would you choose Platt over isotonic?
  • Why do models trained with class weights need recalibration?
Interview tip: Show that AUC doesn't change after calibration while ECE falls. It proves you understand calibration versus discrimination.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Model Calibration (Platt Scaling & Isotonic) with data science interview Q&A and hands-on practice.

Open Data Science Q&A →Machine Learning Q&A
Previous topic
Bayesian Optimization with Optuna
Next topic
Model Explainability with SHAP & LIME