Skip to content

MNT Remove DeprecationWarning for scipy.sparse.linalg.cg tol vs rtol argument #26814

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Aug 18, 2023
7 changes: 7 additions & 0 deletions doc/whats_new/v1.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ random sampling procedures.
and has been fixed.
:pr:`26416` by :user:`Yang Tao <mchikyt3>`.

- |Fix| Ridge models with `solver='sparse_cg'` may have slightly different
results with scipy>=1.12, because of an underlying change in the scipy solver
(see `scipy#18488 <https://github.com/scipy/scipy/pull/18488>`_ for more
details)
:pr:`26814` by :user:`Loïc Estève <lesteve>`


Changes impacting all modules
-----------------------------

Expand Down
7 changes: 3 additions & 4 deletions sklearn/linear_model/_ridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
)
from ..utils._param_validation import Interval, StrOptions, validate_params
from ..utils.extmath import row_norms, safe_sparse_dot
from ..utils.fixes import _sparse_linalg_cg
from ..utils.sparsefuncs import mean_variance_axis
from ..utils.validation import _check_sample_weight, check_is_fitted
from ._base import LinearClassifierMixin, LinearModel, _preprocess_data, _rescale_data
Expand Down Expand Up @@ -105,7 +106,7 @@ def _mv(x):
C = sp_linalg.LinearOperator(
(n_samples, n_samples), matvec=mv, dtype=X.dtype
)
coef, info = sp_linalg.cg(C, y_column, tol=tol, atol="legacy")
coef, info = _sparse_linalg_cg(C, y_column, rtol=tol)
coefs[i] = X1.rmatvec(coef)
else:
# linear ridge
Expand All @@ -114,9 +115,7 @@ def _mv(x):
C = sp_linalg.LinearOperator(
(n_features, n_features), matvec=mv, dtype=X.dtype
)
coefs[i], info = sp_linalg.cg(
C, y_column, maxiter=max_iter, tol=tol, atol="legacy"
)
coefs[i], info = _sparse_linalg_cg(C, y_column, maxiter=max_iter, rtol=tol)

if info < 0:
raise ValueError("Failed with error code %d" % info)
Expand Down
14 changes: 14 additions & 0 deletions sklearn/utils/fixes.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import numpy as np
import scipy
import scipy.sparse.linalg
import scipy.stats
import threadpoolctl

Expand Down Expand Up @@ -109,6 +110,19 @@ def _mode(a, axis=0):
return scipy.stats.mode(a, axis=axis)


# TODO: Remove when Scipy 1.12 is the minimum supported version
if sp_base_version >= parse_version("1.12.0"):
_sparse_linalg_cg = scipy.sparse.linalg.cg
else:

def _sparse_linalg_cg(A, b, **kwargs):
if "rtol" in kwargs:
kwargs["tol"] = kwargs.pop("rtol")
if "atol" not in kwargs:
kwargs["atol"] = "legacy"
return scipy.sparse.linalg.cg(A, b, **kwargs)


###############################################################################
# Backport of Python 3.9's importlib.resources
# TODO: Remove when Python 3.9 is the minimum supported version
Expand Down