Skip to content

Namespace checks trigger unnecessary errors for sparse matrices under array API #34132

Closed

Description

@david-cortes-intel

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

Without array API, estimators that support sparse inputs will typically accept them for both .fit() and predict-related methods, independently of each other.

For example, it's possible to fit a LogisticRegression on dense data and predict on sparse data:

import numpy as np
from scipy.sparse import random as sp_random

rng = np.random.default_rng(seed=123)
X_sp = sp_random(20, 10, .3, random_state=123, format="csr")
X = X_sp.toarray()
y = rng.integers(2, size=X.shape[0])

from sklearn.linear_model import LogisticRegression
model = LogisticRegression().fit(X, y)
model.predict(X_sp)
array([0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0])

But if array API is enabled, this will trigger an error due to namespace and device checks:

import os
os.environ["SCIPY_ARRAY_API"] = "1"

import numpy as np
from scipy.sparse import random as sp_random

rng = np.random.default_rng(seed=123)
X_sp = sp_random(20, 10, .3, random_state=123, format="csr")
X = X_sp.toarray()
y = rng.integers(2, size=X.shape[0])

from sklearn import set_config
set_config(array_api_dispatch=True)

from sklearn.linear_model import LogisticRegression
model = LogisticRegression().fit(X, y)
model.predict(X_sp)
ValueError: Inputs passed to LogisticRegression.predict() must use the same namespace and the same device as those passed to fit(). Devices used during fit (cpu) and predict (None) differ. You can move the estimator to the same namespace and device as X with: 'from sklearn.utils._array_api import move_estimator_to; xp, _, device = get_namespace_and_device(X); estimator = move_estimator_to(estimator, xp, device)'

Steps/Code to Reproduce

Code is given above.

Expected Results

Should work, like it does under array_api_dispatch=False.

Actual Results

Doesn't work.

Versions

System:
    python: 3.13.13 | packaged by conda-forge | (main, Apr  8 2026, 02:00:33) [GCC 14.3.0]
executable: <path>/miniforge3/envs/dpnp/bin/python
   machine: Linux-6.17.0-20-generic-x86_64-with-glibc2.39

Python dependencies:
      sklearn: 1.10.dev0
          pip: 26.1.1
   setuptools: 82.0.1
        numpy: 2.4.4
        scipy: 1.17.1
       Cython: 3.2.4
       pandas: 3.0.3
   matplotlib: None
       joblib: 1.5.3
threadpoolctl: 3.6.0

Built with OpenMP: True

threadpoolctl info:
       user_api: blas
   internal_api: openblas
    num_threads: 64
         prefix: libscipy_openblas
       filepath: <path>/miniforge3/envs/dpnp/lib/python3.13/site-packages/numpy.libs/libscipy_openblas64_-32a4b2a6.so
        version: 0.3.31.188.0
threading_layer: pthreads
   architecture: SkylakeX

       user_api: blas
   internal_api: openblas
    num_threads: 64
         prefix: libscipy_openblas
       filepath: <path>/miniforge3/envs/dpnp/lib/python3.13/site-packages/scipy.libs/libscipy_openblas-6cdc3b4a.so
        version: 0.3.30
threading_layer: pthreads
   architecture: SkylakeX

       user_api: openmp
   internal_api: openmp
    num_threads: 224
         prefix: libomp
       filepath: <path>/miniforge3/envs/dpnp/lib/libomp.so
        version: None

Interest in fixing the bug

No.

Activity

  1. cakedev0 commented on May 27, 2026

    @cakedev0
    Member

    Thanks for the bug report! Hopefully, it shouldn't be too hard to fix, I'll look into it soon.

  2. moved this to Todo in Labson May 27, 2026
  3. cakedev0 commented on May 28, 2026

    @cakedev0
    Member

    Note that it also unnecessary errors for array-like inputs (dataframes/list/...):

    import os
    os.environ["SCIPY_ARRAY_API"] = "1"
    
    import numpy as np
    from scipy.sparse import random as sp_random
    import pandas as pd
    
    rng = np.random.default_rng(seed=123)
    X_sp = sp_random(20, 10, .3, random_state=123, format="csr")
    X = X_sp.toarray()
    y = rng.integers(2, size=X.shape[0])
    
    from sklearn import config_context
    from sklearn.linear_model import LogisticRegression
    
    with config_context(array_api_dispatch=True):
        model = LogisticRegression().fit(X, y)
        # model.predict(X_sp)
        # also raises:
        # model.predict(pd.DataFrame(X))
        model.predict([list(x) for x in X])
  4. moved this from Todo to In progress in Labson May 28, 2026
  5. removed
    Needs Triage
    on May 28, 2026
  6. moved this from In progress to Done in Labson Jun 1, 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

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions