Skip to content

FEA Make LedoitWolf estimator array API compatible#33573

Merged
ogrisel merged 26 commits into
scikit-learn:mainfrom
betatim:array-api-ledoitwolf
Jun 1, 2026
Merged

FEA Make LedoitWolf estimator array API compatible#33573
ogrisel merged 26 commits into
scikit-learn:mainfrom
betatim:array-api-ledoitwolf

Conversation

@betatim
Copy link
Copy Markdown
Member

@betatim betatim commented Mar 18, 2026

Reference Issues/PRs

Came up as part of pondering #33564

Towards #33584

What does this implement/fix? Explain your changes.

This makes LedoitWolf array API compatible. After this it might be easier to convert other covariance estimators as it also updates some of the basic infra shared by them

Todo

  • it might be worth doing ledoit_wolf (the function) in this PR as well

AI usage disclosure

I used AI assistance for:

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding

Any other comments?

betatim added 3 commits March 17, 2026 18:08
The tests were written as part of a TDD approach during development,
they cover things that the common tests also cover. So removing them to
reduce the amount of duplication.
betatim added 2 commits March 18, 2026 15:55
In some cases it returns flaot64 for float32 input. There are users who
rely on this behaviour.
@bruAristimunha
Copy link
Copy Markdown
Contributor

Hey @betatim,

Sorry for dropping into the PR, but what you are working on is something super nice! At Pyriemann, with @agramfort and @qbarthelemy, we are using many of the covariance matrix implementations from scikit-learn and are also moving towards array API compatibility.

I just would like to offer some assistance with the covariance part api compatibility, if necessary.

@bruAristimunha
Copy link
Copy Markdown
Contributor

I tried to find a macro issue to offer help, but I couldn't find a place that seemed appropriate.

Please feel free to delete my comment, given that it's off-topic for your PR.

@betatim
Copy link
Copy Markdown
Member Author

betatim commented Mar 19, 2026

I tried to find a macro issue to offer help, but I couldn't find a place that seemed appropriate.

Please feel free to delete my comment, given that it's off-topic for your PR.

No worries and welcome! It is fantastic to hear from someone using scikit-learn that says "this looks useful". I don't think there is a macro/mega issue related to this area of scikit-learn + array API support. I picked it more or less at random/as an exercise to see how much infrastructure would be missing.

I think if you are interested in contributing to converting estimators/tools from https://scikit-learn.org/stable/api/sklearn.covariance.html go for it! It could be that the first step towards that is helping review this PR or picking another estimator from the list and converting it. I'd have to think about it, it probably depends on how much interdependency there is.

Another thing that could be worth doing is creating an example for the gallery that shows off the array API support in LedoitWolf based on a real-world use-case.

I will create a mega-issue for sklearn.covariance so we can keep track of what is done, what needs doing, etc.

Copy link
Copy Markdown
Contributor

@bruAristimunha bruAristimunha left a comment

Choose a reason for hiding this comment

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

if you do this, should unlock too the empiral covariance

Comment thread sklearn/covariance/_empirical_covariance.py
Comment thread sklearn/covariance/_empirical_covariance.py
@github-actions github-actions Bot added the CI:Linter failure The linter CI is failing on this PR label Mar 20, 2026
@github-actions github-actions Bot removed the CI:Linter failure The linter CI is failing on this PR label Mar 20, 2026
bruAristimunha added a commit to bruAristimunha/scikit-learn that referenced this pull request Mar 22, 2026
Convert _oas() and OAS.fit() to use array API compatible operations,
following the same pattern as LedoitWolf from PR scikit-learn#33573.
bruAristimunha added a commit to bruAristimunha/scikit-learn that referenced this pull request Mar 22, 2026
Convert _oas() and OAS.fit() to use array API compatible operations,
following the same pattern as LedoitWolf from PR scikit-learn#33573.
bruAristimunha added a commit to bruAristimunha/scikit-learn that referenced this pull request Mar 22, 2026
Convert _oas() and OAS.fit() to use array API compatible operations,
following the same pattern as LedoitWolf from PR scikit-learn#33573.
If we flag it as supporting array API all the classes that inherit from
it also get marked as supporting array API. This doesn't work, so we
need to do this at the end when all estimators support array API.
@betatim
Copy link
Copy Markdown
Member Author

betatim commented Mar 23, 2026

I undid the changes to EmpiricalCovariance again. By marking this class as supporting array API classes that inherit from it also get marked with support. So we need to tackle this at the end when everyone else has array API support.

@bruAristimunha
Copy link
Copy Markdown
Contributor

I can create a separate PR for this, if you want, @betatim. I fixed in my draft PR, small tricks are necessary for empirical covariance.

