Skip to content

FIX always use logits in CalibratedClassifierCV - #34313

Merged
lorentzenchr merged 73 commits into
scikit-learn:mainfrom
ogrisel:fix-always-use-logits-in-CalibratedClassifierCV
Sep 24, 2026
Merged

lorentzenchr merged 73 commits into
scikit-learn:mainfrom
ogrisel:fix-always-use-logits-in-CalibratedClassifierCV

Conversation

@ogrisel

@ogrisel ogrisel commented Jun 16, 2026 •

Copy link
Copy Markdown
Member

Fixes: #34312

Note: this draft PR was started a long time ago. Work is needed to realign it with the current main branch.

@antoinebaker

Copy link
Copy Markdown
Member

I agree, we should definitively use the logits for sigmoid recalibration / Platt scaling.

@ogrisel Could you grant me rights to push to your PR ? I'll try to merge main and update this draft PR.

@ogrisel

ogrisel commented Jun 19, 2026 •

Copy link
Copy Markdown
Member Author

@antoinebaker you were already registered as a collaborator on my scikit-learn fork :)

Please go ahead with the merge and update of this PR.

@antoinebaker antoinebaker moved this to In progress in Labs Jun 19, 2026
Comment thread sklearn/calibration.py Resolved
@antoinebaker

Copy link
Copy Markdown
Member

Hi @ogrisel and @virchan, the PR is currently passing the CI.

However I would like to refactor the code, in line with your code comments @ogrisel and your comment @virchan. I would greatly appreciate your feedback (see Comments section) to discuss future implementations !

With the help of cursor, here a summary of the current logic of _enforce_logits (which role is to preprocess the predictions as logits before feeding them to the calibrators).

predict_proba

Inputs are always probabilities p_k = P(y=k).

Calibration method Target logit space Transformation
sigmoid / isotonic OvR (“Bernoulli”) logits z_k = logit(p_k)
temperature Multinomial logits z_k = log(p_k) - log(p_mean)

decision_function

Inputs are scores s whose meaning varies by estimator.

Calibration method Target logit space Current transformation
sigmoid / isotonic OvR (“Bernoulli”) logits Binary (1D): pass-through s (assumed positive-class log-odds). Multiclass (K>2): softmax(s) → p_k, then z_k = logit(p_k).
temperature Multinomial logits Pass-through s (assumed joint logits with p = softmax(s)).

Comments

For me, the handling of predict_proba is correct and solves #34312. The handling of decision_function is more debatable. It seems to always assume "Multinomial logits"-like scores, and in particular converts them to Bernoulli logits for the sigmoid/isotonic calibration method.

Here are some implementation options I'd like to discuss:

  1. get rid of the logit_preprocessing parameter. Instead we always decide based on the calibration method (sigmoid vs softmax) and response_method_name (predict_proba vs decision_function)
  2. only convert predict_proba for now, let decision_function passthrough. This would make the code much easier to understand. This would already fixes # 34312.
  3. try to determine (for an estimator) if the decision_function scores are "morally" more Bernoulli vs Multinomial logits and convert based on that. But this seems difficult to do in general.
  4. always convert the decision_function scores to the target logit space, even if they are in the correct logit space to start with. Currently done for the sigmoid method, but could be done for the temperature method as well.

@ogrisel

ogrisel commented Jun 25, 2026 •

Copy link
Copy Markdown
Member Author
  1. get rid of the logit_preprocessing parameter. Instead we always decide based on the calibration method (sigmoid vs softmax) and response_method_name (predict_proba vs decision_function)

I think I originally introduced the logit_preprocessing parameter in order to be able to conduct an empirical evaluation similarly to what was done in the CalArena paper. This evaluation was added at the end of examples/calibration/plot_calibration_multiclass.py but it wasn't meant to stay after the review of the PR. Since the results of the CalArena paper are clear, I think we can indeed drop this parameter.

  1. only convert predict_proba for now, let decision_function passthrough. This would make the code much easier to understand. This would already fixes # 34312.

I agree.

@ogrisel

ogrisel commented Jun 25, 2026

Copy link
Copy Markdown
Member Author

@antoinebaker Let's first add temperature scaling to examples/calibration/plot_calibration_multiclass.py and then check then results collected at the end of this notebook that "softmax" logits preprocessing is always better than (or very similar to) sigmoid logits preprocessing for temperature scaling.

Let's also check that "sigmoid" logits preprocessing is always better or similar to "softmax" logits preprocessing for the two OvR methods.

If all goes according to our plan, we can hard code the logits preprocessing logic and remove the empirical evaluation of the logits_processing parameter from that example.

@antoinebaker antoinebaker 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.

I did a last quick review, LGTM !

I'd like to remove test_asymptotic_multiclass_calibration_improvement because I find this test hard to understand, but I'm fine if others want to keep it.

Comment thread sklearn/tests/test_calibration.py Resolved
Comment thread doc/modules/calibration.rst Resolved
Comment thread doc/modules/calibration.rst Resolved
@lorentzenchr

Copy link
Copy Markdown
Member

Once the review comments are addressed (mostly wording), it is good to go.

Co-authored-by: Christian Lorentzen <[email protected]>
Co-authored-by: Olivier Grisel <[email protected]>
@ogrisel

ogrisel commented Sep 24, 2026

Copy link
Copy Markdown
Member Author

I applied most of the suggestions from the last pass via github and will address the remaining feedback manually in new commits.

Comment thread sklearn/tests/test_calibration.py Resolved
@github-actions github-actions Bot added the CI:Linter failure The linter CI is failing on this PR label Sep 24, 2026
@lorentzenchr

Copy link
Copy Markdown
Member

@ogrisel 7e37f65 also remove test_calibration_zero_probability. I think that was a valuable test.

@ogrisel

ogrisel commented Sep 24, 2026

Copy link
Copy Markdown
Member Author

Circle CI fails with:

[email protected]: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
failed to fetch repo: exit status 128

which is unrelated to this PR and observed on other PRs. Hopefully it's a transient problem on the circle ci infra rather than a config problem on our side.

@github-actions github-actions Bot removed the CI:Linter failure The linter CI is failing on this PR label Sep 24, 2026
@ogrisel

ogrisel commented Sep 24, 2026

Copy link
Copy Markdown
Member Author

@ogrisel 7e37f65 also remove test_calibration_zero_probability. I think that was a valuable test.

Thanks for catching this... my bad.

@ogrisel

ogrisel commented Sep 24, 2026

Copy link
Copy Markdown
Member Author

CI is green again \o/ and the plots still look good.

I think I addressed all @lorentzenchr's feedback but I cannot approve my own PR :)

@lorentzenchr
lorentzenchr merged commit 8578499 into scikit-learn:main Sep 24, 2026
37 checks passed
@ogrisel
ogrisel deleted the fix-always-use-logits-in-CalibratedClassifierCV branch September 24, 2026 21:14
@ogrisel

ogrisel commented Sep 24, 2026 •

Copy link
Copy Markdown
Member Author

Thanks for the reviews and the code/doc contributions to this PR @virchan @lorentzenchr and @antoinebaker!

@lorentzenchr

Copy link
Copy Markdown
Member

For me, the need of this fix was the most surprising. I just never looked up our exact code.

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.

CalibratedClassifierCV(method="sigmoid") should always take the raw logits as inputs instead of the predicted probabilities

5 participants