def test_bethe_hessian(self): "Bethe Hessian matrix" # fmt: off H = np.array([[4, -2, 0], [-2, 5, -2], [0, -2, 4]]) # fmt: on permutation = [2, 0, 1] # Bethe Hessian gives expected form np.testing.assert_equal( nx.bethe_hessian_matrix(self.P, r=2).todense(), H) # nodelist is correctly implemented np.testing.assert_equal( nx.bethe_hessian_matrix(self.P, r=2, nodelist=permutation).todense(), H[np.ix_(permutation, permutation)], ) # Equal to Laplacian matrix when r=1 np.testing.assert_equal( nx.bethe_hessian_matrix(self.G, r=1).todense(), nx.laplacian_matrix(self.G).todense(), ) # Correct default for the regularizer r np.testing.assert_equal( nx.bethe_hessian_matrix(self.G).todense(), nx.bethe_hessian_matrix(self.G, r=1.25).todense(), )
def bethe_hessian_spectrum(G, r=None): """Returns eigenvalues of the Bethe Hessian matrix of G. Parameters ---------- G : Graph A NetworkX Graph or DiGraph r : float Regularizer parameter Returns ------- evals : NumPy array Eigenvalues See Also -------- bethe_hessian_matrix References ---------- .. [1] A. Saade, F. Krzakala and L. Zdeborová "Spectral clustering of graphs with the bethe hessian", Advances in Neural Information Processing Systems. 2014. """ from scipy.linalg import eigvalsh return eigvalsh(nx.bethe_hessian_matrix(G, r).todense())
def bethe_hessian_spectrum(G, r=None): """Returns eigenvalues of the Bethe Hessian matrix of G. Parameters ---------- G : Graph A NetworkX Graph or DiGraph r : float Regularizer parameter Returns ------- evals : NumPy array Eigenvalues See Also -------- bethe_hessian_matrix References ---------- .. [1] A. Saade, F. Krzakala and L. Zdeborová "Spectral clustering of graphs with the bethe hessian", Advances in Neural Information Processing Systems. 2014. """ from scipy.linalg import eigvalsh return eigvalsh(nx.bethe_hessian_matrix(G, r).todense())
def test_bethe_hessian(self): "Bethe Hessian matrix" H = numpy.array([[ 4, -2, 0], [-2, 5, -2], [ 0, -2, 4]]) permutation = [2, 0, 1] # Bethe Hessian gives expected form assert_equal(nx.bethe_hessian_matrix(self.P, r=2).todense(), H) # nodelist is correctly implemented assert_equal(nx.bethe_hessian_matrix(self.P, r=2, nodelist=permutation).todense(), H[numpy.ix_(permutation, permutation)]) # Equal to Laplacian matrix when r=1 assert_equal(nx.bethe_hessian_matrix(self.G, r=1).todense(), nx.laplacian_matrix(self.G).todense()) # Correct default for the regularizer r assert_equal(nx.bethe_hessian_matrix(self.G).todense(), nx.bethe_hessian_matrix(self.G, r=1.25).todense())