Skip to content

Fix: Array-API - avoid failing for numpy fit + predict with sparse or array-like X - #34144

Merged
OmarManzoor merged 16 commits into
scikit-learn:mainfrom
cakedev0:fix/34132
Jun 1, 2026
Merged

OmarManzoor merged 16 commits into
scikit-learn:mainfrom
cakedev0:fix/34132

Conversation

@cakedev0

@cakedev0 cakedev0 commented May 28, 2026 •

Copy link
Copy Markdown
Member

Reference Issues/PRs

Fixes #34132

What does this implement/fix? Explain your changes.

  • Add a condition in check_same_namespace to allow the cases:
    • [estimator xp is numpy, X is not an array]
    • [estimator is sparsified, X is a numpy array]
  • Add a (hopefully) helpful error message for the case [estimator's xp is NOT numpy, X is not an array], for which the current message might be quite hard to understand.
  • Add tests

AI usage disclosure

I used AI assistance for:

  • Test/benchmark generation
  • Research and understanding

@david-cortes-intel

Copy link
Copy Markdown
Contributor

Thanks for looking into it.

But this would still error out in the cases where the attribute being checked is sparse. For example:

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

import numpy as np
rng = np.random.default_rng(seed=123)
X = rng.random(size=(20, 3))
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.sparsify()
model.predict(X)

@cakedev0

cakedev0 commented May 28, 2026 •

Copy link
Copy Markdown
Member Author

Indeed, good catch thanks.

Edit: Fixed.

Comment thread sklearn/utils/tests/test_array_api.py Resolved
Comment thread sklearn/utils/_array_api.py Resolved
Comment thread sklearn/utils/_array_api.py Resolved
@ogrisel

ogrisel commented May 29, 2026

Copy link
Copy Markdown
Member

@cakedev0 We also need a changelog entry for this fix.

@ogrisel ogrisel moved this to In Progress in Array API May 29, 2026
cakedev0 and others added 2 commits May 29, 2026 15:43
@github-actions github-actions Bot added the CI:Linter failure The linter CI is failing on this PR label May 29, 2026
@github-actions github-actions Bot removed the CI:Linter failure The linter CI is failing on this PR label May 29, 2026

@OmarManzoor OmarManzoor 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.

A really minor error message content suggestion only. Otherwise LGTM.

Thank you @cakedev0

Comment thread sklearn/utils/_array_api.py Resolved
@OmarManzoor
OmarManzoor enabled auto-merge (squash) June 1, 2026 07:51
Comment thread sklearn/utils/tests/test_array_api.py Resolved
auto-merge was automatically disabled June 1, 2026 08:16

Head branch was pushed to by a user without write access

@OmarManzoor
OmarManzoor merged commit 93bdc13 into scikit-learn:main Jun 1, 2026
33 checks passed
@github-project-automation github-project-automation Bot moved this from In Progress to Done in Array API Jun 1, 2026
@jeremiedbb jeremiedbb mentioned this pull request Jun 1, 2026
16 tasks done
jeremiedbb pushed a commit that referenced this pull request Jun 2, 2026
… array-like X (#34144)

Co-authored-by: Olivier Grisel <[email protected]>
Co-authored-by: Anne Beyer <[email protected]>
Co-authored-by: Omar Salman <[email protected]>
prady0t pushed a commit to prady0t/scikit-learn that referenced this pull request Sep 2, 2026
… array-like X (scikit-learn#34144)

Co-authored-by: Olivier Grisel <[email protected]>
Co-authored-by: Anne Beyer <[email protected]>
Co-authored-by: Omar Salman <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Namespace checks trigger unnecessary errors for sparse matrices under array API

6 participants