Chinese Whispers for Python¶
This is an implementation of the Chinese Whispers graph clustering algorithm in Python.
Version Information¶
In [1]:
from matplotlib import __version__ as plt_version
from networkx import __version__ as nx_version
from chinese_whispers import __version__ as cw_version
print(f"Chinese Whispers {cw_version}")
print(f"NetworkX {nx_version}")
print(f"matplotlib {plt_version}")
Chinese Whispers 0.9.0 NetworkX 3.4.2 matplotlib 3.9.2
Clustering¶
In [2]:
import networkx as nx
from chinese_whispers import aggregate_clusters, chinese_whispers
In [3]:
G = nx.karate_club_graph()
In [4]:
# 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(f"{label}\t{cluster}\n")
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}
Visualization¶
In [5]:
%config InlineBackend.figure_formats = ['svg']
import matplotlib.pyplot as plt
In [6]:
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")
Citation¶
- Ustalov, D., Panchenko, A., Biemann, C., Ponzetto, S.P.: Watset: Local-Global Graph Clustering with Applications in Sense and Frame Induction. Computational Linguistics 45(3), 423–479 (2019)
@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.
In [ ]: