Skip to content

DOC document Array API limitation for LogisticRegression.sparsify - #34129

Merged
StefanieSenger merged 3 commits into
scikit-learn:mainfrom
sarveshvetrivel:doc/array-api-logistic-sparsify-limitation
May 28, 2026
Merged

StefanieSenger merged 3 commits into
scikit-learn:mainfrom
sarveshvetrivel:doc/array-api-logistic-sparsify-limitation

Conversation

@sarveshvetrivel

Copy link
Copy Markdown
Contributor

Reference Issues/PRs

Fixes #34114

What does this implement/fix?

Documents the limitation of calling .sparsify() on a LogisticRegression
estimator fitted with Array API inputs.

When array_api_dispatch=True is set via config_context, calling
.sparsify() on a fitted LogisticRegression silently succeeds but leaves
coef_ as a scipy.sparse matrix. This causes subsequent predict() calls
to raise a ValueError due to a device/namespace mismatch on coef_.

Changes:

  • doc/modules/array_api.rst: added a note next to the LogisticRegression
    entry describing this limitation
  • sklearn/linear_model/_base.py: added a .. warning:: block in the
    sparsify() docstring explaining that it is not supported for estimators
    fitted with Array API inputs

AI usage disclosure

I used AI assistance for:

  • Research and understanding

Any other comments?

@Jaidhuria

This comment was marked as off-topic.

@StefanieSenger StefanieSenger left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, @sarveshvetrivel.

Seems the same will be true for a few other models that inherit sparsify() from SparseCoefMixin (LinearSVC, SGD*, Perceptron, and PassiveAggressive*) as soon as they support Array API.

I've left you two comments.

Comment thread doc/modules/array_api.rst Outdated
Comment on lines +173 to +177
- :class:`linear_model.LogisticRegression` (with `solver="lbfgs"`); note that
calling :meth:`~linear_model.LogisticRegression.sparsify` on an estimator
fitted with Array API inputs is not supported: the call may silently succeed
but subsequent calls to :meth:`~linear_model.LogisticRegression.predict` will
raise a ``ValueError`` due to a namespace/device mismatch on ``coef_``

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- :class:`linear_model.LogisticRegression` (with `solver="lbfgs"`); note that
calling :meth:`~linear_model.LogisticRegression.sparsify` on an estimator
fitted with Array API inputs is not supported: the call may silently succeed
but subsequent calls to :meth:`~linear_model.LogisticRegression.predict` will
raise a ``ValueError`` due to a namespace/device mismatch on ``coef_``
- :class:`linear_model.LogisticRegression` (with `solver="lbfgs"`)

No, I think we should not add this to the list of estimators supporting Array API. It is too specific and we try to keep the docs as targeted as possible. Please remove the change in this file.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, removed the note from this file

Comment thread sklearn/linear_model/_base.py Outdated
Comment on lines +498 to +500
``array_api_dispatch=True``). The call may silently succeed but
will cause subsequent calls to :meth:`predict` to raise a
``ValueError`` due to a device/namespace mismatch in ``coef_``.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, in fact this method seems to work but the problem are the subsequent calls to other methods on the estimator.

For instance

import sklearn
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
X, y = load_iris(return_X_y=True)

with sklearn.config_context(array_api_dispatch=True):
    clf = LogisticRegression(random_state=0, max_iter=1000).fit(X, y)
    clf.sparsify()
    clf.predict(X[:2, :])

raises the ValueError

ValueError: Inputs passed to LogisticRegression.predict() must use the same namespace and the same device as those passed to fit(). Devices used during fit (None) and predict (cpu) 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)' 

But if we follow the instructions, then we do not convert the sparsified estimator back to a dense type using move_estimator_to:

from sklearn.utils._array_api import move_estimator_to, get_namespace_and_device
xp, _, device = get_namespace_and_device(X)
estimator = move_estimator_to(clf, xp, device)
estimator.coef_
#<Compressed Sparse Row sparse matrix of dtype 'float64'
#	with 12 stored elements and shape (3, 4)>

Also this may differ for other estimators that inherit sparsify() from SparseCoefMixin once they also support Array API in the future, we don't know yet.

Therefore, I would try to express this a bit more carefully/generally, maybe like this, but feel free to adjust:

Suggested change
``array_api_dispatch=True``). The call may silently succeed but
will cause subsequent calls to :meth:`predict` to raise a
``ValueError`` due to a device/namespace mismatch in ``coef_``.
``array_api_dispatch=True``). The call may succeed but subsequent
calls to :meth:`predict` and other methods involving passing arrays
may rise or return unexpected results.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, updated the wording as suggested.

@sarveshvetrivel
sarveshvetrivel force-pushed the doc/array-api-logistic-sparsify-limitation branch from edc639b to 69c945f Compare May 27, 2026 10:04

@StefanieSenger StefanieSenger left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks good to me. Thanks for adjusting, @sarveshvetrivel.

(For the future please don't force push, because this makes reviewing harder when more time passes in between reviews.)

@StefanieSenger StefanieSenger added the Waiting for Second Reviewer First reviewer is done, need a second one! label May 27, 2026
@sarveshvetrivel

Copy link
Copy Markdown
Contributor Author

That looks good to me. Thanks for adjusting, @sarveshvetrivel.

(For the future please don't force push, because this makes reviewing harder when more time passes in between reviews.)

Sure!

@virchan virchan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR, @sarveshvetrivel. I have a few comments.

Could we move the warning block to before the Returns section, since the warning is part of the method behaviour/usage rather than return-value information?

Otherwise, LGTM.

@sarveshvetrivel

Copy link
Copy Markdown
Contributor Author

Thank you for the PR, @sarveshvetrivel. I have a few comments.

Could we move the warning block to before the Returns section, since the warning is part of the method behaviour/usage rather than return-value information?

Otherwise, LGTM.

Done, moved the warning block to before the Returns section.

@virchan virchan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for updating the PR, @sarveshvetrivel.

I have one more nitpick.

Comment thread sklearn/linear_model/_base.py Resolved

@virchan virchan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks, @sarveshvetrivel!

@StefanieSenger, could we merge this?

@StefanieSenger

Copy link
Copy Markdown
Member

Yes, merging. Thanks everyone! :)

@StefanieSenger
StefanieSenger merged commit 3604450 into scikit-learn:main May 28, 2026
33 checks passed
@jeremiedbb jeremiedbb mentioned this pull request Jun 1, 2026
16 tasks done
jeremiedbb pushed a commit that referenced this pull request Jun 2, 2026
prady0t pushed a commit to prady0t/scikit-learn that referenced this pull request Sep 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Interactions between array API and sparse coefficients are not documented

4 participants