本文整理汇总了Python中apgl.graph.SparseGraph.SparseGraph.getWeightMatrix方法的典型用法代码示例。如果您正苦于以下问题:Python SparseGraph.getWeightMatrix方法的具体用法?Python SparseGraph.getWeightMatrix怎么用?Python SparseGraph.getWeightMatrix使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类apgl.graph.SparseGraph.SparseGraph
的用法示例。
在下文中一共展示了SparseGraph.getWeightMatrix方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testKwayNormalisedCut
# 需要导入模块: from apgl.graph.SparseGraph import SparseGraph [as 别名]
# 或者: from apgl.graph.SparseGraph.SparseGraph import getWeightMatrix [as 别名]
def testKwayNormalisedCut(self):
numVertices = 6
graph = SparseGraph(GeneralVertexList(numVertices))
graph.addEdge(0, 1)
graph.addEdge(0, 2)
graph.addEdge(2, 1)
graph.addEdge(3, 4)
graph.addEdge(3, 5)
graph.addEdge(5, 4)
W = graph.getWeightMatrix()
clustering = numpy.array([0,0,0, 1,1,1])
self.assertEquals(GraphUtils.kwayNormalisedCut(W, clustering), 0.0)
#Try sparse W
Ws = scipy.sparse.csr_matrix(W)
self.assertEquals(GraphUtils.kwayNormalisedCut(Ws, clustering), 0.0)
graph.addEdge(2, 3)
W = graph.getWeightMatrix()
self.assertEquals(GraphUtils.kwayNormalisedCut(W, clustering), 1.0/7)
Ws = scipy.sparse.csr_matrix(W)
self.assertEquals(GraphUtils.kwayNormalisedCut(Ws, clustering), 1.0/7)
clustering = numpy.array([0,0,0, 1,1,2])
self.assertEquals(GraphUtils.kwayNormalisedCut(W, clustering), 61.0/105)
self.assertEquals(GraphUtils.kwayNormalisedCut(Ws, clustering), 61.0/105)
#Test two vertices without any edges
W = numpy.zeros((2, 2))
clustering = numpy.array([0, 1])
self.assertEquals(GraphUtils.kwayNormalisedCut(W, clustering), 0.0)
Ws = scipy.sparse.csr_matrix(W)
self.assertEquals(GraphUtils.kwayNormalisedCut(Ws, clustering), 0.0)
示例2: testModularity
# 需要导入模块: from apgl.graph.SparseGraph import SparseGraph [as 别名]
# 或者: from apgl.graph.SparseGraph.SparseGraph import getWeightMatrix [as 别名]
def testModularity(self):
numVertices = 6
graph = SparseGraph(GeneralVertexList(numVertices))
graph.addEdge(0,0)
graph.addEdge(1,1)
graph.addEdge(2,2)
graph.addEdge(0,1)
graph.addEdge(0,2)
graph.addEdge(2,1)
graph.addEdge(3,4,2)
graph.addEdge(3,5,2)
graph.addEdge(4,5,2)
graph.addEdge(3,3,2)
graph.addEdge(4,4,2)
graph.addEdge(5,5,2)
W = graph.getWeightMatrix()
clustering = numpy.array([0,0,0,1,1,1])
#This is the same as the igraph result
Q = GraphUtils.modularity(W, clustering)
self.assertEquals(Q, 4.0/9.0)
Ws = scipy.sparse.csr_matrix(W)
Q = GraphUtils.modularity(Ws, clustering)
self.assertEquals(Q, 4.0/9.0)
W = numpy.ones((numVertices, numVertices))
Q = GraphUtils.modularity(W, clustering)
self.assertEquals(Q, 0.0)
Ws = scipy.sparse.csr_matrix(W)
Q = GraphUtils.modularity(Ws, clustering)
self.assertEquals(Q, 0.0)
示例3: GraphMatchTest
# 需要导入模块: from apgl.graph.SparseGraph import SparseGraph [as 别名]
# 或者: from apgl.graph.SparseGraph.SparseGraph import getWeightMatrix [as 别名]
class GraphMatchTest(unittest.TestCase):
def setUp(self):
numpy.set_printoptions(suppress=True, precision=3)
numpy.random.seed(21)
numpy.set_printoptions(threshold=numpy.nan, linewidth=100)
#Use the example in the document
self.numVertices = 10
self.numFeatures = 2
self.graph1 = SparseGraph(VertexList(self.numVertices, self.numFeatures))
self.graph1.setVertices(range(self.numVertices), numpy.random.rand(self.numVertices, self.numFeatures))
edges = numpy.array([[0,1], [0, 2], [0,4], [0,5], [0,8], [0,9]])
self.graph1.addEdges(edges)
edges = numpy.array([[1,3], [1, 5], [1,6], [1,8], [2,9], [3,4], [3,5], [3,6], [3,7], [3,8], [3,9]])
self.graph1.addEdges(edges)
edges = numpy.array([[4,2], [4, 7], [4,9], [5,8], [6, 7]])
self.graph1.addEdges(edges)
self.graph2 = SparseGraph(VertexList(self.numVertices, self.numFeatures))
self.graph2.setVertices(range(self.numVertices), numpy.random.rand(self.numVertices, self.numFeatures))
edges = numpy.array([[0,3], [0, 4], [0,5], [0,8], [0,9], [1,2]])
self.graph2.addEdges(edges)
edges = numpy.array([[1,3], [1,5], [1, 7], [1,8], [1,9], [2,3], [2,5], [3,5], [4,5], [4,6]])
self.graph2.addEdges(edges)
edges = numpy.array([[4,9], [6, 8], [7,8], [7,9], [8, 9]])
self.graph2.addEdges(edges)
def testMatch(self):
matcher = GraphMatch(algorithm="U", alpha=0.3)
permutation, distance, time = matcher.match(self.graph1, self.graph2)
#Checked output file - seems correct
distance2 = GraphMatch(alpha=0.0).distance(self.graph1, self.graph2, permutation)
self.assertAlmostEquals(distance[0], distance2)
#Now test case in which alpha is different
matcher = GraphMatch(algorithm="U", alpha=0.5)
permutation, distance, time = matcher.match(self.graph1, self.graph2)
distance2 = GraphMatch(alpha=0.0).distance(self.graph1, self.graph2, permutation)
self.assertAlmostEquals(distance[0], distance2)
#Test normalised distance
alpha = 0.0
permutation, distance, time = GraphMatch(algorithm="U", alpha=alpha).match(self.graph1, self.graph2)
distance2 = GraphMatch(alpha=alpha).distance(self.graph1, self.graph2, permutation, True)
self.assertAlmostEquals(distance[1], distance2)
alpha = 1.0
permutation, distance, time = GraphMatch(algorithm="U", alpha=alpha).match(self.graph1, self.graph2)
distance2 = GraphMatch(alpha=alpha).distance(self.graph1, self.graph2, permutation, True)
self.assertAlmostEquals(distance[1], distance2, 5)
#Test empty graph
alpha = 0.0
graph1 = SparseGraph(VertexList(0, 0))
graph2 = SparseGraph(VertexList(0, 0))
permutation, distance, time = GraphMatch(algorithm="U", alpha=alpha).match(graph1, graph2)
nptst.assert_array_equal(permutation, numpy.array([], numpy.int))
self.assertEquals(distance, [0, 0, 0])
#Test where 1 graph is empty
permutation, distance, time = GraphMatch(algorithm="U", alpha=alpha).match(graph1, self.graph1)
self.assertEquals(numpy.linalg.norm(self.graph1.getWeightMatrix())**2, distance[0])
self.assertEquals(distance[1], 1)
self.assertEquals(distance[2], 1)
permutation, distance, time = GraphMatch(algorithm="U", alpha=alpha).match(self.graph1, graph1)
self.assertEquals(numpy.linalg.norm(self.graph1.getWeightMatrix())**2, distance[0])
self.assertEquals(distance[1], 1)
self.assertEquals(distance[2], 1)
alpha = 1.0
permutation, distance, time = GraphMatch(algorithm="U", alpha=alpha).match(graph1, self.graph1)
self.assertEquals(numpy.linalg.norm(self.graph1.getWeightMatrix())**2, distance[0])
V2 = self.graph1.vlist.getVertices()
V1 = numpy.zeros(V2.shape)
C = GraphMatch(algorithm="U", alpha=alpha).matrixSimilarity(V1, V2)
dist = numpy.trace(C)/numpy.linalg.norm(C)
self.assertAlmostEquals(distance[1], -dist, 4)
self.assertAlmostEquals(distance[2], -dist, 4)
permutation, distance, time = GraphMatch(algorithm="U", alpha=alpha).match(self.graph1, graph1)
self.assertEquals(numpy.linalg.norm(self.graph1.getWeightMatrix())**2, distance[0])
self.assertAlmostEquals(distance[1], -dist, 4)
self.assertAlmostEquals(distance[2], -dist, 4)
#Test one graph which is a subgraph of another
p = 0.2
k = 10
numVertices = 20
generator = SmallWorldGenerator(p, k)
graph = SparseGraph(VertexList(numVertices, 2))
graph = generator.generate(graph)
#.........这里部分代码省略.........
示例4: testVectorStatistics
# 需要导入模块: from apgl.graph.SparseGraph import SparseGraph [as 别名]
# 或者: from apgl.graph.SparseGraph.SparseGraph import getWeightMatrix [as 别名]
def testVectorStatistics(self):
numFeatures = 1
numVertices = 10
vList = VertexList(numVertices, numFeatures)
graph = SparseGraph(vList)
graph.addEdge(0, 2)
graph.addEdge(0, 1)
growthStatistics = GraphStatistics()
statsDict = growthStatistics.vectorStatistics(graph)
self.assertTrue((statsDict["outDegreeDist"] == numpy.array([7,2,1])).all())
self.assertTrue((statsDict["inDegreeDist"] == numpy.array([7,2,1])).all())
self.assertTrue((statsDict["hopCount"] == numpy.array([10,14,16])).all())
self.assertTrue((statsDict["triangleDist"] == numpy.array([10])).all())
W = graph.getWeightMatrix()
W = (W + W.T)/2
lmbda, V = numpy.linalg.eig(W)
maxEigVector = V[:, numpy.argmax(lmbda)]
lmbda = numpy.flipud(numpy.sort(lmbda[lmbda>0]))
self.assertTrue((statsDict["maxEigVector"] == maxEigVector).all())
self.assertTrue((statsDict["eigenDist"] == lmbda).all())
self.assertTrue((statsDict["componentsDist"] == numpy.array([0, 7, 0, 1])).all())
graph.addEdge(0, 3)
graph.addEdge(0, 4)
graph.addEdge(1, 4)
growthStatistics = GraphStatistics()
statsDict = growthStatistics.vectorStatistics(graph)
self.assertTrue((statsDict["outDegreeDist"] == numpy.array([5,2,2,0,1])).all())
self.assertTrue((statsDict["inDegreeDist"] == numpy.array([5,2,2,0,1])).all())
self.assertTrue((statsDict["hopCount"] == numpy.array([10,20,30])).all())
self.assertTrue((statsDict["triangleDist"] == numpy.array([7, 0, 3])).all())
W = graph.getWeightMatrix()
W = (W + W.T)/2
lmbda, V = numpy.linalg.eig(W)
maxEigVector = V[:, numpy.argmax(lmbda)]
lmbda = numpy.flipud(numpy.sort(lmbda[lmbda>0]))
self.assertTrue((statsDict["maxEigVector"] == maxEigVector).all())
self.assertTrue((statsDict["eigenDist"] == lmbda).all())
self.assertTrue((statsDict["componentsDist"] == numpy.array([0, 5, 0, 0, 0, 1])).all())
#Test on a directed graph and generating tree statistics
vList = VertexList(numVertices, numFeatures)
graph = SparseGraph(vList, False)
graph.addEdge(0, 1)
graph.addEdge(0, 2)
graph.addEdge(2, 3)
graph.addEdge(4, 5)
statsDict = growthStatistics.vectorStatistics(graph, treeStats=True)
self.assertTrue(( statsDict["inDegreeDist"] == numpy.array([6, 4]) ).all())
self.assertTrue(( statsDict["outDegreeDist"] == numpy.array([7, 2, 1]) ).all())
self.assertTrue(( statsDict["triangleDist"] == numpy.array([10]) ).all())
self.assertTrue(( statsDict["treeSizesDist"] == numpy.array([0, 4, 1, 0, 1]) ).all())
self.assertTrue(( statsDict["treeDepthsDist"] == numpy.array([4, 1, 1]) ).all())
示例5: PermutationGraphKernelTest
# 需要导入模块: from apgl.graph.SparseGraph import SparseGraph [as 别名]
# 或者: from apgl.graph.SparseGraph.SparseGraph import getWeightMatrix [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()))))
#.........这里部分代码省略.........