Can import edgelist to igraph python

懵懂的女人 提交于 2021-01-27 06:32:34

问题


I have a list of twitter followers in text file that I want to import to iGraph.

Here's the sample of my list

393795446 18215973
393795446 582203919
393795446 190709835
393795446 1093090866
393795446 157780872
393795446 1580109739
393795446 3301748909
393795446 1536791610
393795446 106170345
393795446 9409752

And this is how I import it

from igraph import *
twitter_igraph = Graph.Read_Edgelist('twitter_edgelist.txt', directed=True)

But I get this error.

---------------------------------------------------------------------------
InternalError                             Traceback (most recent call last)
<ipython-input-10-d808f2237fa8> in <module>()
----> 1 twitter_igraph = Graph.Read_Edgelist('twitter_edgelist.txt', directed=True)

InternalError: Error at type_indexededgelist.c:369: cannot add negative number of vertices, Invalid value

I'm not sure why it's saying something about negative number. I check the file and it doesn't have any negative number or id.


回答1:


You need to use graph.Read_Ncol for this type of file format. Why your file doesn't conform to a typical "edgelist" format is beyond me. I've wondered this myself many times. I should also mention that I grabbed the answer from here. Tamàs seems to be the main igraph guy around here. I'm sure he can give a more detailed reason as to why you need to use Ncol as opposed to Edgelist.

This works for me.

from igraph import *
twitter_igraph = Graph.Read_Ncol('twitter_edgelist.txt', directed=True)

Personal Plug

This is a great example of where igraph's documentation could be improved.

For example: The only accompanying text with graph.Read_Edgelist() doc says...

Reads an edge list from a file and creates a graph based on it. Please note that the vertex indices are zero-based.

This doesn't really tell me anything when obviously there are nuances with how the file needs to be formatted. Saying what format this function expects the file to be in would save a lot of people their sanity.



来源:https://stackoverflow.com/questions/32513650/can-import-edgelist-to-igraph-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!