【问题标题】:How do I run the louvain community detection algorithm in igraph?如何在 igraph 中运行 louvain 社区检测算法?
【发布时间】:2014-09-27 00:13:30
【问题描述】:

谁能给我一个简单的例子,说明如何使用 python 接口在 igraph 中运行 louvain 社区检测算法。有文件吗?

谢谢!

【问题讨论】:

  • 主要问题是我从 multilevel.community 获得的结果与通过在 networkx 中重建相同的精确图然后运行它得到的结果不匹配:perso.crans.org/aynaud/communities/community.py 我浏览了代码,但我没有'看不到任何特定的参数选择。
  • Louvain 中的某些处理是随机的,因此即使多次处理同一张图,也不会有完全相同的结果是正常的。

标签: python graph networkx igraph


【解决方案1】:

它叫multilevel.community

根据https://bugs.launchpad.net/igraph/+bug/925038 ...这个功能确实存在它只是称为igraph_community_multilevel

如果您查看 igraph 的 github 存储库

https://github.com/igraph/igraph/blob/master/src/community.c

igraph_community_multilevel 确实存在,它是用 C 语言编写的。我不是 100% 肯定这是你想要的算法,但它可能是。

这是个好消息!谢谢! 此功能是否导出到 R 中? 为什么函数具有通用名称(igraph_community_multilevel) 而不是作者给出的名称是(“louvain 方法”)? 使用名称“louvain”可以让用户更容易找到功能!

【讨论】:

    【解决方案2】:

    这里是如何在 python (igraph,networkx,bct) 的 3 个不同模块中使用 louvain 算法估计模块化 Q

    import numpy as np
    import networkx as nx
    np.random.seed(9)
    
    # I will generate a stochastic block model using `networkx` and then extract the weighted adjacency matrix.
    sizes = [50, 50, 50] # 3 communities
    probs = [[0.25, 0.05, 0.02],
             [0.05, 0.35, 0.07],
             [0.02, 0.07, 0.40]]
    
    # Build the model
    Gblock = nx.stochastic_block_model(sizes, probs, seed=0)
    
    # Extract the weighted adjacency
    W = np.array(nx.to_numpy_matrix(Gblock, weight='weight'))
    W[W==1] = 1.5
    print(W.shape)
    # (150, 150)
    
    #* Modularity estimation using Louvain algorithm 
    # 1. `igraph` package
    
    from igraph import *
    graph = Graph.Weighted_Adjacency(W.tolist(), mode=ADJ_UNDIRECTED, attr="weight", loops=False)
    louvain_partition = graph.community_multilevel(weights=graph.es['weight'], return_levels=False)
    modularity1 = graph.modularity(louvain_partition, weights=graph.es['weight'])
    print("The modularity Q based on igraph is {}".format(modularity1))
    
    # 2. `networkx`package using `python-louvain`
    # https://python-louvain.readthedocs.io/en/latest/
    
    import networkx as nx
    import community
    G = nx.from_numpy_array(W)
    louvain_partition = community.best_partition(G, weight='weight')
    modularity2 = community.modularity(louvain_partition, G, weight='weight')
    print("The modularity Q based on networkx is {}".format(modularity2))
    
    # 3. `bct` module
    # https://github.com/aestrivex/bctpy
    
    import bct
    com, q = bct.community_louvain(W)
    print("The modularity Q based on bct is {}".format(q))
    
    
    

    打印出来:

    The modularity Q based on igraph is 0.4257613861340037
    The modularity Q based on networkx is 0.4257613861340036
    The modularity Q based on bct is 0.42576138613400366
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-24
      • 2019-02-14
      • 2012-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-21
      • 1970-01-01
      相关资源
      最近更新 更多