Model Calibration: Expected Calibration Error

medium
Unit 11 · Evaluation & Regression0/3 to milestone

A model that says "90% confident" should be right about 90% of the time. Calibration measures whether predicted probabilities match reality. It matters for fraud thresholds, medical risk and LLM confidence scores.

Expected Calibration Error: split predictions into n_bins equal-width confidence bins over [0,1][0, 1]. For each non-empty bin bb, compare the average confidence with the accuracy:

ECE=∑b∣b∣N ∣acc(b)−conf(b)∣\text{ECE} = \sum_b \frac{|b|}{N}\,\big|\text{acc}(b) - \text{conf}(b)\big|

Implement reliability_bins(confidences, correct, n_bins=10), returning a list of dicts {"lo", "hi", "count", "confidence", "accuracy"} for the non-empty bins, in order. Bins are [lo, hi), except the last, which includes 1.0. Then implement ece(confidences, correct, n_bins=10).

#evaluation#calibration#probability2 visible + 3 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Model Calibration: Expected Calibration Error · TechGradify