class ThreeClustIterator(object): 
    def __init__(self, p=0.1, numClusters=3, seed=21): 
        numpy.random.seed(seed)
        self.numClusters = numClusters
        self.startClusterSize = 20
        self.endClusterSize = 60
        self.clusterStep = 5
        self.pClust = 0.3
                
        self.numVertices = self.numClusters*self.endClusterSize
        vList = GeneralVertexList(self.numVertices)
        
        subgraphIndicesList = [range(0, self.startClusterSize)]
        subgraphIndicesList[0].extend(range(self.endClusterSize, self.endClusterSize+self.startClusterSize))
        subgraphIndicesList[0].extend(range(2*self.endClusterSize, 2*self.endClusterSize+self.startClusterSize))
        
        for i in range(self.startClusterSize, self.endClusterSize-self.clusterStep+1, self.clusterStep):
            subgraphIndices = copy.copy(subgraphIndicesList[-1])
            subgraphIndices.extend(range(i, i+self.clusterStep))
            subgraphIndices.extend(range(self.endClusterSize+i, self.endClusterSize+i+self.clusterStep))
            subgraphIndices.extend(range(2*self.endClusterSize+i, 2*self.endClusterSize+i+self.clusterStep))
            subgraphIndicesList.append(subgraphIndices)
        
        # to test removing
        # - increasing graph
        # do nothing
        # - decreasing graph
        #subgraphIndicesList.reverse()
        # - increasing and decreasing graph
        tmp = copy.copy(subgraphIndicesList[:-1])
        tmp.reverse()
        subgraphIndicesList.extend(tmp)
        self.subgraphIndicesList = subgraphIndicesList
        self.p = p 
        
        W = numpy.ones((self.numVertices, self.numVertices))*self.p
        
        for i in range(numClusters):
            W[self.endClusterSize*i:self.endClusterSize*(i+1), self.endClusterSize*i:self.endClusterSize*(i+1)] = self.pClust
            
        P = numpy.random.rand(self.numVertices, self.numVertices)
        W = numpy.array(P < W, numpy.float)
        upTriInds = numpy.triu_indices(self.numVertices)
        W[upTriInds] = 0
        W = W + W.T
        self.graph = SparseGraph(vList)
        self.graph.setWeightMatrix(W)
        
    def getIterator(self):
        return IncreasingSubgraphListIterator(self.graph, self.subgraphIndicesList)
Beispiel #2
0
def run(): 
    W = createDataset()
    
    numVertices = W.shape[0]
    graph = SparseGraph(GeneralVertexList(numVertices))
    graph.setWeightMatrix(W)
    L = graph.normalisedLaplacianSym()
    #L = GraphUtils.shiftLaplacian(scipy.sparse.csr_matrix(W)).todense()
    n = 100 
    omega, Q = numpy.linalg.eigh(L)
    omega2, Q2 = Nystrom.eigpsd(L, n)
    
    print(omega)
    print(omega2)
    
    plt.figure(1)
    plt.plot(numpy.arange(omega.shape[0]), omega)
    plt.plot(numpy.arange(omega2.shape[0]), omega2)
    plt.show()

#run()
Beispiel #3
0
#There is no eigengap in this case so bound does poorly 
W = scipy.sparse.csr_matrix(createDataset(sigma=1.5))
nystromNs = numpy.arange(20, 151, 10) 
k = 2

errors = numpy.zeros((len(nystromNs), numMethods))  
innerProds = numpy.zeros((len(nystromNs), numMethods))  

L = GraphUtils.shiftLaplacian(W)
L2 = GraphUtils.normalisedLaplacianSym(W)

print(L2.todense())

#Find connected components 
graph = SparseGraph(GeneralVertexList(W.shape[0]))
graph.setWeightMatrix(W)
components = graph.findConnectedComponents()
print(len(components)) 

#Compute exact eigenvalues 
omega, Q = numpy.linalg.eigh(L.todense())
inds = numpy.flipud(numpy.argsort(omega)) 
omega, Q = omega[inds], Q[:, inds]
omegak, Qk = omega[0:k], Q[:, 0:k]    

print(omega)
print(Q)

omegaHat, Qhat = numpy.linalg.eigh(L2.todense())
inds = numpy.argsort(omegaHat)
omegaHat, Qhat = omegaHat[inds], Qhat[:, inds]