在Python中使用“igraph”读取加权图
我需要从外部文件读取一个带权重的图。
在其他帖子中推荐使用ncol格式,但我试过“ncol”格式,结果不行:
g = igraph.Graph.Read_Ncol("small.ncol")
for vertex in g.vs():
print vertex["weight"]
small.ncol
0 1 0.47
2 0 0.67
2 1 0.94
3 0 0.98
3 1 0.05
3 2 0.24
4 0 0.12
4 1 0.22
4 2 0.36
4 3 0.69
5 0 0.82
6 5 0.97
7 5 0.43
7 6 0.83
8 5 0.44
8 6 0.49
8 7 0.39
9 5 0.37
9 6 0.55
9 7 0.73
9 8 0.68
10 0 0.34
11 10 0.22
12 11 0.40
13 12 0.78
14 10 0.59
14 13 0.81
输出:
Traceback (most recent call last):
File "stackoverflow.py", line 54, in <module>
print vertex["weight"]
KeyError: 'Attribute does not exist'
我尝试从Nexus读取带权重的图:
例如:这是一个带权重的图:http://nexus.igraph.org/api/dataset_info?format=xml&id=1
<id>1</id>
<sid>karate</sid>
<tags>
<tag>social network</tag>
<tag>undirected</tag>
**<tag>weighted</tag>**
</tags>
但这也不行:
g = igraph.Nexus.get("karate")
for vertex in g.vs():
print vertex["weight"]
输出:
Traceback (most recent call last):
File "stackoverflow.py", line 54, in <module>
print vertex["weight"]
KeyError: 'Attribute does not exist'
我不知道怎么读取带权重的图,有人能帮忙吗?
1 个回答
3
你想要读取图中顶点的权重,但在这些图里,权重其实是对应于边的:
g = igraph.Nexus.get("karate")
for edge in g.es:
print edge["weight"]