From 6bdb9da1580a2a5d4d39f29491f0b695eaf6c4d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Est=C3=A8ve?= Date: Mon, 10 Jul 2023 15:04:30 +0200 Subject: [PATCH 1/9] MNT Remove DeprecationWarning for scipy.sparse.linalg.cg tol vs rtol argument --- sklearn/linear_model/_ridge.py | 17 +++-------------- sklearn/utils/fixes.py | 12 ++++++++++++ 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/sklearn/linear_model/_ridge.py b/sklearn/linear_model/_ridge.py index 5d498b7e13830..0258a379b8852 100644 --- a/sklearn/linear_model/_ridge.py +++ b/sklearn/linear_model/_ridge.py @@ -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 @@ -105,12 +106,7 @@ def _mv(x): C = sp_linalg.LinearOperator( (n_samples, n_samples), matvec=mv, dtype=X.dtype ) - # FIXME atol - try: - coef, info = sp_linalg.cg(C, y_column, tol=tol, atol="legacy") - except TypeError: - # old scipy - coef, info = sp_linalg.cg(C, y_column, tol=tol) + coef, info = _sparse_linalg_cg(C, y_column, rtol=tol) coefs[i] = X1.rmatvec(coef) else: # linear ridge @@ -119,14 +115,7 @@ def _mv(x): C = sp_linalg.LinearOperator( (n_features, n_features), matvec=mv, dtype=X.dtype ) - # FIXME atol - try: - coefs[i], info = sp_linalg.cg( - C, y_column, maxiter=max_iter, tol=tol, atol="legacy" - ) - except TypeError: - # old scipy - coefs[i], info = sp_linalg.cg(C, y_column, maxiter=max_iter, tol=tol) + 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) diff --git a/sklearn/utils/fixes.py b/sklearn/utils/fixes.py index 2202a1daaf90a..8765faee25b19 100644 --- a/sklearn/utils/fixes.py +++ b/sklearn/utils/fixes.py @@ -15,6 +15,7 @@ import numpy as np import scipy +import scipy.sparse.linalg import scipy.stats import threadpoolctl @@ -109,6 +110,17 @@ def _mode(a, axis=0): return scipy.stats.mode(a, axis=axis) +# TODO: Remove when Scipy 1.12 is the minimum supported version +if parse_version(scipy.__version__) >= parse_version("1.12.0.dev0"): + _sparse_linalg_cg = scipy.sparse.linalg.cg +else: + + def _sparse_linalg_cg(A, b, **kwargs): + if "rtol" in kwargs: + kwargs["tol"] = kwargs.pop("rtol") + 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 From 15004cac5082a5d90b759ebb2a667885348654ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Est=C3=A8ve?= Date: Mon, 10 Jul 2023 15:59:40 +0200 Subject: [PATCH 2/9] [scipy-dev] From 5f95e34c0f2c7ac0ccd3d109fcd242c87f939a47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Est=C3=A8ve?= Date: Tue, 11 Jul 2023 11:15:50 +0200 Subject: [PATCH 3/9] tweak --- sklearn/utils/fixes.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sklearn/utils/fixes.py b/sklearn/utils/fixes.py index 8765faee25b19..b690302777fb7 100644 --- a/sklearn/utils/fixes.py +++ b/sklearn/utils/fixes.py @@ -111,13 +111,15 @@ def _mode(a, axis=0): # TODO: Remove when Scipy 1.12 is the minimum supported version -if parse_version(scipy.__version__) >= parse_version("1.12.0.dev0"): +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) From 4e25ec925e720411c4123d20f5c7309822a53b64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Est=C3=A8ve?= Date: Tue, 11 Jul 2023 11:16:03 +0200 Subject: [PATCH 4/9] [scipy-dev] From 45afc43d19ed2fd06d125fc2fc653afb7ad272ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Est=C3=A8ve?= Date: Mon, 7 Aug 2023 14:12:52 +0200 Subject: [PATCH 5/9] [scipy-dev] [azure parallel] From 14dce1e13d83c7ab8291ec4b89d2e5cbd9dc443d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Est=C3=A8ve?= Date: Fri, 18 Aug 2023 10:18:13 +0200 Subject: [PATCH 6/9] [scipy-dev] Add changelog --- doc/whats_new/v1.4.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/doc/whats_new/v1.4.rst b/doc/whats_new/v1.4.rst index 3508d85fdcbff..8a5650c0916d3 100644 --- a/doc/whats_new/v1.4.rst +++ b/doc/whats_new/v1.4.rst @@ -24,6 +24,12 @@ random sampling procedures. and has been fixed. :pr:`26416` by :user:`Yang Tao `. +- |Fix| Ridge models with `solver='sparse_cg'` may have slightly different + results because of an underlying change in the scipy solver related to + tolerance handling. + :pr:`26814` by :user:`Loïc Estève ` + + Changes impacting all modules ----------------------------- From 8a2816d4f9bbeb245e585fe46620399a859b5a0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Est=C3=A8ve?= Date: Fri, 18 Aug 2023 10:22:51 +0200 Subject: [PATCH 7/9] [scipy-dev] tweak changelog --- doc/whats_new/v1.4.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/doc/whats_new/v1.4.rst b/doc/whats_new/v1.4.rst index 8a5650c0916d3..73fbae83140a0 100644 --- a/doc/whats_new/v1.4.rst +++ b/doc/whats_new/v1.4.rst @@ -25,8 +25,9 @@ random sampling procedures. :pr:`26416` by :user:`Yang Tao `. - |Fix| Ridge models with `solver='sparse_cg'` may have slightly different - results because of an underlying change in the scipy solver related to - tolerance handling. + results with scipy>=1.12, because of an underlying change in the scipy solver + (see `scipy/#18488 `_ for more + details) :pr:`26814` by :user:`Loïc Estève ` From e73274018b3c9f492848c0410debca65820f8cea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Est=C3=A8ve?= Date: Fri, 18 Aug 2023 10:23:48 +0200 Subject: [PATCH 8/9] [scipy-dev] tweak changelog --- doc/whats_new/v1.4.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/whats_new/v1.4.rst b/doc/whats_new/v1.4.rst index 73fbae83140a0..871475f259289 100644 --- a/doc/whats_new/v1.4.rst +++ b/doc/whats_new/v1.4.rst @@ -26,7 +26,7 @@ random sampling procedures. - |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 `_ for more + (see `scipy#18488 `_ for more details) :pr:`26814` by :user:`Loïc Estève ` From 8d43a313e89284e20e4e8ad25189358ee393b3e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Est=C3=A8ve?= Date: Fri, 18 Aug 2023 11:11:01 +0200 Subject: [PATCH 9/9] [scipy-dev] [azure parallel]