FIX array API classification metrics with pandas labels - #34779
Conversation
| if ( | ||
| _is_xp_namespace(xp, "torch") | ||
| and _is_numpy_namespace(xp_array) | ||
| and any(stride < 0 for stride in array.strides) |
There was a problem hiding this comment.
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) |
|
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): |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.
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. |
virchan
left a comment
There was a problem hiding this comment.
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.
cakedev0
left a comment
There was a problem hiding this comment.
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.
|
Thank you everyone for your time! Merging! |
Closes #33822.