Skip to content

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

Closed

Description

@david-cortes-intel

Describe the bug and give evidence about its user-facing impact

The logic for array API was recently modified to allow 'y' inputs of a potentially different type than 'X', particularly when it comes to classifiers where 'y' might be strings.

But it appears this logic does not always work when y is a pandas object, or at least doesn't work for all methods.

I guess an easy fix would be to convert pandas 'y' to numpy inside move_to, but not sure if by that point it is always passed as an integer-encoded array for classifiers.

Steps/Code to Reproduce

import os
os.environ["SCIPY_ARRAY_API"] = "1"
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)

Expected Results

No error.

Actual Results

Traceback (most recent call last):
  Cell In[3], line 18
    score = model.score(Xt, ys)
  File ~/code/scikit-learn/sklearn/base.py:615 in score
    return accuracy_score(y, self.predict(X), sample_weight=sample_weight)
  File ~/code/scikit-learn/sklearn/utils/_param_validation.py:218 in wrapper
    return func(*args, **kwargs)
  File ~/code/scikit-learn/sklearn/metrics/_classification.py:407 in accuracy_score
    y_true, sample_weight = move_to(y_true, sample_weight, xp=xp, device=device)
  File ~/code/scikit-learn/sklearn/utils/_array_api.py:602 in move_to
    array_converted = xp.from_dlpack(array, device=device)
  File ~/venvs/uv-xpu-dpnp/lib/python3.13/site-packages/torch/utils/dlpack.py:226 in from_dlpack
    raise AssertionError(
AssertionError: device and copy kwargs not supported when ext_tensor is already a DLPack capsule

Versions

System:
    python: 3.13.12 | packaged by conda-forge | (main, Feb  5 2026, 05:53:46) [GCC 14.3.0]
executable: /export/users/dcortes/miniforge3/envs/torch19/bin/python
   machine: Linux-6.8.0-49-generic-x86_64-with-glibc2.39

Python dependencies:
      sklearn: 1.9.dev0
          pip: 26.0.1
   setuptools: 82.0.0
        numpy: 2.4.2
        scipy: 1.17.0
       Cython: 3.2.4
       pandas: 3.0.0
   matplotlib: 3.10.8
       joblib: 1.5.3
threadpoolctl: 3.6.0

Built with OpenMP: True

threadpoolctl info:
       user_api: blas
   internal_api: openblas
    num_threads: 128
         prefix: libopenblas
       filepath: /localdisk2/mkl/dcortes/miniforge3/envs/torch19/lib/libopenblasp-r0.3.30.so
        version: 0.3.30
threading_layer: pthreads
   architecture: SapphireRapids

       user_api: openmp
   internal_api: openmp
    num_threads: 112
         prefix: libgomp
       filepath: /localdisk2/mkl/dcortes/miniforge3/envs/torch19/lib/libgomp.so.1.0.0
        version: None

Interest in fixing the bug

No.

Activity

  1. virchan commented on Apr 22, 2026

    @virchan
    Member

    Thank you for the report, @david-cortes-intel.

    scikit-learn’s array API support is limited to NumPy, CuPy, and PyTorch. Since Pandas is not one of them, the behaviour you observed is expected.

    Closing as not a bug.

    Edit: See #33822 (comment).

  2. added and removed
    Needs Triage
    on Apr 22, 2026
  3. lorentzenchr commented on Apr 22, 2026

    @lorentzenchr
    Member

    @virchan Don‘t you find the error message cryptic?

  4. david-cortes-intel commented on Apr 22, 2026

    @david-cortes-intel
    ContributorAuthor

    @virchan Thanks for your quick response. This detail is very unclear from the documentation for array API, and it's not obvious to me from what one can find throughout this issue tracker that it is the intended design either (e.g. #32836 #28668 (comment))

    Would be very helpful if the docs could describe in more details these compatibility constraints.

  5. virchan commented on Apr 22, 2026

    @virchan
    Member

    @virchan Don‘t you find the error message cryptic?

    I don't, but that's probably because I've become numb after working on Array API support for a while and, as a result, overlooked the user's point of view 😅.

    But I see your point now, @david-cortes-intel. The move_to function should be able to move Pandas dataframe to the reference array type, since we consider it as NumPy compat namespace in #32838.

    Reopening the issue, and apologies for the noise.

  6. betatim commented on Apr 28, 2026

    @betatim
    Member

    I'm not sure if we should start accepting pd.Series and converting it for the user. The reason I am a bit hesitant is that once we start allowing it, we can't easily go back to not accepting it. This is what makes me want to ponder this for a bit.

    For example, I think we should not get in a situation where it looks like move_to(X_df, ..) would work. I think when X is a data frame we should take the "not array API" path through the scikit-learn code and not trigger a conversion of it to Numpy.

    The reason I bring up moving X is that it feels like that is an edge case we should consider in the decision of how all this should work.

    A reason to support converting a y = pd.Series() for the user is that I think you can pass y = list() and we convert that too. Even though a list isn't a array API thing.


    We should update the documentation and improve the error messages. No matter if we decide to accept pd.Series there will always be things that we do not accept, so having a good error message seems like a good thing.

  7. ogrisel commented on Apr 30, 2026

    @ogrisel
    Member

    I think we can/should accept pd.Series when it has a numerical dtype (but fail otherwise, as for a numpy array with str or object dtype).

  8. 3 remaining items

  9. moved this to Todo in Labson May 18, 2026
  10. moved this from Todo to In progress in Labson May 18, 2026
  11. moved this from In progress to Todo in Labson May 18, 2026
  12. ogrisel commented on May 18, 2026

    @ogrisel
    Member

    Indeed, there is a problem in the way we do things. I think we should not try to move y_true when it holds non-numeric content in classification metrics. Instead, we should move the result of label encoding / one-hot / ordinal encoding of the class labels into boolean or integer class membership indicators just before they are combined with y_pred and sample_weight using the namespace / device / dtype of y_pred. The details of how this happens is specific for each classification metric I believe.

  13. ogrisel commented on May 20, 2026

    @ogrisel
    Member

    Note that there is a related fix in #34065 to make RidgeClassifier.fit pass the common tests for mixed input namespaces, but that does not fix the problem with .score. We probably need to improve the common test to cover this.

  14. StefanieSenger commented on May 23, 2026

    @StefanieSenger
    Member

    I can see how from a user's perspective, one might expect a pandas series or df to comply to the array API rules, since we did not document otherwise and pandas input is usually converted to numpy in the process (in check_array). Thanks for reporting that, @david-cortes-intel.

    But in array API we are explicitly excluding pandas from being an array namespace. In _remove_non_arrays (called in get_namespace_and_device), we filter out dataframe inputs:

    if is_df_or_series(array):
    continue

    Therefore, and because we are planning to support narwhals, I think it is better not to take pandas objects under the umbrella of array_api_dispatch=True right now (which in my mind would be a new feature).

    I would suggest two actions:

    1. document cleanly that we don't consider dataframe inputs as array containers
    2. catch the torch error and re-raise it with a clearer scikit-learn message

    CC, @AnneBeyer, because this issue is a good starting point for thinking about the scope of array API, if you are interested to participate.

  15. ogrisel commented on May 28, 2026

    @ogrisel
    Member

    I find it unexpected that model.score(X, y) would accept y as a pandas series when array_api_dispatch=False but would raise an exception when array_api_dispatch=True.

    The goal is to enable array API dispatch by default in the future, we should not introduce a usability regression when doing so.

    I think we need to find a way to change to ensure that model.fit(X, y) and model.score(X, y) still accept pandas dataframes and series whether array API dispatch is enabled or not.

    Since when array API dispatch is disabled, this particular estimator treats pandas series as array-like and converts them to NumPy array, we should continue doing so here.

    Other estimators such as ColumnTransformer have particular handling of pandas dataframes, and we should ensure that we do not break that.

  16. ogrisel commented on May 28, 2026

    @ogrisel
    Member

    I think this is a bug to me, and we probably need to extend the common test discussed in #34094 to cover non-array API array-like inputs (in particular dataframes and series).

  17. moved this from Todo to In progress in Labson Aug 10, 2026
  18. moved this from In progress to Done in Labson Aug 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions