当前位置: 首页>>代码示例>>Python>>正文


Python SparseGraph.getNumVertices方法代码示例

本文整理汇总了Python中apgl.graph.SparseGraph.SparseGraph.getNumVertices方法的典型用法代码示例。如果您正苦于以下问题:Python SparseGraph.getNumVertices方法的具体用法?Python SparseGraph.getNumVertices怎么用?Python SparseGraph.getNumVertices使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在apgl.graph.SparseGraph.SparseGraph的用法示例。


在下文中一共展示了SparseGraph.getNumVertices方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: readFromFile

# 需要导入模块: from apgl.graph.SparseGraph import SparseGraph [as 别名]
# 或者: from apgl.graph.SparseGraph.SparseGraph import getNumVertices [as 别名]
    def readFromFile(self, fileName):
        X = numpy.loadtxt(fileName, skiprows=1, converters=self.converters)
        vertexIds = numpy.zeros(X.shape[0]*2)
        
        #First, we will map the vertex Ids to a set of numbers
        for i in range(0, X.shape[0]):
            vertexIds[2*i] = X[i, self.vertex1IdIndex]
            vertexIds[2*i+1] = X[i, self.vertex2IdIndex]

        vertexIds = numpy.unique(vertexIds)

        numVertices = vertexIds.shape[0]
        numFeatures = len(self.vertex1Indices)
        
        vList = VertexList(numVertices, numFeatures)
        sGraph = SparseGraph(vList, self.undirected)

        for i in range(0, X.shape[0]):
            vertex1Id = X[i, self.vertex1IdIndex]
            vertex2Id = X[i, self.vertex2IdIndex]
            
            vertex1 = X[i, self.vertex1Indices]
            vertex2 = X[i, self.vertex2Indices]

            vertex1VListId = numpy.nonzero(vertexIds==vertex1Id)[0]
            vertex2VListId = numpy.nonzero(vertexIds==vertex2Id)[0]

            vertex1VListId = int(vertex1VListId)
            vertex2VListId = int(vertex2VListId)
            vList.setVertex(vertex1VListId, vertex1)
            vList.setVertex(vertex2VListId, vertex2)

            sGraph.addEdge(vertex1VListId, vertex2VListId, self.edgeWeight)

        logging.info("Read " + fileName + " with " + str(sGraph.getNumVertices()) + " vertices and " + str(sGraph.getNumEdges()) + " edges")

        return sGraph 
开发者ID:awj223,项目名称:Insight-Data-Engineering-Code-Challenge,代码行数:39,代码来源:CsvGraphReader.py

示例2: testClusterOnIncreasingGraphs

# 需要导入模块: from apgl.graph.SparseGraph import SparseGraph [as 别名]
# 或者: from apgl.graph.SparseGraph.SparseGraph import getNumVertices [as 别名]
    def testClusterOnIncreasingGraphs(self):
        #Create a large graph and try the clustering.
        numClusters = 3
        ClusterSize = 30
        numFeatures = 0
        
        pNoise = 0
        pClust = 1

        numVertices = numClusters*ClusterSize
        vList = GeneralVertexList(numVertices)

        vList = VertexList(numVertices, numFeatures)
        graph = SparseGraph(vList)

