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

Fix: Array-API - avoid failing for numpy fit + predict with sparse or array-like X#34144
OmarManzoor merged 16 commits into
scikit-learn:mainfrom
cakedev0:fix/34132

Conversation

@cakedev0
Copy link
Copy Markdown
Contributor

@cakedev0 cakedev0 commented May 28, 2026

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
Copy link
Copy Markdown
Contributor Author

cakedev0 commented May 28, 2026

Indeed, good catch thanks.

Edit: Fixed.

Comment thread sklearn/utils/tests/test_array_api.py
Comment thread sklearn/utils/_array_api.py Outdated
Comment thread sklearn/utils/_array_api.py Outdated
@ogrisel
Copy link
Copy Markdown
Member

ogrisel commented May 29, 2026

@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
Co-authored-by: Olivier Grisel <olivier.grisel@ensta.org>
Co-authored-by: Anne Beyer <anne.beyer@mailbox.org>
@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
Copy link
Copy Markdown
Contributor

@OmarManzoor OmarManzoor left a comment

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 Outdated
cakedev0 and others added 2 commits June 1, 2026 09:50
Co-authored-by: Omar Salman <omar.salman@arbisoft.com>
@OmarManzoor OmarManzoor enabled auto-merge (squash) June 1, 2026 07:51
Comment thread sklearn/utils/tests/test_array_api.py Outdated
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
38 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
jeremiedbb pushed a commit that referenced this pull request Jun 2, 2026
… array-like X (#34144)

Co-authored-by: Olivier Grisel <olivier.grisel@ensta.org>
Co-authored-by: Anne Beyer <anne.beyer@mailbox.org>
Co-authored-by: Omar Salman <omar.salman@arbisoft.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Namespace checks trigger unnecessary errors for sparse matrices under array API

6 participants