Skip to content

Fix .score() with array API and pandas y - #34711

Closed
godofecht wants to merge 1 commit into
scikit-learn:mainfrom
godofecht:fix-33822-score-array-api-pandas
Closed

godofecht wants to merge 1 commit into
scikit-learn:mainfrom
godofecht:fix-33822-score-array-api-pandas

Conversation

@godofecht

Copy link
Copy Markdown

Reference Issue

Fixes #33822

What does this implement/fix?

move_to in sklearn/utils/_array_api.py failed when passed a pandas Series or DataFrame because those objects do not support the DLPack protocol. The fallback path also failed because get_namespace_and_device does not recognize pandas objects as array API arrays.

This caused ClassifierMixin.score (and other methods that call accuracy_score or similar metrics) to fail when y was a pandas object and array API dispatch was enabled.

Fix: convert pandas Series/DataFrame to NumPy arrays at the start of move_to, before the namespace detection and DLPack transfer logic.

Reproducer

import numpy as np
import pandas as pd
import torch
from sklearn.linear_model import RidgeClassifier
from sklearn import config_context

rng = np.random.default_rng(seed=123)
X = rng.standard_normal(size=(100, 10))
y = rng.integers(2, size=X.shape[0])
Xt = torch.tensor(X)
ys = pd.Series(y)

with config_context(array_api_dispatch=True):
    model = RidgeClassifier(solver="svd").fit(Xt, ys)
    score = model.score(Xt, ys)  # failed before, now works

This pull request includes code written with the assistance of AI.
The code has not yet been reviewed by a human.

move_to failed when passed a pandas Series or DataFrame because those
objects do not support the DLPack protocol. The fallback path also
failed because get_namespace_and_device does not recognize pandas
objects.

Fix: convert pandas Series/DataFrame to numpy arrays at the start of
move_to, before the namespace detection and DLPack transfer logic.

This pull request includes code written with the assistance of AI.
The code has not yet been reviewed by a human.
@github-actions

Copy link
Copy Markdown

Thank you for opening your first pull request to scikit-learn! 🎉

To help get your contribution reviewed, please make sure that:

  • You have filled out the pull request template.
  • The pull request addresses an existing issue that is ready for
    contribution (e.g. not tagged as "Needs Triage", "Needs Decision", ...).
    If you are proposing a new feature, please open an issue to discuss it first.
  • There are no other open pull requests already targeting the same issue.
  • You have followed the pull request checklist.
    In particular, linting and tests should pass.

@AnneBeyer

Copy link
Copy Markdown
Member

See my comment in #34694.

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

Labels

module:utils spam spam PR and issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.score() with array API does not follow same 'everything follows X' logic as .fit()

3 participants