Thanks to visit codestin.com
Credit goes to github.com

Skip to content

FIX array API classification metrics with pandas labels - #34779

Merged
virchan merged 4 commits into
scikit-learn:mainfrom
ogrisel:tst-array-api-metrics-and-pandas
Aug 26, 2026
Merged

FIX array API classification metrics with pandas labels#34779
virchan merged 4 commits into
scikit-learn:mainfrom
ogrisel:tst-array-api-metrics-and-pandas

Conversation

@ogrisel

@ogrisel ogrisel commented Aug 19, 2026

Copy link
Copy Markdown
Member

Closes #33822.

if (
_is_xp_namespace(xp, "torch")
and _is_numpy_namespace(xp_array)
and any(stride < 0 for stride in array.strides)

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.

Note to reviewers: this is an unrelated stylistic change suggested by my editor: collapse the two nested if with an "and" operator.

# Other array-like containers (e.g. lists,pandas Series)
# are first converted to numpy arrays to ensure dlpack
# compatibility.
array = numpy.asarray(array)

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.

This is the real fix.

@ogrisel

ogrisel commented Aug 19, 2026

Copy link
Copy Markdown
Member Author

I triggered an Intel GPU run at: https://github.com/probabl-ai/scikit-learn-intel-workflow/actions/runs/32241167012 (it's green).

)
),
)
def test_array_api_mixed_pandas_integer_input(metric_name):

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.

This is the non regression test.

@pytest.mark.parametrize("label_type", ["string", "categorical"])
def test_array_api_classification_mixed_string_numeric_input(
metric_name, array_namespace, device_name, dtype_name
metric_name, array_namespace, device_name, dtype_name, label_type

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.

This test would already pass on main even with the pandas labels. But better increase test cases to ensure we don't break pandas categorical outputs in the future.

@cakedev0

Copy link
Copy Markdown
Contributor

Closes #34711

You referenced a closed PR, not the issue. The issue is #33822

@cakedev0 cakedev0 left a comment

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.

This PR looks good

get_namespace_and_device(pd.Index([1,2,3])) under config_context(array_api_dispatch=True) still crashes, so it's not full pandas support compared to without array API (which supports it). Not sure it's something we want to fix, and even less sure we want to fix it in this PR, I'll let you make this call.

Comment thread sklearn/metrics/tests/test_common.py Outdated
@ogrisel

ogrisel commented Aug 25, 2026

Copy link
Copy Markdown
Member Author

get_namespace_and_device(pd.Index([1,2,3])) under config_context(array_api_dispatch=True) still crashes

Do you have an example where this be internally called from scikit-learn public API only? If you find some, I think we can address that in a latter PR as the originally reported problem seems to be fixed.

@ogrisel

ogrisel commented Aug 25, 2026

Copy link
Copy Markdown
Member Author

@virchan @betatim any second opinion on the proposed fix?

@virchan virchan 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! Thanks, @ogrisel!

About accepting pandas.Index as input: I think it's better to wait for an issue reporting a concrete need before deciding. Right now, I don't think we urgently need this on main.

While researching, LLM suggested that pandas.Series.cat.categories could be passed as labels to confusion_matrix, e.g. to control label order for an ordinal categorical.

But I think this particular example is weak. We already have OrdinalEncoder for that, and we generally recommend users preprocess categorical variables themselves before calling metrics or estimators. So in this case, accepting Index wouldn't add value.

On the other hand, the fix itself would be easy. So if a stronger use case turns up, we can fix that quickly.

@virchan virchan added Pandas compatibility Waiting for Second Reviewer First reviewer is done, need a second one! labels Aug 26, 2026

@cakedev0 cakedev0 left a comment

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.

To reach the index thing from the public API, just replace pd.Series by pd.Index in the reproducer from the issue.

But I agree with Virgil, let's wait for a stronger case.

I think we can merge this PR.

@virchan

virchan commented Aug 26, 2026

Copy link
Copy Markdown
Member

Thank you everyone for your time! Merging!

@virchan
virchan merged commit d3ad363 into scikit-learn:main Aug 26, 2026
52 checks passed
@github-project-automation github-project-automation Bot moved this from In Progress to Done in Array API Aug 26, 2026
@ogrisel
ogrisel deleted the tst-array-api-metrics-and-pandas branch August 28, 2026 08:03
prady0t pushed a commit to prady0t/scikit-learn that referenced this pull request Sep 2, 2026
@jeremiedbb jeremiedbb mentioned this pull request Sep 8, 2026
14 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Archived in project

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