用Louvain在图中查找社区

2024-09-27 04:22:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我读了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)

Tags: networkxaddlistnodespartitionnxpkledges
1条回答
网友
1楼 · 发布于 2024-09-27 04:22:36

首先,您可以绘制它以可视化集群:

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,其中概述了时态社区检测等其他特性。你知道吗

相关问题 更多 >

    热门问题