Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0143fe4

Browse filesBrowse files
thomasjpfanglemaitre
authored andcommitted
FIX Fixes PLSRegression regression for constant Yk (#19922)
1 parent d04c274 commit 0143fe4
Copy full SHA for 0143fe4

File tree

Expand file treeCollapse file tree

3 files changed

+35
-5
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+35
-5
lines changed

‎doc/whats_new/v0.24.rst

Copy file name to clipboardExpand all lines: doc/whats_new/v0.24.rst
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ Changelog
2525
- |Fix| Fixed a regression in :class:`cross_decomposition.CCA`. :pr:`19646`
2626
by `Thomas Fan`_.
2727

28+
- |Fix| :class:`cross_decomposition.PLSRegression` raises warning for
29+
constant y residuals instead of a `StopIteration` error. :pr:`19922`
30+
by `Thomas Fan`_.
31+
2832
:mod:`sklearn.decomposition`
2933
............................
3034

‎sklearn/cross_decomposition/_pls.py

Copy file name to clipboardExpand all lines: sklearn/cross_decomposition/_pls.py
+16-5Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ def _get_first_singular_vectors_power_method(X, Y, mode="A", max_iter=500,
5252
"""
5353

5454
eps = np.finfo(X.dtype).eps
55-
y_score = next(col for col in Y.T if np.any(np.abs(col) > eps))
55+
try:
56+
y_score = next(col for col in Y.T if np.any(np.abs(col) > eps))
57+
except StopIteration as e:
58+
raise StopIteration("Y residual is constant") from e
59+
5660
x_weights_old = 100 # init to big value for first convergence check
5761

5862
if mode == 'B':
@@ -256,10 +260,17 @@ def fit(self, X, Y):
256260
Yk_mask = np.all(np.abs(Yk) < 10 * Y_eps, axis=0)
257261
Yk[:, Yk_mask] = 0.0
258262

259-
x_weights, y_weights, n_iter_ = \
260-
_get_first_singular_vectors_power_method(
261-
Xk, Yk, mode=self.mode, max_iter=self.max_iter,
262-
tol=self.tol, norm_y_weights=norm_y_weights)
263+
try:
264+
x_weights, y_weights, n_iter_ = \
265+
_get_first_singular_vectors_power_method(
266+
Xk, Yk, mode=self.mode, max_iter=self.max_iter,
267+
tol=self.tol, norm_y_weights=norm_y_weights)
268+
except StopIteration as e:
269+
if str(e) != "Y residual is constant":
270+
raise
271+
warnings.warn(f"Y residual is constant at iteration {k}")
272+
break
273+
263274
self.n_iter_.append(n_iter_)
264275

265276
elif self.algorithm == "svd":

‎sklearn/cross_decomposition/tests/test_pls.py

Copy file name to clipboardExpand all lines: sklearn/cross_decomposition/tests/test_pls.py
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,3 +565,18 @@ def test_loadings_converges():
565565

566566
# Loadings converges to reasonable values
567567
assert np.all(np.abs(cca.x_loadings_) < 1)
568+
569+
570+
def test_pls_constant_y():
571+
"""Checks warning when y is constant. Non-regression test for #19831"""
572+
rng = np.random.RandomState(42)
573+
x = rng.rand(100, 3)
574+
y = np.zeros(100)
575+
576+
pls = PLSRegression()
577+
578+
msg = "Y residual is constant at iteration"
579+
with pytest.warns(UserWarning, match=msg):
580+
pls.fit(x, y)
581+
582+
assert_allclose(pls.x_rotations_, 0)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.