def testEigWeight(self): tol = 10**-3 n = 100 W = numpy.random.rand(n, n) W = W.dot(W.T) w, U = numpy.linalg.eig(W) W = scipy.sparse.csr_matrix(W) k = 4 m = 5 lmbda, V = EfficientNystrom.eigWeight(W, m, k) MHat = V.dot(numpy.diag(lmbda)).dot(V.T) I = scipy.sparse.eye(n, n) L = GraphUtils.normalisedLaplacianSym(W) M = I - L #print(V) numpy.linalg.norm(M.todense() - MHat) #print(numpy.linalg.norm(M.todense())) #self.assertTrue(numpy.linalg.norm(W - WHat) < tol) #For fixed k, increasing m should improve approximation but not always lastError = 10 for m in range(k+1, n+1, 10): lmbda, V = EfficientNystrom.eigWeight(W, m, k) #print(V) MHat = V.dot(numpy.diag(lmbda)).dot(V.T) error = numpy.linalg.norm(M.todense() - MHat) self.assertTrue(error <= lastError) lastError = error
def testEigWeight(self): tol = 10**-3 n = 100 W = numpy.random.rand(n, n) W = W.dot(W.T) w, U = numpy.linalg.eig(W) W = scipy.sparse.csr_matrix(W) k = 4 m = 5 lmbda, V = EfficientNystrom.eigWeight(W, m, k) MHat = V.dot(numpy.diag(lmbda)).dot(V.T) I = scipy.sparse.eye(n, n) L = GraphUtils.normalisedLaplacianSym(W) M = I - L #print(V) numpy.linalg.norm(M.todense() - MHat) #print(numpy.linalg.norm(M.todense())) #self.assertTrue(numpy.linalg.norm(W - WHat) < tol) #For fixed k, increasing m should improve approximation but not always lastError = 10 for m in range(k + 1, n + 1, 10): lmbda, V = EfficientNystrom.eigWeight(W, m, k) #print(V) MHat = V.dot(numpy.diag(lmbda)).dot(V.T) error = numpy.linalg.norm(M.todense() - MHat) self.assertTrue(error <= lastError) lastError = error
if not os.path.exists(resultsDir): logging.warn("Directory did not exist: " + resultsDir + ", created.") os.makedirs(resultsDir) iterator = getIterator() subgraphIndicesList = [] for W in iterator: logging.debug("Graph size " + str(W.shape[0])) subgraphIndicesList.append(range(W.shape[0])) #Try to find number of clusters at end of sequence by looking at eigengap k = 2 if findEigs: L = GraphUtils.normalisedLaplacianSym(W) logging.debug("Computing eigenvalues") omega, Q = scipy.sparse.linalg.eigsh(L, min(k, L.shape[0]-1), which="SM", ncv = min(20*k, L.shape[0])) omegaDiff = numpy.diff(omega) else: omega = numpy.zeros(k) omegaDiff = numpy.zeros(k-1) #No obvious number of clusters and there are many edges graph = SparseGraph(W.shape[0], W=W) logging.debug("Computing graph statistics") graphStats = GraphStatistics() statsMatrix = graphStats.sequenceScalarStats(graph, subgraphIndicesList, slowStats=False)
import numpy import scipy.sparse from apgl.graph import GraphUtils from sandbox.util.Util import Util numpy.set_printoptions(suppress=True, precision=3) n = 10 W1 = scipy.sparse.rand(n, n, 0.5).todense() W1 = W1.T.dot(W1) W2 = W1.copy() W2[1, 2] = 1 W2[2, 1] = 1 print("W1=" + str(W1)) print("W2=" + str(W2)) L1 = GraphUtils.normalisedLaplacianSym(scipy.sparse.csr_matrix(W1)) L2 = GraphUtils.normalisedLaplacianSym(scipy.sparse.csr_matrix(W2)) deltaL = L2 - L1 print("L1=" + str(L1.todense())) print("L2=" + str(L2.todense())) print("deltaL=" + str(deltaL.todense())) print("rank(deltaL)=" + str(Util.rank(deltaL.todense())))
import numpy import scipy.sparse from apgl.graph import GraphUtils from sandbox.util.Util import Util numpy.set_printoptions(suppress=True, precision=3) n = 10 W1 = scipy.sparse.rand(n, n, 0.5).todense() W1 = W1.T.dot(W1) W2 = W1.copy() W2[1, 2] = 1 W2[2, 1] = 1 print("W1="+str(W1)) print("W2="+str(W2)) L1 = GraphUtils.normalisedLaplacianSym(scipy.sparse.csr_matrix(W1)) L2 = GraphUtils.normalisedLaplacianSym(scipy.sparse.csr_matrix(W2)) deltaL = L2 - L1 print("L1="+str(L1.todense())) print("L2="+str(L2.todense())) print("deltaL="+str(deltaL.todense())) print("rank(deltaL)=" + str(Util.rank(deltaL.todense())))