为什么 optimal_count 没有给出正确的结果?

Why isn't optimal_count giving the right result?

我正在尝试理解 python-igraph,特别是 community_walktrap 函数。我创建了以下示例:

import numpy as np
import igraph

mat = np.zeros((200,200)) + 50
mat[20:30,20:30] = 2
mat[80:90,80:90] = 2

g = igraph.Graph.Weighted_Adjacency(mat.tolist(),
                                    mode=igraph.ADJ_DIRECTED) 
wl = g.community_walktrap(weights=g.es['weight'])

我假设社区的最佳数量是 3,但是 运行

print wl.optimal_count

给我 1。如果我强制在 3 wl.as_clustering(3) 处切割树状图,我会得到一个正确的成员列表。 optimal_count 我做错了什么?

为什么您认为最佳簇数应该是 3?在我看来,所有节点之间都有相当强的连接(它们的权重为 50),除了连接较弱的两个小组。请注意,igraph 中的聚类方法期望权重表示 相似性 ,而不是距离。另请注意,igraph 中的大多数聚类算法都没有为定向网络明确定义(其中一些甚至简单地拒绝定向网络)。

对于它的价值,wl.optimal_count简单地计算了所谓的模块化度量(参见Graphclass的modularity()方法)然后选择集群计算模块化程度最高的地方。只有一个集群的模块化为零(根据定义,这是度量的工作方式)。三个集群的模块化约为 -0.0083,因此 igraph 只选择一个集群而不是三个集群是正确的:

>>> wl.as_clustering(3).modularity
-0.00829996846600007
>>> wl.as_clustering(1).modularity
0.0