#        ell = 2 
#        m = 2 
#        generator = BarabasiAlbertGenerator(ell, m)
#        graph = generator.generate(graph)
        #Generate matrix of probabilities
        W = numpy.ones((numVertices, numVertices))*pNoise
        for i in range(numClusters):
            W[ClusterSize*i:ClusterSize*(i+1), ClusterSize*i:ClusterSize*(i+1)] = pClust
        P = numpy.random.rand(numVertices, numVertices)
        W = numpy.array(P < W, numpy.float)
        upTriInds = numpy.triu_indices(numVertices)
        W[upTriInds] = 0
        W = W + W.T
        graph = SparseGraph(vList)
        graph.setWeightMatrix(W)

        indices = numpy.random.permutation(numVertices)
        subgraphIndicesList = [indices[0:numVertices/2], indices]

        k1 = numClusters
        k2 = 10
        clusterer = IterativeSpectralClustering(k1, k2)
        #Test full computation of eigenvectors
        graphIterator = IncreasingSubgraphListIterator(graph, subgraphIndicesList)
        clustersList = clusterer.clusterFromIterator(graphIterator, False)

        self.assertEquals(len(clustersList), len(subgraphIndicesList))

        for i in range(len(clustersList)):
            clusters = clustersList[i]
            self.assertEquals(len(subgraphIndicesList[i]), len(clusters))
            #print(clusters)

        #Test full computation of eigenvectors with iterator
        graphIterator = IncreasingSubgraphListIterator(graph, subgraphIndicesList)
        clustersList = clusterer.clusterFromIterator(graphIterator, False)

        self.assertEquals(len(clustersList), len(subgraphIndicesList))

        for i in range(len(clustersList)):
            clusters = clustersList[i]
            self.assertEquals(len(subgraphIndicesList[i]), len(clusters))
            #print(clusters)

        #Now test approximation of eigenvectors with iterator
        graphIterator = IncreasingSubgraphListIterator(graph, subgraphIndicesList)
        clustersList2 = clusterer.clusterFromIterator(graphIterator)

        for i in range(len(clustersList2)):
            clusters = clustersList2[i]
            self.assertEquals(len(subgraphIndicesList[i]), len(clusters))
            #print(clusters)

        #Test case where 2 graphs are identical
        subgraphIndicesList = []
        subgraphIndicesList.append(range(graph.getNumVertices()))
        subgraphIndicesList.append(range(graph.getNumVertices()))

        graphIterator = IncreasingSubgraphListIterator(graph, subgraphIndicesList)
        clustersList = clusterer.clusterFromIterator(graphIterator, True)
开发者ID:malcolmreynolds,项目名称:APGL,代码行数:77,代码来源:IterativeSpectralClusteringTest.py

示例3: ErdosRenyiGenerator

# 需要导入模块: from apgl.graph.SparseGraph import SparseGraph [as 别名]
# 或者: from apgl.graph.SparseGraph.SparseGraph import getNumVertices [as 别名]
from scipy.sparse import csr_matrix
from pyamg import smoothed_aggregation_solver
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)
开发者ID:kentwang,项目名称:sandbox,代码行数:32,代码来源:EigTest.py

示例4: PermutationGraphKernelTest

