Ben Ben - 6 months ago 105
Python Question

Different result of community detection function in igraph R and Python

I detected communities in a large network and got a low modularity in igraph Python. I suspected the result. So I tried again in igraph R. The modularity was much higher. I do not know the reason. In the following, I will write a sample network and the code I used.

graph:(ncol format. The graph is undirected. The third column is weight.)

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

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


Python code:

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


In my original network, the number of communities using R and Python are much different. It is not only the multilevel method. I also tried fastgreedy method. The results using R and Python are also different.

Answer

The R interface is automatically using the weights from the weight attribute when it calculates the community structure and the modularity, while the Python interface doesn't. E.g., in Python:

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