|
1 | 1 | import numpy as np |
| 2 | +from numpy.testing import assert_approx_equal |
| 3 | + |
2 | 4 | from sklearn.utils.testing import (assert_equal, assert_array_almost_equal, |
3 | 5 | assert_array_equal, assert_true, |
4 | 6 | assert_raise_message) |
5 | 7 | from sklearn.datasets import load_linnerud |
6 | 8 | from sklearn.cross_decomposition import pls_, CCA |
| 9 | +from sklearn.preprocessing import StandardScaler |
7 | 10 |
|
8 | 11 |
|
9 | 12 | def test_pls(): |
@@ -351,11 +354,38 @@ def test_scale_and_stability(): |
351 | 354 | assert_array_almost_equal(X_s_score, X_score) |
352 | 355 | assert_array_almost_equal(Y_s_score, Y_score) |
353 | 356 |
|
| 357 | + |
354 | 358 | def test_pls_errors(): |
355 | 359 | d = load_linnerud() |
356 | 360 | X = d.data |
357 | 361 | Y = d.target |
358 | 362 | for clf in [pls_.PLSCanonical(), pls_.PLSRegression(), |
359 | 363 | pls_.PLSSVD()]: |
360 | 364 | clf.n_components = 4 |
361 | | - assert_raise_message(ValueError, "Invalid number of components", clf.fit, X, Y) |
| 365 | + assert_raise_message(ValueError, "Invalid number of components", |
| 366 | + clf.fit, X, Y) |
| 367 | + |
| 368 | + |
| 369 | +def test_pls_scaling(): |
| 370 | + # sanity check for scale=True |
| 371 | + n_samples = 1000 |
| 372 | + n_targets = 5 |
| 373 | + n_features = 10 |
| 374 | + |
| 375 | + rng = np.random.RandomState(0) |
| 376 | + |
| 377 | + Q = rng.randn(n_targets, n_features) |
| 378 | + Y = rng.randn(n_samples, n_targets) |
| 379 | + X = np.dot(Y, Q) + 2 * rng.randn(n_samples, n_features) + 1 |
| 380 | + X *= 1000 |
| 381 | + X_scaled = StandardScaler().fit_transform(X) |
| 382 | + |
| 383 | + pls = pls_.PLSRegression(n_components=5, scale=True) |
| 384 | + |
| 385 | + pls.fit(X, Y) |
| 386 | + score = pls.score(X, Y) |
| 387 | + |
| 388 | + pls.fit(X_scaled, Y) |
| 389 | + score_scaled = pls.score(X_scaled, Y) |
| 390 | + |
| 391 | + assert_approx_equal(score, score_scaled) |
0 commit comments