如何使用pythonigraph对图进行聚类

2024-10-03 11:21:06 发布

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

我一直在使用pythonigraph来简化生成和分析图形的时间。我下面的代码生成一个随机图,由50个节点组成,并对其进行聚类:

from igraph import *
import random as rn

g = Graph()

size = 50

g.add_vertices(size)

vert = []

for i in range(size):
    for j in range(size):
        test = rn.randint(0,5)
        if j >= i or test is not 0:
            continue
        g.add_edges([(i,j)])

#layout = g.layout("kk")
#plot(g, layout = layout)

#dend = VertexDendrogram(graph=g, optimal_count=10)

clust = VertexClustering(g, membership=range(size))
#clust = dend.as_clustering()

c = clust.cluster_graph()

plot(clust, mark_groups=True)

layout = c.layout("kk")
#plot(c, layout = layout)

但是,我不确定这是否正确,因为结果将每个节点都作为自己的独立集群。我假设这与membership有关,这是VertexClustering的必需参数,因为我真的不明白它的含义或为什么需要它。在

以下是the terse documentation所说的:

the membership list. The length of the list must be equal to the number of vertices in the graph. If None, every vertex is assumed to belong to the same cluster.

并没有解释我应该设置membership以获得正常的规则聚类。在

或者,我尝试使用VertexDendrogram,如两行注释代码所示。但是,运行此操作时会出现以下运行时错误:

^{pr2}$

这是因为VertexDendrogram需要参数merges。但是,我再一次不知道merges应该设置为什么,或者为什么需要它。The documentation再说一遍几乎什么也没说:

merges - the merges performed given in matrix form.

我不知道那是什么。那么我应该如何处理这些代码,以获得一个正常的,有规律的随机图聚类,我可以用它来做实验?在


Tags: thetoinimportsize节点plotrange
1条回答
网友
1楼 · 发布于 2024-10-03 11:21:06

我坚持我最初的评估,即igraph的文档过于简洁,这让人恼火。在

我实际需要的函数是igraph.Graph.community_fastgreedy()。一般来说,似乎所有运行聚类算法的函数都以其名称中的“community”开头。在

相关问题 更多 >