This is an implementation of the Chinese Whispers graph clustering algorithm in Python.
from chinese_whispers import __version__ as cw_version
from networkx import __version__ as nx_version
from matplotlib import __version__ as plt_version
print('Chinese Whispers {}'.format(cw_version))
print('NetworkX {}'.format(nx_version))
print('matplotlib {}'.format(plt_version))
Chinese Whispers 0.8.2.rc1 NetworkX 3.1 matplotlib 3.7.1
import networkx as nx
from chinese_whispers import chinese_whispers, aggregate_clusters
G = nx.karate_club_graph()
# Perform clustering of G, parameters weighting and seed can be omitted
chinese_whispers(G, weighting='top', seed=1337)
# Print the clusters in the descending order of size
print('ID\tCluster\n')
for label, cluster in sorted(aggregate_clusters(G).items(), key=lambda e: len(e[1]), reverse=True):
print('{}\t{}\n'.format(label, cluster))
ID Cluster 24 {32, 33, 8, 9, 14, 15, 18, 20, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31} 3 {0, 1, 2, 3, 4, 7, 10, 11, 12, 13, 17, 19, 21} 7 {16, 5, 6}
%config InlineBackend.figure_formats = ['retina']
import matplotlib.pyplot as plt
colors = [1. / G.nodes[node]['label'] for node in G.nodes()]
nx.draw_networkx(G, cmap=plt.get_cmap('jet'), node_color=colors, font_color='white')
@article{Ustalov:19:cl,
author = {Ustalov, Dmitry and Panchenko, Alexander and Biemann, Chris and Ponzetto, Simone Paolo},
title = {{Watset: Local-Global Graph Clustering with Applications in Sense and Frame Induction}},
journal = {Computational Linguistics},
year = {2019},
volume = {45},
number = {3},
pages = {423--479},
doi = {10.1162/COLI_a_00354},
publisher = {MIT Press},
issn = {0891-2017},
language = {english},
}
In case you require higher performance, please consider our Java implementation that also includes other graph clustering algorithms: https://github.com/nlpub/watset-java.