|
11 | 11 | from sklearn.utils.testing import SkipTest |
12 | 12 | from sklearn.utils.testing import assert_true |
13 | 13 | from sklearn.utils.testing import assert_false |
| 14 | +from sklearn.utils.testing import if_not_mac_os |
14 | 15 |
|
15 | 16 | from sklearn.decomposition import SparsePCA, MiniBatchSparsePCA |
16 | 17 | from sklearn.utils import check_random_state |
@@ -62,18 +63,29 @@ def test_fit_transform(): |
62 | 63 | spca_lars = SparsePCA(n_components=3, method='lars', alpha=alpha, |
63 | 64 | random_state=0) |
64 | 65 | spca_lars.fit(Y) |
| 66 | + |
| 67 | + # Test that CD gives similar results |
| 68 | + spca_lasso = SparsePCA(n_components=3, method='cd', random_state=0, |
| 69 | + alpha=alpha) |
| 70 | + spca_lasso.fit(Y) |
| 71 | + assert_array_almost_equal(spca_lasso.components_, spca_lars.components_) |
| 72 | + |
| 73 | + |
| 74 | +@if_not_mac_os() |
| 75 | +def test_fit_transform_parallel(): |
| 76 | + alpha = 1 |
| 77 | + rng = np.random.RandomState(0) |
| 78 | + Y, _, _ = generate_toy_data(3, 10, (8, 8), random_state=rng) # wide array |
| 79 | + spca_lars = SparsePCA(n_components=3, method='lars', alpha=alpha, |
| 80 | + random_state=0) |
| 81 | + spca_lars.fit(Y) |
65 | 82 | U1 = spca_lars.transform(Y) |
66 | 83 | # Test multiple CPUs |
67 | 84 | spca = SparsePCA(n_components=3, n_jobs=2, method='lars', alpha=alpha, |
68 | 85 | random_state=0).fit(Y) |
69 | 86 | U2 = spca.transform(Y) |
70 | 87 | assert_true(not np.all(spca_lars.components_ == 0)) |
71 | 88 | assert_array_almost_equal(U1, U2) |
72 | | - # Test that CD gives similar results |
73 | | - spca_lasso = SparsePCA(n_components=3, method='cd', random_state=0, |
74 | | - alpha=alpha) |
75 | | - spca_lasso.fit(Y) |
76 | | - assert_array_almost_equal(spca_lasso.components_, spca_lars.components_) |
77 | 89 |
|
78 | 90 |
|
79 | 91 | def test_transform_nan(): |
|
0 commit comments