Repository navigation
DOC document Array API limitation for LogisticRegression.sparsify - #34129
StefanieSenger merged 3 commits into
Conversation
This comment was marked as off-topic.
This comment was marked as off-topic.
StefanieSenger
left a comment
There was a problem hiding this comment.
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.
| - :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_`` |
There was a problem hiding this comment.
| - :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.
There was a problem hiding this comment.
Done, removed the note from this file
| ``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_``. |
There was a problem hiding this comment.
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:
| ``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. |
There was a problem hiding this comment.
Done, updated the wording as suggested.
edc639b to
69c945f
Compare
StefanieSenger
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
Thank you for updating the PR, @sarveshvetrivel.
I have one more nitpick.
Co-authored-by: Virgil Chan <[email protected]>
virchan
left a comment
There was a problem hiding this comment.
LGTM! Thanks, @sarveshvetrivel!
@StefanieSenger, could we merge this?
|
Yes, merging. Thanks everyone! :) |
…4129) Co-authored-by: Virgil Chan <[email protected]>
…ikit-learn#34129) Co-authored-by: Virgil Chan <[email protected]>
Reference Issues/PRs
Fixes #34114
What does this implement/fix?
Documents the limitation of calling
.sparsify()on aLogisticRegressionestimator fitted with Array API inputs.
When
array_api_dispatch=Trueis set viaconfig_context, calling.sparsify()on a fittedLogisticRegressionsilently succeeds but leavescoef_as ascipy.sparsematrix. This causes subsequentpredict()callsto raise a
ValueErrordue to a device/namespace mismatch oncoef_.Changes:
doc/modules/array_api.rst: added a note next to theLogisticRegressionentry describing this limitation
sklearn/linear_model/_base.py: added a.. warning::block in thesparsify()docstring explaining that it is not supported for estimatorsfitted with Array API inputs
AI usage disclosure
I used AI assistance for:
Any other comments?