简体   繁体   中英

Read weighted graph using “igraph” in python

I need read an weighted graph from external file

In other posts is recommended to use ncol, but I tried "ncol" format and not work:

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

Output:

Traceback (most recent call last):
  File "stackoverflow.py", line 54, in <module>
    print vertex["weight"]
KeyError: 'Attribute does not exist'

I tried to read weighted graph from Nexus:

Eg: This is a weighted graph: 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>

But too not work:

g = igraph.Nexus.get("karate")
for vertex in g.vs():
    print vertex["weight"]

Output:

Traceback (most recent call last):
  File "stackoverflow.py", line 54, in <module>
    print vertex["weight"]
KeyError: 'Attribute does not exist'

I dont know how read a weighted graph, someone could help?

You are trying to read the weights of the vertices but in these graphs the weights correspond to the edges :

g = igraph.Nexus.get("karate")
for edge in g.es:
    print edge["weight"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM