def testNormalisedLaplacianSym(self): numVertices = 10 numFeatures = 0 vList = VertexList(numVertices, numFeatures) graph = SparseGraph(vList) ell = 2 m = 2 generator = BarabasiAlbertGenerator(ell, m) graph = generator.generate(graph) k = 10 W = graph.getSparseWeightMatrix() L = GraphUtils.shiftLaplacian(W) L2 = graph.normalisedLaplacianSym() tol = 10**-6 self.assertTrue( numpy.linalg.norm(L + L2 - 2 * numpy.eye(numVertices)) < tol) #Test zero rows/cols W = scipy.sparse.csr_matrix((5, 5)) W[1, 0] = 1 W[0, 1] = 1 L = GraphUtils.normalisedLaplacianSym(W) for i in range(2, 5): self.assertEquals(L[i, i], 0)
def testNormalisedLaplacianSym(self): numVertices = 10 numFeatures = 0 vList = VertexList(numVertices, numFeatures) graph = SparseGraph(vList) ell = 2 m = 2 generator = BarabasiAlbertGenerator(ell, m) graph = generator.generate(graph) k = 10 W = graph.getSparseWeightMatrix() L = GraphUtils.shiftLaplacian(W) L2 = graph.normalisedLaplacianSym() tol = 10**-6 self.assertTrue(numpy.linalg.norm(L + L2 - 2*numpy.eye(numVertices)) < tol) #Test zero rows/cols W = scipy.sparse.csr_matrix((5, 5)) W[1, 0] = 1 W[0, 1] = 1 L = GraphUtils.normalisedLaplacianSym(W) for i in range(2, 5): self.assertEquals(L[i, i], 0)
def testShiftLaplacian(self): numVertices = 10 numFeatures = 0 vList = VertexList(numVertices, numFeatures) graph = SparseGraph(vList) ell = 2 m = 2 generator = BarabasiAlbertGenerator(ell, m) graph = generator.generate(graph) k = 10 W = graph.getSparseWeightMatrix() L = GraphUtils.shiftLaplacian(W) L2 = 2*numpy.eye(numVertices) - graph.normalisedLaplacianSym() tol = 10**-6 self.assertTrue(numpy.linalg.norm(L - L2) < tol)
def testShiftLaplacian(self): numVertices = 10 numFeatures = 0 vList = VertexList(numVertices, numFeatures) graph = SparseGraph(vList) ell = 2 m = 2 generator = BarabasiAlbertGenerator(ell, m) graph = generator.generate(graph) k = 10 W = graph.getSparseWeightMatrix() L = GraphUtils.shiftLaplacian(W) L2 = 2 * numpy.eye(numVertices) - graph.normalisedLaplacianSym() tol = 10**-6 self.assertTrue(numpy.linalg.norm(L - L2) < tol)
cluster1 = numpy.array([[0,1], [0,2], [1,3], [2,3], [3,4], [4,5]]) graph1.addEdges(cluster1) cluster2 = numpy.array([[5,7], [5,8], [7,9], [8,9], [6,7]]) graph1.addEdges(cluster2) cluster3 = numpy.array([[6,10], [10,11], [10,12], [11,12], [11,13], [12,14]]) graph1.addEdges(cluster3) graph2 = SparseGraph(GeneralVertexList(numVertices)) cluster1 = numpy.array([[0,1], [0,2], [1,3], [2,3], [3,4], [0,3],[4,5]]) graph2.addEdges(cluster1) cluster2 = numpy.array([[5,7], [5,8], [7,9], [8,9], [6,7]]) graph2.addEdges(cluster2) cluster3 = numpy.array([[6,10], [10,11], [10,12], [11,12], [11,13], [12,14]]) graph2.addEdges(cluster3) L1 = graph1.normalisedLaplacianSym() L2 = graph2.normalisedLaplacianSym() l1, U = numpy.linalg.eig(L1) inds = numpy.argsort(l1)[0:k] U = U[:, inds] U = Standardiser().normaliseArray(U.T).T l2, V = numpy.linalg.eig(L2) inds = numpy.argsort(l2)[0:k] V = V[:, inds] V = Standardiser().normaliseArray(V.T).T kmeans = KMeans(k) kmeans.fit(U) C = FeatureGenerator().categoricalToIndicator(numpy.array([kmeans.labels_]).T, numpy.array([0]))
from apgl.graph.SparseGraph import SparseGraph from apgl.graph.GeneralVertexList import GeneralVertexList numpy.set_printoptions(suppress=True, precision=4, linewidth=100) numpy.random.seed(21) p = 0.001 numVertices = 10000 generator = ErdosRenyiGenerator(p) graph = SparseGraph(GeneralVertexList(numVertices)) graph = generator.generate(graph) print("Num vertices = " + str(graph.getNumVertices())) print("Num edges = " + str(graph.getNumEdges())) L = graph.normalisedLaplacianSym(sparse=True) # L = csr_matrix(L) print("Created Laplacian") # ml = smoothed_aggregation_solver(L) # M = ml.aspreconditioner() start = time.clock() w, V = scipy.sparse.linalg.eigsh(L, k=20) totalTime = time.clock() - start print(totalTime) # print(w) # Eigsh is quite fast
from apgl.generator.ErdosRenyiGenerator import ErdosRenyiGenerator from apgl.graph.SparseGraph import SparseGraph from apgl.graph.GeneralVertexList import GeneralVertexList numpy.set_printoptions(suppress=True, precision=4, linewidth=100) numpy.random.seed(21) p = 0.001 numVertices = 10000 generator = ErdosRenyiGenerator(p) graph = SparseGraph(GeneralVertexList(numVertices)) graph = generator.generate(graph) print("Num vertices = " + str(graph.getNumVertices())) print("Num edges = " + str(graph.getNumEdges())) L = graph.normalisedLaplacianSym(sparse=True) #L = csr_matrix(L) print("Created Laplacian") #ml = smoothed_aggregation_solver(L) #M = ml.aspreconditioner() start = time.clock() w, V = scipy.sparse.linalg.eigsh(L, k=20) totalTime = time.clock() - start print(totalTime) #print(w) #Eigsh is quite fast