本文整理汇总了Python中igraph.Graph.to_undirected方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.to_undirected方法的具体用法?Python Graph.to_undirected怎么用?Python Graph.to_undirected使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类igraph.Graph
的用法示例。
在下文中一共展示了Graph.to_undirected方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Physic
# 需要导入模块: from igraph import Graph [as 别名]
# 或者: from igraph.Graph import to_undirected [as 别名]
class Physic (BaseInputGraph):
def __init__(self):
'''
@return: Arxiv ASTRO-PH (Astro Physics) collaboration network as iGraph graph instance
'''
edges = []
weights = []
f = open("./physic/compact-physic.txt", "r")
for line in f:
if line and line[0]!='#':
seg = line.split()
edges.append( (int(seg[0]), int(seg[1])) )
weights.append( 1 )
maxid = max( edges, key=itemgetter(1) )[1]
maxid = max( maxid, max(edges,key=itemgetter(0))[0] )
self.g = Graph()
self.g.add_vertices(maxid + 1)
self.g.add_edges(edges)
self.g.to_undirected()
self.g.simplify()
self.g.vs["myID"] = [ str(int(i)) for i in range(maxid+1)]
print "#nodes=", maxid + 1
print "#edges=", len(self.g.es)
def run(self):
C = BaseInputGraph.unsupervised_logexpand(self)
BaseInputGraph.run(self, C, p0=np.array([0.04, 0.04]))
with open("./physic/Physic_weights.pairs", "w+") as txt:
for e in self.g.es:
txt.write("%d %d %f\n" %(e.tuple[0], e.tuple[1], e["weight"]) )
示例2: Football
# 需要导入模块: from igraph import Graph [as 别名]
# 或者: from igraph.Graph import to_undirected [as 别名]
class Football ( BaseInputGraph ):
def __init__(self):
'''
@return: American Colleage Football Network as iGraph graph instance
'''
edges = []
weights = []
f = open("./football/footballTSEinputEL.dat", "r")
for line in f:
seg = line.split()
edges.append( (int(seg[0]), int(seg[1])) )
weights.append( 1 )
maxid = max( edges, key=itemgetter(1))[1]
maxid = max( maxid, max(edges,key=itemgetter(0))[0] )
self.g = Graph()
self.g.add_vertices(maxid + 1)
self.g.add_edges(edges)
self.g.to_undirected()
conf = []
with open("./football/footballTSEinputConference.clu", "r") as fconf:
conf = (fconf.read()).split()
self.g.vs["comm"] = [x for x in conf]
self.g.vs["myID"] = [ str(int(i)) for i in range(maxid+1)]
print "#nodes=", maxid + 1
print "#edges=", len(self.g.es)
def run(self):
AMI_increase = [[], [], [], [], [], []]
for igroup in range(10):
#semi-supervised
#group = [i for i, conf in enumerate(self.g.vs["comm"]) if conf == str(igroup)]
#if len(group) < 3: continue
#C = BaseInputGraph.get_C(self, group)
#unsupervised
C = BaseInputGraph.unsupervised_logexpand(self)
BaseInputGraph.run(self, C)
AMI_increase[0] += BaseInputGraph.results(self, Graph.community_fastgreedy, hasgnc = True)
AMI_increase[1] += BaseInputGraph.results(self, Graph.community_label_propagation, hasgnc = True)
AMI_increase[2] += BaseInputGraph.results(self, Graph.community_leading_eigenvector, hasgnc = True)
AMI_increase[3] += BaseInputGraph.results(self, Graph.community_walktrap, hasgnc = True)
AMI_increase[4] += BaseInputGraph.results(self, Graph.community_edge_betweenness, hasgnc = True)
AMI_increase[5] += BaseInputGraph.results(self, Graph.community_multilevel, hasgnc = True)
for i in range(6):
print "& %.5f" % ( 1.0 * sum(AMI_increase[i]) / len(AMI_increase[i]) )
示例3: LFR
# 需要导入模块: from igraph import Graph [as 别名]
# 或者: from igraph.Graph import to_undirected [as 别名]
class LFR ( BaseInputGraph ):
def __init__(self, trialval=1):
ws = []
edges = []
self.trial = trialval
with open("./binary_networks/mu0.5/network%d.dat" % self.trial, "r") as txt:
for line in txt:
seg = line.split()
edges.append((int(seg[0]), int(seg[1])))
ws.append(1)
maxid = max( edges, key=itemgetter(1))[1]
maxid = max( maxid, max(edges,key=itemgetter(0))[0] )
self.g = Graph()
print maxid
self.g.add_vertices(maxid + 1)
with open("./binary_networks/mu0.5/community%d.dat" % self.trial, "r") as txt:
for line in txt:
seg = line.split()
#print seg[0]
self.g.vs[int(seg[0])]["comm"] = seg[1] #note: string is returned
self.g.add_edges(edges)
self.g.to_undirected()
self.g.simplify()
self.g.delete_vertices(0)
self.g.es["weight"] = ws
BaseInputGraph.write_ground_truth(self, "./ground_truth_community%d.groups" % self.trial)
print "#nodes=", maxid + 1
print "#edges=", len(self.g.es)
def run(self):
#supervised
C = []
for i in range(6):
commval = str(random.randint(0,100))
group = [i for i, comm in enumerate(self.g.vs["comm"]) if comm == commval]
C += BaseInputGraph.get_C(self, group)
#unsupervised
C = BaseInputGraph.unsupervised_logexpand(self)
BaseInputGraph.run(self, C, p0=np.array([1 , 1]))
BaseInputGraph.results(self, Graph.community_fastgreedy, hasgnc = False,\
filename="%d" %self.trial)
示例4: Enron
# 需要导入模块: from igraph import Graph [as 别名]
# 或者: from igraph.Graph import to_undirected [as 别名]
class Enron (BaseInputGraph):
def __init__(self):
'''
@return: Enron email communication network as iGraph graph instance
'''
edges = []
weights = []
f = open("./enron/email-Enron.txt", "r")
for line in f:
if line and line[0]!='#':
seg = line.split()
edges.append( (int(seg[0]), int(seg[1])) )
weights.append( 1 )
maxid = max( edges, key=itemgetter(1) )[1]
maxid = max( maxid, max(edges,key=itemgetter(0))[0] )
self.g = Graph()
self.g.add_vertices(maxid + 1)
self.g.add_edges(edges)
self.g.to_undirected()
self.g.simplify()
self.g.vs["myID"] = [ str(int(i)) for i in range(maxid+1)]
print "#nodes=", maxid + 1
print "#edges=", len(self.g.es)
def run(self):
C = BaseInputGraph.unsupervised_logexpand(self)
BaseInputGraph.run(self, C, p0=np.array([0.04, 0.04]))
with open("./enron/email-Enron_weights.pairs", "w+") as txt:
for e in self.g.es:
txt.write("%d %d %f\n" %(e.tuple[0], e.tuple[1], e["weight"]) )
with open("./enron/email-Enron_unweights.pairs", "w+") as txt:
count = 0
for e in self.g.es:
txt.write("%d %d\n" %(e.tuple[0], e.tuple[1]) )
count += 1
print count , "edges written."
示例5: results
# 需要导入模块: from igraph import Graph [as 别名]
# 或者: from igraph.Graph import to_undirected [as 别名]
class Execution:
def results(self, algo, filename):
title = self.__class__.__name__
#=====Community Detection====#
# weighted graph
print "Weighted: %s" % algo
vd = algo(self.g, weights = [ (lambda w: max(w,0.001) )(w) for w in self.g.es["weight"]] )
try:
vc1 = vd.as_clustering()
except:
vc1 = vd #in case a VertexCluster instance is returned
self.write_vertex_clustering(vc1, "_weighted_%s" % filename)
# unweighted graph
print "Un-weighted: %s" % algo
vd = algo(self.g)
try:
vc2 = vd.as_clustering()
except:
vc2 = vd #in case a VertexCluster instance is returned
self.write_vertex_clustering(vc2, "_unweighted_%s" % filename)
#=====Validation====#
if "football" in filename and validate_foot_ball_only:
self.quality(vc1, vc2)
def write_vertex_clustering(self, vc, tag):
title = "community"
with open("./output/" + title + tag + ".group", "w+") as txt:
for cc in range(len(vc)):
if len(vc[cc]) > 1:
txt.write(" ".join([str(self.reverse_id(_)) for _ in vc[cc]]) + "\n")
print "Write to file", "./output/" + title + tag + ".group"
def quality(self, vc1, vc2):
global display
with open("./input/football_raw_data/footballTSEinputConference.clu", "r") as fconf:
conf = (fconf.read()).split()
self.g.vs["comm"] = [x for x in conf]
for index, vc in enumerate([vc1, vc2]):
print "=================="
for cc in range(len(vc)):
for cci in vc[cc]:
self.g.vs[ self.reverse_id(cci) ]["detected"] = str(cc)
ami = metrics.adjusted_mutual_info_score(self.g.vs["detected"], self.g.vs["comm"])
print "AMI", ami
display["AMI"][index].append(ami)
ari = metrics.adjusted_rand_score(self.g.vs["detected"], self.g.vs["comm"])
print "ARI", ari
display["ARI"][index].append(ari)
def compact_id(self, i):
if i in self.ID:
return self.ID[i]
else:
self.ID[i] = self.cap
self.rID[self.cap] = i
self.cap += 1
return self.ID[i]
def reverse_id(self, i):
assert i in self.rID
return self.rID[i]
def __init__(self, filename):
print "Load file", filename
self.cap = 0
self.ID = {}
self.rID = {}
edges = []
weights = []
with open(filename, "r") as txt:
for line in txt:
seg = line.split()
edges.append( (self.compact_id(int(seg[0])), self.compact_id(int(seg[1]))) )
if len(seg) == 3:
weights.append( float(seg[2]) )
else:
weights.append(1)
self.g = Graph()
self.g.add_vertices(self.cap)
self.g.add_edges(edges)
self.g.to_undirected()
self.g.es["weight"] = weights