Skip to content

Commit

Permalink
Add test for scale=True
Browse files Browse the repository at this point in the history
  • Loading branch information
lesteve committed Jun 7, 2017
1 parent 18eeb77 commit a6974d5
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion sklearn/cross_decomposition/tests/test_pls.py
@@ -1,4 +1,6 @@
import numpy as np
from numpy.testing import assert_approx_equal

from sklearn.utils.testing import (assert_equal, assert_array_almost_equal,
assert_array_equal, assert_true,
assert_raise_message)
Expand Down Expand Up @@ -351,11 +353,38 @@ def test_scale_and_stability():
assert_array_almost_equal(X_s_score, X_score)
assert_array_almost_equal(Y_s_score, Y_score)


def test_pls_errors():
d = load_linnerud()
X = d.data
Y = d.target
for clf in [pls_.PLSCanonical(), pls_.PLSRegression(),
pls_.PLSSVD()]:
clf.n_components = 4
assert_raise_message(ValueError, "Invalid number of components", clf.fit, X, Y)
assert_raise_message(ValueError, "Invalid number of components",
clf.fit, X, Y)


def test_pls_scaling():
# sanity check for scale=True
n_samples = 1000
n_targets = 5
n_features = 10

rng = np.random.RandomState(0)

Q = rng.randn(n_targets, n_features)
Y = rng.randn(n_samples, n_targets)
X = np.dot(Y, Q) + 2 * rng.randn(n_samples, n_features)

scale = 1000.
X_scaled = scale * X

pls = pls_.PLSRegression(n_components=5, scale=True)
pls.fit(X, Y)
score = pls.score(X, Y)

pls.fit(X_scaled, Y)
score_scaled = pls.score(X_scaled, Y)

assert_approx_equal(score, score_scaled)

0 comments on commit a6974d5

Please sign in to comment.