Skip to content

FEA: Calibration Error - #11096

Closed
samronsin wants to merge 115 commits into
scikit-learn:mainfrom
samronsin:calibration-loss
Closed

samronsin wants to merge 115 commits into
scikit-learn:mainfrom
samronsin:calibration-loss

Conversation

@samronsin

@samronsin samronsin commented May 15, 2018 •

Copy link
Copy Markdown
Contributor

This PR implements calibration errors for binary classifiers, namely:

See initial discussion on issue #10883
Closes #18268

@jnothman jnothman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You will also need to update:

  • doc/modules/classes.rst
  • doc/modules/model_evaluation.rst
  • sklearn/metrics/tests/test_common.py

@jnothman jnothman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be available as a scorer for cross validation? Should it be available for CalibratedClassifierCV?

Comment thread sklearn/metrics/classification.py Resolved
Comment thread sklearn/metrics/classification.py Resolved
Comment thread sklearn/metrics/classification.py Resolved
Comment thread sklearn/metrics/classification.py Resolved
Comment thread sklearn/metrics/classification.py Resolved
Comment thread sklearn/metrics/classification.py Resolved
@jnothman

Copy link
Copy Markdown
Member

You don't need to test sample weights as the common tests will

@amueller

Copy link
Copy Markdown
Member

I'm not sure if it makes sense to have this as a ready-made scorer, and so I might want to hold out on that? It should probably be mentioned in the calibration docs and examples.

@amueller

amueller commented May 19, 2018 •

Copy link
Copy Markdown
Member

Can you maybe also add https://www.math.ucdavis.edu/~saito/data/roc/ferri-class-perf-metrics.pdf to the references and maybe include some of the discussion there about this loss? In particular, this is one of two calibration losses they discuss, so maybe we should be more specific with the name?

@samronsin

Copy link
Copy Markdown
Contributor Author

Actually I did not implement any of the losses from the paper you mention @amueller, as I take non-overlapping bins instead of sliding window in CalB.
CalB would totally make sense in this PR, although:

  • n_bins would have a very different meaning: e.g. the number of non-overlapping bins
  • taking sample_weight into account seems non-trivial because of the sliding window
  • implementing CalB efficiently would actually be non-trivial in itself since it is based on a sliding window
  • greedy implementation would be quite drag on performance, compared to non-overlapping binning

@samronsin

Copy link
Copy Markdown
Contributor Author

I ended up implementing the calB loss suggested by @amueller, but did not provide support for sample weights.
Also, I'll be happy to take suggestions regarding its implementation.

@jnothman jnothman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for fashion again not being in a situation to review the main content...

Comment thread doc/modules/model_evaluation.rst Resolved
Comment thread sklearn/metrics/classification.py Resolved
Comment thread sklearn/metrics/classification.py Resolved
Comment thread doc/modules/model_evaluation.rst Resolved
Comment thread doc/modules/model_evaluation.rst Outdated
'balanced_accuracy' :func:`metrics.balanced_accuracy_score` for binary targets
'average_precision' :func:`metrics.average_precision_score`
'brier_score_loss' :func:`metrics.brier_score_loss`
'calibration_loss' :func:`metrics.calibration_loss`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to respect the convention higher is better we should maybe call it

neg_calibration_error

thoughts anyone?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes for the grid search tools in scikit-learn will always to maximize the scoring criterion. If the criterion is derived from a loss to minimize we instead maximize the the negative criterion. It's important to name it accordingly to make the column names and column values of pd.DataFrame(grid_search.cv_results_) consistent.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might also want to provide standard aliases neg_max_calibration_error and neg_expected_calibration_error to set the norm parameter to make it possible to grid search for the ECE or MCE metrics traditionally used in the literature.

Comment thread sklearn/metrics/classification.py Resolved
Comment thread sklearn/metrics/classification.py Resolved
Comment thread sklearn/metrics/classification.py Resolved
Comment thread sklearn/metrics/classification.py Resolved
Comment thread sklearn/metrics/classification.py Resolved
Comment thread sklearn/metrics/classification.py Resolved
@github-actions github-actions Bot added the CI:Linter failure The linter CI is failing on this PR label May 10, 2026
@github-actions github-actions Bot removed the CI:Linter failure The linter CI is failing on this PR label May 11, 2026
@github-actions github-actions Bot added the CI:Linter failure The linter CI is failing on this PR label May 12, 2026
@github-actions github-actions Bot removed the CI:Linter failure The linter CI is failing on this PR label May 12, 2026
@samronsin
samronsin requested review from glemaitre and ogrisel May 18, 2026 15:16
@samronsin samronsin changed the title [MRG] Implement calibration loss metrics FEA: Calibration Error May 28, 2026
@antoinebaker

Copy link
Copy Markdown
Member

Salut @samronsin ;)

Thanks a lot for the PR !

We had a lot of discussions recently, before and during the scikit-learn sprint, on the "best" estimate for the calibration error. Based on the analyses reported here and IRL discussions with @ogrisel @lorentzenchr we currently think that:

  • we should have a brier_calibration_error score
  • the best estimate seems to be binning with n_bins=n**(1/3)
  • we postpone for now the debiasing, could be done in a follow up PR
  • we also postpone the L1 version and the multiclass version in follow up PRs

I would say your PR is quite close with the following modifications:

  • rename to brier_calibration_error
  • drop the reduce_bias, strategy, n_bins, norm parameters

Please ping me if you're willing to work on this or if you need a review.

@samronsin

Copy link
Copy Markdown
Contributor Author

Salut @antoinebaker, good to read you 🙂 ! Thank you for your message and suggestions.

This makes a lot of sense, let's break this PR into smaller scoped PRs !

I'll start by extracting the brier_calibration_error initial bit as you suggested, then the follow-ups.

@samronsin

Copy link
Copy Markdown
Contributor Author

Closing as superseded by #34261 (and follow-ups).

@samronsin samronsin closed this Jun 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request: function to calculate Expected Calibration Error (ECE)