Skip to content

ENH Add Array API compatibility to KernelCenterer - #27556

Merged
betatim merged 6 commits into
scikit-learn:mainfrom
EdAbati:KernelCenterer-array-api
Oct 23, 2023
Merged

betatim merged 6 commits into
scikit-learn:mainfrom
EdAbati:KernelCenterer-array-api

Conversation

@EdAbati

@EdAbati EdAbati commented Oct 9, 2023

Copy link
Copy Markdown
Contributor

Reference Issues/PRs

Towards #26024

What does this implement/fix? Explain your changes.

It makes the KernelCenterer implementation compatible and tested with the Array API.

@github-actions

github-actions Bot commented Oct 9, 2023 •

Copy link
Copy Markdown

✔️ Linting Passed

All linting checks passed. Your pull request is in excellent shape! ☀️

Generated for commit: 2d03e43. Link to the linter CI: here

)

K_pred_cols = (np.sum(K, axis=1) / self.K_fit_rows_.shape[0])[:, np.newaxis]
K_pred_cols = (xp.sum(K, axis=1) / self.K_fit_rows_.shape[0])[:, None]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I actually tried with xp.newaxis initially, but I got an AttributeError both with torch and numpy

AttributeError: module 'array_api_compat.torch' has no attribute 'newaxis'
AttributeError: module 'numpy.array_api' has no attribute 'newaxis'

@betatim do you know why newaxis may not be included?

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.

Unfortunately I don't know why newaxis doesn't exist for them. I would have guessed "bug in array_api_compat" if that was the only place where its missing, but I think ofnumpy.array_api as the "very much exactly what the standard says" implementation. One option is that it is something that only got added in a newer version of the standard

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 opened data-apis/array-api-compat#64 upstream.

Ok for using None in scikit-learn in the mean time.

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

Along with #27642 I get the MPS tests to pass for this PR on my local macOS laptop.

I also ran the tests with pytorch and cupy on a CUDA host and all array_api tests pass for the KernelCenterer class.

@betatim
betatim merged commit 1dd395c into scikit-learn:main Oct 23, 2023
@EdAbati
EdAbati deleted the KernelCenterer-array-api branch October 23, 2023 16:14
glemaitre pushed a commit to glemaitre/scikit-learn that referenced this pull request Oct 31, 2023
REDVM pushed a commit to REDVM/scikit-learn that referenced this pull request Nov 16, 2023
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.

4 participants