|
3 | 3 |
|
4 | 4 | from scipy.sparse import csr_matrix
|
5 | 5 | from scipy.sparse import csc_matrix
|
| 6 | +from scipy.sparse import coo_matrix |
6 | 7 | from scipy.linalg import eigh
|
7 | 8 | import numpy as np
|
8 | 9 | from numpy.testing import assert_array_almost_equal
|
| 10 | +from numpy.testing import assert_array_equal |
9 | 11 |
|
10 | 12 | from nose.tools import assert_raises
|
11 | 13 | from nose.plugins.skip import SkipTest
|
@@ -48,12 +50,50 @@ def _check_with_col_sign_flipping(A, B, tol=0.0):
|
48 | 50 | return True
|
49 | 51 |
|
50 | 52 |
|
| 53 | +def test_sparse_graph_connected_component(): |
| 54 | + rng = np.random.RandomState(42) |
| 55 | + n_samples = 300 |
| 56 | + boundaries = [0, 42, 121, 200, n_samples] |
| 57 | + p = rng.permutation(n_samples) |
| 58 | + connections = [] |
| 59 | + |
| 60 | + for start, stop in zip(boundaries[:-1], boundaries[1:]): |
| 61 | + group = p[start:stop] |
| 62 | + # Connect all elements within the group at least once via an |
| 63 | + # arbitrary path that spans the group. |
| 64 | + for i in range(len(group) - 1): |
| 65 | + connections.append((group[i], group[i + 1])) |
| 66 | + |
| 67 | + # Add some more random connections within the group |
| 68 | + min_idx, max_idx = 0, len(group) - 1 |
| 69 | + n_random_connections = 1000 |
| 70 | + source = rng.randint(min_idx, max_idx, size=n_random_connections) |
| 71 | + target = rng.randint(min_idx, max_idx, size=n_random_connections) |
| 72 | + connections.extend(zip(group[source], group[target])) |
| 73 | + |
| 74 | + # Build a symmetric affinity matrix |
| 75 | + row_idx, column_idx = tuple(np.array(connections).T) |
| 76 | + data = rng.uniform(.1, 42, size=len(connections)) |
| 77 | + affinity = coo_matrix((data, (row_idx, column_idx))) |
| 78 | + affinity = 0.5 * (affinity + affinity.T) |
| 79 | + |
| 80 | + for start, stop in zip(boundaries[:-1], boundaries[1:]): |
| 81 | + component_1 = _graph_connected_component(affinity, p[start]) |
| 82 | + component_size = stop - start |
| 83 | + assert_equal(component_1.sum(), component_size) |
| 84 | + |
| 85 | + # We should retrieve the same component mask by starting by both ends |
| 86 | + # of the group |
| 87 | + component_2 = _graph_connected_component(affinity, p[stop - 1]) |
| 88 | + assert_equal(component_2.sum(), component_size) |
| 89 | + assert_array_equal(component_1, component_2) |
| 90 | + |
| 91 | + |
51 | 92 | def test_spectral_embedding_two_components(seed=36):
|
52 | 93 | # Test spectral embedding with two components
|
53 | 94 | random_state = np.random.RandomState(seed)
|
54 | 95 | n_sample = 100
|
55 |
| - affinity = np.zeros(shape=[n_sample * 2, |
56 |
| - n_sample * 2]) |
| 96 | + affinity = np.zeros(shape=[n_sample * 2, n_sample * 2]) |
57 | 97 | # first component
|
58 | 98 | affinity[0:n_sample,
|
59 | 99 | 0:n_sample] = np.abs(random_state.randn(n_sample, n_sample)) + 2
|
@@ -208,7 +248,8 @@ def test_spectral_embedding_deterministic():
|
208 | 248 |
|
209 | 249 |
|
210 | 250 | def test_spectral_embedding_unnormalized():
|
211 |
| - # Test that spectral_embedding is also processing unnormalized laplacian correctly |
| 251 | + # Test that spectral_embedding is also processing unnormalized laplacian |
| 252 | + # correctly |
212 | 253 | random_state = np.random.RandomState(36)
|
213 | 254 | data = random_state.randn(10, 30)
|
214 | 255 | sims = rbf_kernel(data)
|
|
0 commit comments