如何使用python igraph集群图 [英] How to cluster a graph using python igraph

查看:213
本文介绍了如何使用python igraph集群图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用python igraph来尝试简化生成和分析图形的时间.我的以下代码生成了一个包含50个节点的随机图并将其聚类:

I've been using python igraph to try to make an easier time of generating and analyzing graphs. My code below generates a random graph of 50 nodes and clusters it:

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)

但是,我不确定这是否正确,因为结果将每个节点都作为自己的隔离群集.我以为这与VertexClustering的必需参数membership有某种联系,因为我真的不明白它的含义或原因.

However, I'm not sure if this is correct, since the result has every single node as its own, isolated cluster. I am assuming this is somehow related to membership, the required parameter of VertexClustering, because I really don't understand what it means or why it is required.

这是简洁文档所说的内容:

会员列表.列表的长度必须等于图中的顶点数.如果为None,则假定每个顶点都属于同一簇.

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设置为什么以获得正常的常规聚类.

And doesn't explain what I am supposed to set membership to in order to get a normal regular clustering.

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

Alternatively I tried using the VertexDendrogram as seen in the two commented lines of code. However, running this comes up with this runtime error:

Traceback (most recent call last):
  File "sma_hw6_goedeke.py", line 22, in <module>
    dend = VertexDendrogram(graph=g, optimal_count=10)
TypeError: __init__() takes at least 3 arguments (3 given)

这是因为VertexDendrogram需要参数merges.但是,我再也不知道应该将merges设置为什么或为什么要设置它. 文档再次几乎什么也没说:

This is because VertexDendrogram requires the parameter merges. However, once again I have no clue what merges is supposed to be set to or why it is required. The documentation again says almost nothing:

merges-以矩阵形式给出的合并.

merges - the merges performed given in matrix form.

我不知道那是什么.那么,对于该代码,我应该怎么做才能为我可以尝试的随机图获得正常,规则的聚类?

And I have no clue what that is. So what should I do to this code to get a normal, regular clustering for my random graph I can experiment with?

推荐答案

我坚持最初的评估,认为igraph的文档太过简练了.

I stand by my original assessment that the documentation for igraph is maddeningly too terse.

我实际需要做的功能是igraph.Graph.community_fastgreedy().通常,运行聚类算法的所有功能似乎都以"community"开头.

The function I actually needed for what I am doing is igraph.Graph.community_fastgreedy(). In general, it seems all the functions that run a clustering algorithm all start with "community" in their name.

这篇关于如何使用python igraph集群图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