Python 使用 louvain 包的模块化统计

Python modularity statistic using louvain package

我正在尝试对 python 的 运行 网络进行至少 100 次鲁汶模块化分区。我需要在 partition.Is 之后保存分区和模块化分数,有什么办法可以同时获得它们吗?我已经尝试了文档和示例,它只有 returns 分区但没有模块化统计信息。请为我指出正确的方向,我将非常感谢你的帮助。

您可以使用 igraph.Graph.community():

计算模块化分数
import igraph

g = igraph.Graph.Erdos_Renyi(n=100, p=0.1)
clusters = g.community_multilevel()
modularity_score = g.modularity(clusters.membership)

这里是如何使用 igraph 中的 louvain 算法估计模块性 Q

重要提示:如果您有加权邻接矩阵,请不要忘记以下函数中的 weights=graph.es['weight'] 参数!

import numpy as np
from igraph import *

W = np.random.rand(10,10)
np.fill_diagonal(W,0.0)

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

The modularity Q based on igraph is 0.02543865641866777