使用Louvain在图中查找社区

时间:2019-11-06 19:18:46

标签: python graph igraph

我读了T0.pkl,其中包含一个用networkx创建的有向图,因此我在igraph图中启用了该图。 接下来,我将Louvain应用于图形,现在我有一个louvain.VertexPartition.ModularityVertexPartition,我不知道如何使用它

G0_nx = nx.read_gpickle("../snapshots_original/T0.pkl")
G0_nx_edges = list(G0_nx.edges)
G0_nx_nodes = list(G0_nx.nodes)
G0_ig = igraph.Graph(directed=True)
G0_ig.add_vertices(G0_nx_nodes)
G0_ig.add_edges(G0_nx_edges)

partition = louvain.find_partition(G0_ig, lv.ModularityVertexPartition)

1 个答案:

答案 0 :(得分:0)

首先,您可以对其进行绘制以可视化聚类:

igraph.plot(partition)

如果您尝试用简单的方法打印它

print(partition)

您将获得类似于:

 Clustering with V elements and n clusters

 [cluster_id]: node1, node2, node3... (the nodes in the n-th cluster)

例如:

Clustering with 33 elements and 3 clusters

[0] 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
[1] 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21
[2] 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32

然后,您可以访问群集中的节点,就像使用将所需群集ID作为索引的列表一样:

print(partition[1])

[11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]

您还应该检查Louvain库docs,其中概述了其他功能(例如临时社区检测)。