可以将 edgelist 导入 igraph python

Can import edgelist to igraph python

我在文本文件中有一个 Twitter 关注者列表,我想将其导入 iGraph。

这是我的列表示例

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

这就是我导入它的方式

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

但是我收到了这个错误。

---------------------------------------------------------------------------
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

我不确定为什么要说负数。我检查了文件,它没有任何负数或 ID。

对于这种文件格式,您需要使用 graph.Read_Ncol。为什么你的文件不符合典型的 "edgelist" 格式是超出我的范围。我自己也想过很多次。我还应该提到我从 here 中获取了答案。 Tamàs 似乎是这里主要的 igraph 人。我相信他可以给出更详细的理由,说明为什么您需要使用 Ncol 而不是 Edgelist.

这对我有用。

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

个人插件

这是 igraph 文档可以改进的一个很好的例子。

例如:唯一带有 graph.Read_Edgelist() doc 的附带文字说...

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

当文件需要如何格式化时显然存在细微差别,这并没有真正告诉我任何信息。说出这个函数期望文件的格式可以让很多人保持理智。