Repository navigation
Description
Describe the workflow you want to enable
I would like to add the ability to calculate Expected Calibration Error (ECE) within scikit-learn. ECE is defined in equation (3) from Guo et al. On Calibration of Modern Neural Networks. (2017). This is a well-cited paper (over 700 citations as of Aug. 26, 2020), and the ECE metric is now widely used in academic papers on model calibration. There is even a method now in TensorFlow Probability for computing ECE: tfp.stats.expected_calibration_error().
Describe your proposed solution
I can see several possibilities for how the ECE calculation could be added to scikit-learn, with pros and cons for each:
-
make ECE an extra return value of
sklearn.calibration.calibration_curve(). This method currently already does 99% of the work towards calculating ECE. Computing ECE is simply a one-liner added to the end of that method:def calibration_curve(...): ... ece = np.sum(np.abs(prob_true - prob_pred) * (bin_total[nonzero] / len(y_true))) return prob_true, prob_pred, ece
The downside is that introducing a 3rd return value is a breaking change. One possible mitigation is to add a boolean
return_eceparameter to the function definition with a default value ofFalse, and only returneceifreturn_ece=True. -
add a dedicated function to
sklearn.metrics, i.e.,sklearn.metrics.expected_calibration_error(y_true, y_pred). Downside here is that it would need to re-compute everything that calibration_curve() already computes, so there is a performance penalty for a user who wants to both calculate ECE and plot a calibration curve. -
add a dedicated function to
sklearn.calibration, i.e.,sklearn.calibration.expected_calibration_error(y_true, y_pred). This would keep the ECE calibration within the calibration subpackage. Same downside as in option 2.
Additional context
I am happy to write the code and tests to add ECE calculation to scikit-learn. After all, the code to calculate ECE is just a one line addition to calibration_curve(). However, as I am not a regular contributor to scikit-learn, I am unfamiliar with the best place within the scikit-learn library to add such a feature. Please advise!
Activity
lucyleeow commentedon Aug 27, 2020
Thanks @chrisyeh96. I think this was discussed in #10883 and implemented here #11096. It's been a long process (to merge it) but please feel free to review #11096
rth commentedon Sep 11, 2020
Yes, it would be quite useful to add it.
Since it reuses most of the code for calibration_curve, we can put all that code in a private function and use it both in
calibration_curveandexpected_calibration_error. That should avoid the concerns you mentioned.jdey4 commentedon Apr 12, 2022
I think, using adaptive bin size would be a better solution for ECE calculation as mentioned here: https://openaccess.thecvf.com/content_CVPRW_2019/papers/Uncertainty%20and%20Robustness%20in%20Deep%20Visual%20Learning/Nixon_Measuring_Calibration_in_Deep_Learning_CVPRW_2019_paper.pdf. I have a tentative implementation here: https://github.com/neurodata/kdg/blob/cc88886819676020402f19ec8b224ed5aafdc440/kdg/utils.py#L8. I am new to sklearn contribution. I will be happy to add the code if it seems useful.
EvanLavizadeh commentedon Nov 22, 2022
Hi @chrisyeh96,
I don't think your code is a proper implementation of the ECE formula (Formula 3 in Guo et al.).
sklearn.calibration_cruve returns prob_true which is "The proportion of samples whose class is the positive class, in each bin (fraction of positives). Prob_true is independent of the classification result. This is different than accuracy (what the formula references) which is the proportion of correctly classified samples.
lorentzenchr commentedon Feb 8, 2023
Could someone say, what the Bayes rule/classifier of the proposed ECE is?
I think a better option is to have score decompositions like #21774 or more general #23767.
lorentzenchr commentedon Apr 30, 2026
Some more info about estimating calibration error:
References: