Repository navigation
Closed
Labels
Description
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: NoneInterest in fixing the bug
No.
Reacted by Arthur Lacote
Activity
cakedev0 commentedon May 27, 2026
Thanks for the bug report! Hopefully, it shouldn't be too hard to fix, I'll look into it soon.
cakedev0 commentedon May 28, 2026
Note that it also unnecessary errors for array-like inputs (dataframes/list/...):