igraph R和Python中社区检测功能的不同结果

时间:2014-10-27 10:42:26

标签: python r igraph modularity

我在大型网络中检测到社区,并且在igraph Python中获得了低模块性。我怀疑结果。所以我在igraph R再试一次。模块化程度要高得多。我不知道原因。在下文中,我将编写一个示例网络和我使用的代码。

graph :( ncol格式。图表是无向的。第三列是权重。)

1 2 123
1 3 32
2 3 523
3 6 3
6 5 11
6 8 234
5 8 324
3 9 234
9 11 32
9 12 5534
9 13 32
11 12 322
11 13 3
12 13 32

R代码:

library(igraph)
g=read.graph('graph.ncol',format='ncol',directed=F)
c=multilevel.community(g)
modularity(c)
[1] 0.2845496

Python代码:

import igraph
g=igraph.Graph.Read_Ncol('graph.ncol',directed=False)
c=g.community_multilevel()
c.modularity
0.4974489795918367

在我原来的网络中,使用R和Python的社区数量差异很大。它不仅是多级方法。我也尝试过fastgreedy方法。使用R和Python的结果也不同。

1 个答案:

答案 0 :(得分:2)

R接口在计算社区结构和模块性时会自动使用weight属性的权重,而Python接口则不会。例如,在Python中:

>>> g=load("test.ncol", directed=False)
>>> g.multilevel_community().q
0.4974489795918367
>>> g.multilevel_community(weights=g.es["weight"]).q
0.2845496103894415