更新时间:2023-01-28 11:26:25
您处在正确的轨道上;可以通过communities.optimal_count
检索社区的***数量(其中最优"被定义为最大化模块化得分的社区数量"),并且可以使用communities.as_clustering(num_communities)
将社区结构转换为平坦的不连续聚类.如果恰好等于communities.optimal_count
,则可以忽略社区数目.完成后,您将获得一个具有membership
属性的VertexClustering
对象,该属性为您提供图形中每个顶点的聚簇索引
You are on the right track; the optimal number of communities (where "optimal" is defined as "the number of communities that maximizes the modularity score) can be retrieved by communities.optimal_count
and the community structure can be converted into a flat disjoint clustering using communities.as_clustering(num_communities)
. Actually, the number of communities can be omitted if it happens to be equal to communities.optimal_count
. Once you've done that, you get a VertexClustering
object with a membership
property which gives you the cluster index for each vertex in the graph.
为清楚起见,我将您的communities
变量重命名为dendrogram
,因为边缘中间性社区检测算法实际上会生成树状图::
For sake of clarity, I'm renaming your communities
variable to dendrogram
because the edge betweenness community detection algorithm actually produces a dendrogram::
# calculate dendrogram
dendrogram = graph.community_edge_betweenness()
# convert it into a flat clustering
clusters = dendrogram.as_clustering()
# get the membership vector
membership = clusters.membership
现在,我们可以开始将成员矢量和节点名称一起写入CSV文件:
Now we can start writing the membership vector along with the node names into a CSV file::
import csv
from itertools import izip
writer = csv.writer(open("output.csv", "wb"))
for name, membership in izip(graph.vs["name"], membership):
writer.writerow([name, membership])
如果您使用的是Python 3,请使用zip
而不是izip
,并且无需导入itertools
.
If you are using Python 3, use zip
instead of izip
and there is no need to import itertools
.