@betatim betatim marked this pull request as ready for review March 23, 2026 12:14
@betatim
Copy link
Copy Markdown
Member Author

betatim commented May 18, 2026

I've addressed all the comments, let me know what y'all think. Maybe we could get this wrapped up in time for the release?

Copy link
Copy Markdown
Member

@ogrisel ogrisel left a comment

Choose a reason for hiding this comment

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

LGTM besides the following:

Comment thread sklearn/covariance/tests/test_covariance.py Outdated
@ogrisel
Copy link
Copy Markdown
Member

ogrisel commented May 19, 2026

I ran the tests of this PR with DPNP on an Intel GPU with float32-only support and there are two failures barely above the rtol threshold for float64 inputs:

https://github.com/probabl-ai/scikit-learn-intel-workflow/actions/runs/26148891699/job/76911036653

I think we should bump it up a bit (e.g. 1e-6 instead of 1e-7).

@ogrisel
Copy link
Copy Markdown
Member

ogrisel commented May 20, 2026

@cakedev0 Actually there is something I don't understand in the above. Does your float32-only GPU:

How is the case the following does not fail if the GPU is float32 only xp.asarray(np.ones(3, dtype=np.float64, device="gpu").

Also: I would have expected _array_api_for_tests to have skipped that particular test case (dpnp-gpu-float64 on a float32-only GPU host) entirely.

@cakedev0
Copy link
Copy Markdown
Contributor

Not sure to understand exactly your message but, on my laptop (float32-only GPU):

>>> dpnp.asarray(np.ones(3, dtype=np.float64), device="gpu")
array([1., 1., 1.], dtype=float32)

And this test should definitely have been skipped, let's not bump the rtol but make sure it's skipped instead (I'm looking into that)

Copy link
Copy Markdown
Contributor

@cakedev0 cakedev0 left a comment

Choose a reason for hiding this comment

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

Let's update the usage of _array_api_for_tests to skip tests for which the device doesn't support the dtype.

Comment thread sklearn/covariance/tests/test_covariance.py Outdated
Comment thread sklearn/covariance/tests/test_covariance.py Outdated
Comment thread sklearn/covariance/tests/test_covariance.py Outdated
Comment thread sklearn/covariance/tests/test_covariance.py Outdated
Comment thread sklearn/covariance/tests/test_covariance.py Outdated
Comment thread sklearn/covariance/tests/test_covariance.py Outdated
Co-authored-by: Arthur Lacote <arthur.lcte@gmail.com>
@github-actions github-actions Bot added the CI:Linter failure The linter CI is failing on this PR label May 28, 2026
@ogrisel
Copy link
Copy Markdown
Member

ogrisel commented May 28, 2026

New Intel GPU workflow: https://github.com/probabl-ai/scikit-learn-intel-workflow/actions/runs/26577559267

EDIT: It's green!

Let me fix the linter.

@ogrisel
Copy link
Copy Markdown
Member

ogrisel commented May 28, 2026

@betatim this PR should be ready to merge once the following is addressed: #33573 (review)

@github-actions github-actions Bot removed the CI:Linter failure The linter CI is failing on this PR label May 28, 2026
@betatim
Copy link
Copy Markdown
Member Author

betatim commented May 29, 2026

I fixed the use of _convert_to_numpy. GitHub says that @cakedev0 requested changes, but I can't find what/where? Has that been resolved already or is there a change you'd like to see?

Copy link
Copy Markdown
Contributor

@cakedev0 cakedev0 left a comment

Choose a reason for hiding this comment

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

This should discard the "request changes" review. (I'll avoid that next time)

@betatim
Copy link
Copy Markdown
Member Author

betatim commented Jun 1, 2026

Yeah, my impression is that the "request changes" feature isn't super useful for a project like scikit-learn. It ends up making it more complicated to merge things and I don't think I've ever experienced it where someone's feedback was ignored/not worked on because they "only" left it as a comment/normal review feedback. The social fabric is strong here :D

@betatim
Copy link
Copy Markdown
Member Author

betatim commented Jun 1, 2026

@OmarManzoor and @ogrisel you both left 👍 , do you want to take a last look and then press merge? Or should we attract some more reviews?

@ogrisel ogrisel merged commit 3b37225 into scikit-learn:main Jun 1, 2026
43 checks passed
@github-project-automation github-project-automation Bot moved this from In Progress to Done in Array API Jun 1, 2026
@ogrisel
Copy link
Copy Markdown
Member

ogrisel commented Jun 1, 2026

Merged! Thanks all!

@betatim betatim deleted the array-api-ledoitwolf branch June 1, 2026 08:30
@jeremiedbb jeremiedbb mentioned this pull request Jun 1, 2026
16 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

6 participants