def test_predict(self):
     spectral = Spectral(4)
     spectral.fit(self.adjacency)
     unit_vector = np.zeros(self.adjacency.shape[0])
     unit_vector[0] = 1
     error = max(abs(spectral.predict(self.adjacency.dot(unit_vector)) - spectral.embedding_[0]))
     self.assertAlmostEqual(error, 0)
Ejemplo n.º 2
0
 def test_bipartite(self):
     for biadjacency in [
             test_digraph(),
             test_bigraph(),
             test_bigraph_disconnect()
     ]:
         n_row, n_col = biadjacency.shape
         adjacency = bipartite2undirected(biadjacency)
         # normalized Laplacian
         spectral = Spectral(3)
         spectral.fit(biadjacency)
         embedding_full = np.vstack(
             [spectral.embedding_row_, spectral.embedding_col_])
         weights = adjacency.dot(np.ones(n_row + n_col))
         if not is_connected(adjacency):
             weights += 1
         self.assertAlmostEqual(
             np.linalg.norm(embedding_full.T.dot(weights)), 0)
         # regular Laplacian
         spectral = Spectral(3, normalized_laplacian=False)
         spectral.fit(biadjacency)
         embedding_full = np.vstack(
             [spectral.embedding_row_, spectral.embedding_col_])
         self.assertAlmostEqual(np.linalg.norm(embedding_full.sum(axis=0)),
                                0)
Ejemplo n.º 3
0
 def test_noreg(self):
     adjacency = test_graph_disconnect()
     n = adjacency.shape[0]
     spectral = Spectral(regularization=None, equalize=True)
     with self.assertRaises(ValueError):
         spectral.fit(adjacency)
     spectral = Spectral(regularization=0.)
     spectral.fit(adjacency)
     spectral.predict(np.random.rand(n))
Ejemplo n.º 4
0
    def test_regular(self):
        # regular Laplacian
        spectral = Spectral(self.k,
                            normalized_laplacian=False,
                            barycenter=False,
                            normalized=False)
        embedding = spectral.fit_transform(self.adjacency)
        self.assertAlmostEqual(np.linalg.norm(embedding.mean(axis=0)), 0)
        error = np.abs(spectral.predict(self.adjacency[1]) -
                       embedding[1]).sum()
        self.assertAlmostEqual(error, 0)

        with self.assertRaises(ValueError):
            spectral = Spectral(self.k,
                                normalized_laplacian=False,
                                regularization=0,
                                equalize=True)
            spectral.fit(test_bigraph())
            spectral.fit(test_digraph())
            spectral.fit(test_graph_disconnect())

        with self.assertWarns(Warning):
            n = self.k - 1
            spectral.fit_transform(np.ones((n, n)))
    def test_spectral_basic(self):
        # Spectral with lanczos solver
        spectral = Spectral(2, normalized_laplacian=False, scaling=None, solver='lanczos')
        spectral.fit(self.adjacency)
        self.assertTrue(has_proper_shape(self.adjacency, spectral))
        self.assertTrue(min(spectral.eigenvalues_ >= -1) and max(spectral.eigenvalues_ <= 1))

        # test if the embedding is centered
        # without regularization
        spectral = Spectral(2, normalized_laplacian=False, scaling=None, solver='lanczos', regularization=0)
        spectral.fit(self.house)
        self.assertAlmostEqual(barycenter_norm(self.house, spectral), 0)

        # with regularization
        spectral.regularization = 0.1
        spectral.fit(self.house)
        self.assertAlmostEqual(barycenter_norm(self.house, spectral), 0)
Ejemplo n.º 6
0
 def test_equalize(self):
     spectral = Spectral(self.k, equalize=True)
     spectral.fit(self.adjacency)
     spectral.predict(np.ones(self.n))
Ejemplo n.º 7
0
 def test_no_scaling(self):
     spectral = Spectral(self.k, scaling=0)
     spectral.fit(self.adjacency)
     spectral.predict(np.ones(self.n))
    def test_spectral_normalized(self):
        # Spectral with lanczos solver
        spectral = Spectral(2, normalized_laplacian=True, solver='lanczos')
        spectral.fit(self.adjacency)
        self.assertTrue(has_proper_shape(self.adjacency, spectral))
        self.assertTrue(min(spectral.eigenvalues_ >= -1e-6) and max(spectral.eigenvalues_ <= 2))

        # test if the embedding is centered
        # without regularization
        spectral.regularization = None
        spectral.fit(self.house)
        self.assertAlmostEqual(barycenter_norm(self.house, spectral), 0)

        # with regularization
        spectral.regularization = 0.1
        spectral.fit(self.house)
        self.assertAlmostEqual(barycenter_norm(self.house, spectral), 0, places=2)

        # Spectral with halko solver
        spectral = Spectral(2, normalized_laplacian=True, solver='halko')
        spectral.fit(self.adjacency)
        self.assertTrue(has_proper_shape(self.adjacency, spectral))
        self.assertTrue(min(spectral.eigenvalues_ >= -1e-6) and max(spectral.eigenvalues_ <= 2))

        # test if the embedding is centered
        # without regularization
        spectral.regularization = None
        spectral.fit(self.house)
        self.assertAlmostEqual(barycenter_norm(self.house, spectral), 0)

        # with regularization
        spectral.regularization = 0.1
        spectral.fit(self.house)
        self.assertAlmostEqual(barycenter_norm(self.house, spectral), 0, places=2)
 def test_spectral_divide_scaling(self):
     spectral = Spectral(2, scaling='divide')
     spectral.regularization = None
     spectral.fit(self.house)
     self.assertAlmostEqual(barycenter_norm(self.house, spectral), 0, 7)