Skip to content

Array API support for CalibratedClassifierCV #31869

Open

Description

@lorentzenchr

Describe the workflow you want to enable

Towards #26024.
Use CalibratedClassifierCV with pytorch or tensorflow models.
This has become even more interesting use case with #31068.

Describe your proposed solution

In line with out Array API adoption path.

  • Temperature scaling
  • Platt scaling
  • Isotonic regression

Describe alternatives you've considered, if relevant

No response

Additional context

No response

Activity

  1. added
    Hard
    and removed
    Needs Triage
    on Aug 4, 2025
  2. OmarManzoor commented on Sep 4, 2025

    @OmarManzoor
    Contributor

    @lucascolley I was trying this out for TemperatureScaling and within that we make use of scipy.optimize.minimize_scalar which currently is not compatible with the Array API.

    log_beta_minimizer = minimize_scalar(
    log_loss,
    bounds=(-10.0, 10.0),
    options={
    "xatol": 64 * np.finfo(float).eps,
    },
    )
    if not log_beta_minimizer.success: # pragma: no cover
    raise RuntimeError(
    "Temperature scaling fails to optimize during calibration. "
    "Reason from `scipy.optimize.minimize_scalar`: "
    f"{log_beta_minimizer.message}"
    )
    self.beta_ = np.exp(log_beta_minimizer.x)
    return self

    Do we have any alternative that we can use or should we consider adding an Array API compatible version of minimize_scalar in the utils?

  3. lucascolley commented on Sep 4, 2025

    @lucascolley
    Contributor

    @mdhaber have you thought about array API support for constrained minimisation? Any opinions on where to start?

  4. lucascolley commented on Sep 4, 2025

    @lucascolley
    Contributor

    gh-32107 shows the existing test-suite passing while naïvely replacing minimize_scalar with bracket_minimum and find_minimum (benchmarks may be a different matter).

    As documented at https://scipy.github.io/devdocs/dev/api-dev/array_api_modules_tables/optimize_elementwise.html, these functions have CuPy and PyTorch support, so this may be an interesting starting point for you @OmarManzoor @lucyleeow !

    I haven't investigated performance for NumPy at all, but this may at least get you by as a fallback for non-NumPy arrays, while you wait for some more ticks to materialise in https://scipy.github.io/devdocs/dev/api-dev/array_api_modules_tables/optimize.html.

  5. virchan commented on Sep 4, 2025

    @virchan
    Member

    @OmarManzoor, find_minimum and bracket_minimum support the Array API. I've been thinking about this as well.

  6. lucascolley commented on Sep 4, 2025

    @lucascolley
    Contributor

    @virchan see my recent comment :)

  7. OmarManzoor commented on Sep 5, 2025

    @OmarManzoor
    Contributor

    @lucascolley Thank you for the details on how to use bracket_minimum and find_minimum.

  8. OmarManzoor commented on Sep 5, 2025

    @OmarManzoor
    Contributor

    @OmarManzoor, find_minimum and bracket_minimum support the Array API. I've been thinking about this as well.

    Thank you.

  9. lucyleeow commented on Sep 22, 2025

    @lucyleeow
    Member

    @OmarManzoor are you wanting to work on this?

  10. OmarManzoor commented on Sep 22, 2025

    @OmarManzoor
    Contributor

    @lucyleeow I have done some work and experimentation with regards to TemperatureScaling. I am looking to create a PR to share my findings.

  11. ogrisel commented on Oct 27, 2025

    @ogrisel
    Member

    #32246 has implemented array API support for the temperature scaling method which is arguably the lowest hanging fruit of the three methods.

    Adding support for the other methods might be more challenging. I haven't checked the details but, I think that:

    • Platt scaling uses LBFGS to tune its 2 continuous parameters (per class). I don't know if the scipy optimize API allows for non-NumPy array namespaces even if the gradient computing callable accepts it. Note that since the number of parameters to optimize for is very small, I think that it's ok to convert them back and forth if the expensive parts of the computation can happen on the original namespace, but I am not sure if that the case or not. Or we could just accept the array API namespace data to pass it to the underlying base estimator and convert the estimators predictions back to NumPy to fit for the calibration stage for each cv fold.
    • isotonic regression is implemented in Cython so I don't think it's possible to support array API without conversion. But here again the cost of calibration might be small compared to the cost of fitting the underlying estimators, so maybe it's still worth accepting non-NumPy inputs to feed the fit method of the underlying estimator with and then convert the predictions to NumPy prior to passing them to isotonic regression Cython code.
  12. lucascolley commented on Oct 27, 2025

    @lucascolley
    Contributor

    I don't know if the scipy optimize API allows for non-NumPy array namespaces even if the gradient computing callable accepts it.

    I think the answer is not yet but it should be doable.

  13. moved this to Todo in Array APIon Oct 30, 2025
  14. lucyleeow commented on Nov 1, 2025

    @lucyleeow
    Member

    I'll start working on this!

  15. mdhaber commented on Nov 1, 2025

    @mdhaber
    Contributor

    Sorry I didn't see the ping before. Re: #31869 (comment), do you mean like nonlinear constraints in multiple dimensions? (Because technically find_minimum is constrained minimization because it enforces bounds.)

    benchmarks may be a different matter

    They are not intended to be fast for inexpensive user callables acting on size-1 arrays ; I didn't minimize overhead. But I've seen, say, 300x improvement on large arrays with CuPy on pretty practical examples. And actually, Albert implemented the same algorithm as find_minimum for inverting ufuncs in special, all in compiled code, and it was only twice as fast as using find_root on the ufuncs (with reasonably sized NumPy arrays), so it's not terrible.

  16. lucascolley commented on Nov 1, 2025

    @lucascolley
    Contributor

    (Because technically find_minimum is constrained minimization because it enforces bounds.)

    that's what I was referring too, yeah

  17. mdhaber commented on Nov 1, 2025

    @mdhaber
    Contributor

    Oh, ok, so you had already gotten your answer. Haha well sorry I'm two months late to the party.

  18. added a commit that references this issue on Feb 20, 2026
    71a50d2
  19. tusharjamunkar commented on Aug 27, 2026

    @tusharjamunkar

    Hi @lorentzenchr and the scikit-learn team, I would like to contribute on issue #31869 to implement Array API standard support for CalibratedClassifierCV (covering the Platt scaling and Isotonic regression paths). Could you please assign this issue to me?

  20. LeonxLJX commented on Sep 1, 2026

    @LeonxLJX
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions