Skip to content

FIX: RidgeClassifier.classes_ should follow array API y namespace - #34065

Merged
ogrisel merged 5 commits into
scikit-learn:mainfrom
cakedev0:xp/fix/rigdeclf_params
May 22, 2026
Merged

ogrisel merged 5 commits into
scikit-learn:mainfrom
cakedev0:xp/fix/rigdeclf_params

Conversation

@cakedev0

@cakedev0 cakedev0 commented May 20, 2026 •

Copy link
Copy Markdown
Member

Reference Issues/PRs

Fixes #34047

What does this implement/fix? Explain your changes.

As decided in the issue, RidgeClassifier.classes_ should follow array API y namespace like LogisticRegression does.

AI usage disclosure

I used AI assistance for:

  • Code generation
  • Research and understanding

Any other comments?

Intel array API CI run: https://github.com/probabl-ai/scikit-learn-intel-workflow/actions/runs/26164735798 ✅

@cakedev0
cakedev0 marked this pull request as ready for review May 20, 2026 12:22
@cakedev0
cakedev0 marked this pull request as draft May 20, 2026 12:28
Comment on lines -1351 to -1356
if y_is_array_api and xp_y.isdtype(y.dtype, "numeric"):
self.classes_ = move_to(
self._label_binarizer.classes_, xp=xp, device=device_
)
else:
self.classes_ = self._label_binarizer.classes_

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Context on why we can now always rely on LabelBinarizer.classes_ directly here:

LinearClassifierMixin.predict already handles classes_ in its own namespace and only moves the final predictions back to the namespace/device of X:

xp_classes, _, device_classes = get_namespace_and_device(self.classes_)
indices = move_to(indices, xp=xp_classes, device=device_classes)
y_pred = xp_classes.take(self.classes_, indices, axis=0)
if isinstance(y_pred[0], str):
return y_pred
else:
return move_to(y_pred, xp=xp, device=device_)

So classes_ does not need to be eagerly moved during fit.

@cakedev0
cakedev0 marked this pull request as ready for review May 20, 2026 13:16
@ogrisel

ogrisel commented May 20, 2026

Copy link
Copy Markdown
Member

I think we need a changelog entry to document the fix for this one.

@ogrisel

ogrisel commented May 20, 2026

Copy link
Copy Markdown
Member

cc @lucyleeow @StefanieSenger.

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

LGTM besides the following. I am fine with merging this PR as is and continue the work in follow-up PRs.

pred_xp = ridge_xp.predict(X_xp)
assert pred_xp.shape == pred_np.shape == y.shape
assert_allclose(move_to(pred_xp, xp=np, device="cpu"), pred_np)
assert_array_equal(move_to(ridge_xp.classes_, xp=np, device="cpu"), classes_np)

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 checked _check_array_api_core and the classes_ attribute is skipped from the check, so at the moment the common tests do not enforce any consistency with respect to the namespace, device and value of this attribute.

I think we should explicitly set expectations in the array API doc and then update the common test. We can do that either in this PR or in a follow-up issue+PR.

This is particularly the case for string class labels, but the particular case of string class labels can be handled in a dedicated follow-up PR.

Testing classifiers with string labels was originally explored in #33525 but wasn't included in the end so we still need to expand the testing harness for this, possibly with a dedicated common test.

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.

Testing classifiers with string labels was originally explored in #33525 but wasn't included in the end

Ah yes, I have it as a todo, but did not open an issue.

ref: #33525 (comment)

Happy to open an issue unless someone wants to work on it now/soon?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think I prefer to merge this PR as it is, so you can open the issue, thanks!

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 I did not mean in this PR, just soon in a separate PR.
I'll open an issue.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Sorry I did not mean in this PR, just soon in a separate PR.

Yes that's what I understood :) I was answering this part of @ogrisel's comment:

We can do that either in this PR or in a follow-up issue+PR.

Let's go for the follow-up issue+PR solution where you open the issue ;) (or maybe I'm mixing different topics? 🤔 )

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.

@lucyleeow: if you open the issue for common testing classifier with array API X and string labels in y, then we should make sure to test both .fit and .score: #33822 and as a result we will indirectly test integration for mixed namespace with string label support for some common classification metrics.

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.

as a result we will indirectly test integration for mixed namespace with string label support for some common classification metrics.

Good point, though we do already do this in test_array_api_classification_mixed_string_numeric_input in metrics test_common.py ?

@ogrisel

ogrisel commented May 22, 2026

Copy link
Copy Markdown
Member

@lucyleeow shall we merge this, then?

@lucyleeow

Copy link
Copy Markdown
Member

Yes go ahead!

@ogrisel
ogrisel merged commit 07f42cc into scikit-learn:main May 22, 2026
33 checks passed
@github-project-automation github-project-automation Bot moved this from In Progress to Done in Array API May 22, 2026
@jeremiedbb jeremiedbb added this to the 1.9 milestone May 22, 2026
@jeremiedbb jeremiedbb added the To backport PR merged in master that need a backport to a release branch defined based on the milestone. label May 22, 2026
@ogrisel ogrisel added this to Labs May 25, 2026
@ogrisel ogrisel moved this to Done in Labs May 25, 2026
@jeremiedbb jeremiedbb mentioned this pull request Jun 1, 2026
16 tasks done
jeremiedbb pushed a commit that referenced this pull request Jun 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Array API module:linear_model module:utils To backport PR merged in master that need a backport to a release branch defined based on the milestone.

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Inconsistent type of predictions and classes_ for array API classifiers

4 participants