【问题标题】:Louvain and modularity values of the same graph differ a lot in igraph vs networkx在 igraph 与 networkx 中,同一图的 Louvain 和模块化值差异很大
【发布时间】:2020-03-29 12:29:57
【问题描述】:

我有一个图表,我想根据图表的 Louvain split 估计模块性 (Q)。

在 python 中,我使用 igraph 并进行比较,我还在 Matlab 中估计了 Q。 基于 igraph,Q 为负(奇怪),而基于 Matlab 估计的 Q 为正。

我预计值会有所不同,但到目前为止还没有(+ 表示模块化,- 表示反模块化)。 知道为什么会这样吗?

我的代码和数据(https://gofile.io/?c=h24mcU):

Python

import numpy as np
import scipy.io
from igraph import *

A = scipy.io.loadmat('A.mat')['A']

graph = Graph.Weighted_Adjacency(A.tolist(), mode=ADJ_UNDIRECTED, attr="weight", loops=False)
Louvain = graph.community_multilevel(weights=graph.es['weight'], return_levels=False)
Q = graph.modularity(Louvain)
print(Q)

-0.001847596203445795

MATLAB(大脑连接工具箱) 使用 community_louvain.m:Louvain 社区检测算法

clear all
load('A.mat')
[M,Q]=community_louvain(A);

Q =

   0.1466

PYTHON 版本的 community_louvain.m: https://github.com/aestrivex/bctpy/blob/f9526a693a9af57051762442d8490dcdf2ebf4e3/bct/algorithms/modularity.py#L71,

import bct

split, Q = bct.community_louvain(A)
Q
0.14659657544165258

我再次得到大约。 0.1466 与基于 Matlab 和 Python BCT 的结果相匹配,但与igraph 输出相距甚远。

【问题讨论】:

    标签: python networkx igraph modularity


    【解决方案1】:

    找到解决方案。

    我希望 igraph 能够理解这一点,因为我定义了一个加权邻接矩阵,但我需要明确modularity() 中传递 weights 参数。

    import numpy as np
    import scipy.io
    from igraph import *
    
    A = scipy.io.loadmat("A.mat")['A']
    np.fill_diagonal(A,0.0)
    
    # igraph
    graph = Graph.Weighted_Adjacency(A.tolist(), mode=ADJ_UNDIRECTED, attr="weight", loops=False)
    Louvain = graph.community_multilevel(weights=graph.es['weight'], return_levels=False)
    Q = graph.modularity(Louvain, weights=graph.es['weight'])
    print(Q)
    
    #bctpy
    com, q = bct.community_louvain(A)
    print(q)
    
    0.14133150351832535
    
    0.14133150351832674
    

    【讨论】:

      猜你喜欢
      • 2017-08-23
      • 1970-01-01
      • 2014-09-13
      • 2019-01-06
      • 2014-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多