# 需要导入模块: from apgl.graph.SparseGraph import SparseGraph [as 别名]
# 或者: from apgl.graph.SparseGraph.SparseGraph import getNumVertices [as 别名]
class  PermutationGraphKernelTest(unittest.TestCase):
    def setUp(self):
        self.tol = 10**-4
        self.numVertices = 5
        self.numFeatures = 2

        vertexList1 = VertexList(self.numVertices, self.numFeatures)
        vertexList1.setVertex(0, numpy.array([1, 1]))
        vertexList1.setVertex(1, numpy.array([1, 2]))
        vertexList1.setVertex(2, numpy.array([3, 2]))
        vertexList1.setVertex(3, numpy.array([4, 2]))
        vertexList1.setVertex(4, numpy.array([2, 6]))

        vertexList2 = VertexList(self.numVertices, self.numFeatures)
        vertexList2.setVertex(0, numpy.array([1, 3]))
        vertexList2.setVertex(1, numpy.array([7, 2]))
        vertexList2.setVertex(2, numpy.array([3, 22]))
        vertexList2.setVertex(3, numpy.array([54, 2]))
        vertexList2.setVertex(4, numpy.array([2, 34]))

        self.sGraph1 = SparseGraph(vertexList1)
        self.sGraph1.addEdge(0, 1)
        self.sGraph1.addEdge(0, 2)
        self.sGraph1.addEdge(1, 2)
        self.sGraph1.addEdge(2, 3)

        self.sGraph2 = SparseGraph(vertexList2)
        self.sGraph2.addEdge(0, 1)
        self.sGraph2.addEdge(0, 2)
        self.sGraph2.addEdge(1, 2)
        self.sGraph2.addEdge(2, 3)
        self.sGraph2.addEdge(3, 4)

        self.sGraph3 = SparseGraph(vertexList2)
        self.sGraph3.addEdge(4, 1)
        self.sGraph3.addEdge(4, 2)
        self.sGraph3.addEdge(1, 2)
        self.sGraph3.addEdge(1, 0)


    def testEvaluate(self):
        tau = 1.0
        linearKernel = LinearKernel()

        graphKernel = PermutationGraphKernel(tau, linearKernel)

        """
        First tests - if the graphs have identical edges then permutation is identity matrix
        provided that tau = 1. 
        """

        (evaluation, f, P, SW1, SW2, SK1, SK2) = graphKernel.evaluate(self.sGraph1, self.sGraph1, True)
        self.assertTrue(numpy.linalg.norm(P - numpy.eye(self.numVertices)) <= self.tol)

        S1, U = numpy.linalg.eigh(self.sGraph1.getWeightMatrix())
        S2, U = numpy.linalg.eigh(self.sGraph2.getWeightMatrix())

        evaluation2 = numpy.dot(S1, S1)

        self.assertTrue(numpy.linalg.norm(SW1 - S1) <= self.tol)
        self.assertTrue(numpy.linalg.norm(SW2 - S1) <= self.tol)
        self.assertTrue(abs(evaluation - evaluation2) <= self.tol)

        (evaluation, f, P, SW1, SW2, SK1, SK2) = graphKernel.evaluate(self.sGraph2, self.sGraph2, True)
        self.assertTrue(numpy.linalg.norm(P - numpy.eye(self.numVertices)) <= self.tol)

        evaluation2 = numpy.dot(S2, S2)

        self.assertTrue(numpy.linalg.norm(SW1 - S2) <= self.tol)
        self.assertTrue(numpy.linalg.norm(SW2 - S2) <= self.tol)
        self.assertTrue(abs(evaluation - evaluation2) <= self.tol)

        #Test symmetry
        self.assertEquals(graphKernel.evaluate(self.sGraph1, self.sGraph2), graphKernel.evaluate(self.sGraph2, self.sGraph1))

        #Now we choose tau != 1
        tau = 0.5
        graphKernel = PermutationGraphKernel(tau, linearKernel)

        (evaluation, f, P, SW1, SW2, SK1, SK2) = graphKernel.evaluate(self.sGraph1, self.sGraph1, True)
        self.assertTrue(numpy.linalg.norm(P - numpy.eye(self.numVertices)) <= self.tol)

        self.assertTrue(graphKernel.evaluate(self.sGraph1, self.sGraph1) >= 0)
        self.assertTrue(graphKernel.evaluate(self.sGraph2, self.sGraph2) >= 0) 
        self.assertTrue((graphKernel.evaluate(self.sGraph1, self.sGraph2)- graphKernel.evaluate(self.sGraph2, self.sGraph1)) <= self.tol)

        (evaluation, f, P, SW1, SW2, SK1, SK2) = graphKernel.evaluate(self.sGraph1, self.sGraph2, True)

        self.assertTrue(numpy.linalg.norm(numpy.dot(P.T, P) - numpy.eye(self.numVertices)) <= self.tol)

        #Choose tau=0
        tau = 0.0
        graphKernel = PermutationGraphKernel(tau, linearKernel)

        (evaluation, f, P, SW1, SW2, SK1, SK2) = graphKernel.evaluate(self.sGraph1, self.sGraph1, True)
        self.assertTrue(numpy.linalg.norm(P - numpy.eye(self.numVertices)) <= self.tol)
        self.assertTrue(numpy.linalg.norm(numpy.dot(P.T, P) - numpy.eye(self.numVertices)) <= self.tol)

        X1 = self.sGraph1.getVertexList().getVertices(list(range(0, (self.sGraph1.getNumVertices()))))
        X2 = self.sGraph2.getVertexList().getVertices(list(range(0, (self.sGraph2.getNumVertices()))))
#.........这里部分代码省略.........
开发者ID:charanpald,项目名称:sandbox,代码行数:103,代码来源:PermutationGraphKernelTest.py


注:本文中的apgl.graph.SparseGraph.SparseGraph.getNumVertices方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。