本文整理汇总了Python中apgl.graph.SparseGraph.SparseGraph.addEdges方法的典型用法代码示例。如果您正苦于以下问题:Python SparseGraph.addEdges方法的具体用法?Python SparseGraph.addEdges怎么用?Python SparseGraph.addEdges使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类apgl.graph.SparseGraph.SparseGraph
的用法示例。
在下文中一共展示了SparseGraph.addEdges方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: cvModelSelection
# 需要导入模块: from apgl.graph.SparseGraph import SparseGraph [as 别名]
# 或者: from apgl.graph.SparseGraph.SparseGraph import addEdges [as 别名]
def cvModelSelection(self, graph, paramList, paramFunc, folds, errorFunc):
"""
ParamList is a list of lists of parameters and paramFunc
is a list of the corresponding functions to call with the parameters
as arguments. Note that a parameter can also be a tuple which is expanded
out before the function is called.
e.g.
paramList = [[1, 2], [2, 1], [12, 1]]
paramFunc = [predictor.setC, predictor.setD]
"""
inds = Sampling.crossValidation(folds, graph.getNumEdges())
errors = numpy.zeros((len(paramList), folds))
allEdges = graph.getAllEdges()
for i in range(len(paramList)):
paramSet = paramList[i]
logging.debug("Using paramSet=" + str(paramSet))
for j in range(len(paramSet)):
if type(paramSet[j]) == tuple:
paramFunc[j](*paramSet[j])
else:
paramFunc[j](paramSet[j])
predY = numpy.zeros(0)
y = numpy.zeros(0)
j = 0
for (trainInds, testInds) in inds:
trainEdges = allEdges[trainInds, :]
testEdges = allEdges[testInds, :]
trainGraph = SparseGraph(graph.getVertexList(), graph.isUndirected())
trainGraph.addEdges(trainEdges, graph.getEdgeValues(trainEdges))
testGraph = SparseGraph(graph.getVertexList(), graph.isUndirected())
testGraph.addEdges(testEdges, graph.getEdgeValues(testEdges))
self.learnModel(trainGraph)
predY = self.predictEdges(testGraph, testGraph.getAllEdges())
y = testGraph.getEdgeValues(testGraph.getAllEdges())
#Note that the order the edges is different in testGraphs as
#opposed to graph when calling getAllEdges()
errors[i, j] = errorFunc(y, predY)
j = j+1
logging.info("Error of current fold: " + str(numpy.mean(errors[i, :])))
meanErrors = numpy.mean(errors, 1)
strErrors = numpy.std(errors, 1)
return meanErrors, strErrors
示例2: findInfoDecayGraph
# 需要导入模块: from apgl.graph.SparseGraph import SparseGraph [as 别名]
# 或者: from apgl.graph.SparseGraph.SparseGraph import addEdges [as 别名]
def findInfoDecayGraph(self, egoTestFileName, alterTestFileName, egoIndicesR, alterIndices, egoIndicesNR, alterIndicesNR, egoFileName, alterFileName, missing=0):
(egoTestArray, egoTitles) = self.readFile(egoTestFileName, self.egoTestIds, missing=0)
(alterTestArray, alterTitles) = self.readFile(alterTestFileName, self.alterTestIds, missing=0)
egoMarks = numpy.zeros(egoTestArray.shape[0])
alterMarks = numpy.zeros(alterTestArray.shape[0])
decays = numpy.zeros(egoIndicesR.shape[0])
correctAnswers = numpy.array([1,2,4,5,8,9,10,12])
wrongAnswers = numpy.array([3, 6, 7, 11])
for i in range(egoTestArray.shape[0]):
egoMarks[i] = numpy.intersect1d(egoTestArray[i], correctAnswers).shape[0]
egoMarks[i] += wrongAnswers.shape[0] - numpy.intersect1d(egoTestArray[i], wrongAnswers).shape[0]
for i in range(alterMarks.shape[0]):
alterMarks[i] = numpy.intersect1d(alterTestArray[i], correctAnswers).shape[0]
alterMarks[i] += wrongAnswers.shape[0] - numpy.intersect1d(alterTestArray[i], wrongAnswers).shape[0]
"""
We just return how much the alter understood, since this represents the
decay in understanding of the ego and transmission.
"""
for i in range(decays.shape[0]):
decays[i] = alterMarks[alterIndices[i]]
#A lot of people could not be bothered to fill the questions and hence
#get 4 correct by default
decays = (decays) / float(self.numTestQuestions)
defaultDecay = 10**-6
#Finally, put the data into a graph
(egoArray, egoTitles) = self.readFile(egoFileName, self.egoQuestionIds, missing)
(alterArray, alterTitles) = self.readFile(alterFileName, self.alterQuestionIds, missing)
V = egoArray
V = numpy.r_[V, alterArray[alterIndices, :]]
V = numpy.r_[V, egoArray[alterIndicesNR, :]]
vList = VertexList(V.shape[0], V.shape[1])
vList.setVertices(V)
graph = SparseGraph(vList, False)
edgesR = numpy.c_[egoIndicesR, egoArray.shape[0]+numpy.arange(alterIndices.shape[0])]
graph.addEdges(edgesR, decays)
edgesNR = numpy.c_[egoIndicesNR, egoArray.shape[0]+alterIndices.shape[0]+numpy.arange(alterIndicesNR.shape[0])]
graph.addEdges(edgesNR, numpy.ones(edgesNR.shape[0]) * defaultDecay)
return graph
示例3: GraphMatchTest
# 需要导入模块: from apgl.graph.SparseGraph import SparseGraph [as 别名]
# 或者: from apgl.graph.SparseGraph.SparseGraph import addEdges [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: DictGraph
# 需要导入模块: from apgl.graph.SparseGraph import SparseGraph [as 别名]
# 或者: from apgl.graph.SparseGraph.SparseGraph import addEdges [as 别名]
# -*- coding: utf-8 -*-
from apgl.graph.DictGraph import DictGraph
from apgl.graph.SparseGraph import SparseGraph
from apgl.graph.GeneralVertexList import GeneralVertexList
graph = DictGraph()
graph.addEdge("a", "b")
graph.addEdge("a", "c")
graph.addEdge("a", "d")
edgeIndices = graph.getAllEdgeIndices()
graph2 = SparseGraph(GeneralVertexList(graph.getNumVertices()))
graph2.addEdges(edgeIndices)
示例5: SparseGraph
# 需要导入模块: from apgl.graph.SparseGraph import SparseGraph [as 别名]
# 或者: from apgl.graph.SparseGraph.SparseGraph import addEdges [as 别名]
#!/usr/bin/env python
import numpy as np
from apgl.graph.VertexList import VertexList
from apgl.graph.SparseGraph import SparseGraph
numVertices = 5
numFeatures = 2
graph = SparseGraph(VertexList(numVertices, numFeatures))
# Add some edges to the graph.
# Vertices are indexed starting from 0.
graph[0, 1] = 0.1
graph[1, 2] = 1.0
# Set the label of the 0th vertex to [2, 3].
graph.setVertex(0, np.array([2, 3]))
# Display edge weights.
print(graph[1, 2]) # 1.0
print(graph[1, 3]) # 0.0 (default)
# Add some edges to the graph.
edges = np.array([[0, 1], [1, 2]], np.int)
edgeValues = np.array([0.1, 1.0])
graph.addEdges(edges, edgeValues)
# Display the edge weight between v1 and v2.
print(graph[1, 2])
示例6: SparseGraph
# 需要导入模块: from apgl.graph.SparseGraph import SparseGraph [as 别名]
# 或者: from apgl.graph.SparseGraph.SparseGraph import addEdges [as 别名]
import numpy
import matplotlib.pyplot as plt
from apgl.data.Standardiser import Standardiser
from apgl.data.FeatureGenerator import FeatureGenerator
from apgl.graph.SparseGraph import SparseGraph
from apgl.graph.GeneralVertexList import GeneralVertexList
from sklearn.cluster import KMeans
"""
Test the error bound for the clustering process.
"""
k = 3
numVertices = 15
graph1 = SparseGraph(GeneralVertexList(numVertices))
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()