Ward

This notebook illustrates the hierarchical clustering of graphs by the Ward method, after embedding in a space of low dimension.

[1]:
from IPython.display import SVG
[2]:
import numpy as np
[3]:
from sknetwork.data import karate_club, painters, movie_actor
from sknetwork.embedding import Spectral
from sknetwork.hierarchy import Ward, cut_straight, dasgupta_score, tree_sampling_divergence
from sknetwork.visualization import svg_graph, svg_bigraph, svg_dendrogram

Graphs

[4]:
graph = karate_club(metadata=True)
adjacency = graph.adjacency
position = graph.position
[5]:
# hierarchical clustering
ward = Ward()
dendrogram = ward.fit_predict(adjacency)
[6]:
image = svg_dendrogram(dendrogram)
SVG(image)
[6]:
../../_images/tutorials_hierarchy_ward_8_0.svg
[7]:
# cuts
labels = cut_straight(dendrogram)
print(labels)
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 1 1]
[8]:
n_clusters = 4
labels, dendrogram_aggregate = cut_straight(dendrogram, n_clusters, return_dendrogram=True)
print(labels)
[0 2 0 3 0 0 0 3 0 0 0 0 3 3 1 1 0 2 1 2 1 2 1 0 0 0 0 0 0 0 0 0 1 1]
[9]:
_, counts = np.unique(labels, return_counts=True)
[10]:
# aggregate dendrogram
image = svg_dendrogram(dendrogram_aggregate, names=counts, rotate_names=False)
SVG(image)
[10]:
../../_images/tutorials_hierarchy_ward_12_0.svg
[11]:
# clustering
image = svg_graph(adjacency, position, labels=labels)
SVG(image)
[11]:
../../_images/tutorials_hierarchy_ward_13_0.svg
[12]:
# metrics
dasgupta_score(adjacency, dendrogram)
[12]:
0.5082956259426847
[13]:
# other embedding
ward = Ward(embedding_method=Spectral(4))

Directed graphs

[14]:
graph = painters(metadata=True)
adjacency = graph.adjacency
position = graph.position
names = graph.names
[15]:
# hierarchical clustering
ward = Ward()
dendrogram = ward.fit_predict(adjacency)
[16]:
image = svg_dendrogram(dendrogram, names, n_clusters=3, rotate=True)
SVG(image)
[16]:
../../_images/tutorials_hierarchy_ward_19_0.svg
[17]:
# cut with 3 clusters
labels = cut_straight(dendrogram, n_clusters = 3)
print(labels)
[0 0 1 1 0 2 0 0 0 2 0 1 0 0]
[18]:
image = svg_graph(adjacency, position, names=names, labels=labels)
SVG(image)
[18]:
../../_images/tutorials_hierarchy_ward_21_0.svg
[19]:
# metrics
dasgupta_score(adjacency, dendrogram)
[19]:
0.31285714285714294

Bipartite graphs

[20]:
graph = movie_actor(metadata=True)
biadjacency = graph.biadjacency
names_row = graph.names_row
names_col = graph.names_col
[21]:
# hierarchical clustering
ward = Ward(co_cluster = True)
ward.fit(biadjacency)
[21]:
Ward(embedding_method=Spectral(n_components=10, decomposition='rw', regularization=-1, normalized=True), co_cluster=True)
[22]:
dendrogram_row = ward.dendrogram_row_
dendrogram_col = ward.dendrogram_col_
dendrogram_full = ward.dendrogram_full_
[23]:
image = svg_dendrogram(dendrogram_row, names_row, n_clusters=4, rotate=True)
SVG(image)
[23]:
../../_images/tutorials_hierarchy_ward_27_0.svg
[24]:
image = svg_dendrogram(dendrogram_col, names_col, n_clusters=4, rotate=True)
SVG(image)
[24]:
../../_images/tutorials_hierarchy_ward_28_0.svg
[25]:
# cuts
labels = cut_straight(dendrogram_full, n_clusters = 4)
n_row = biadjacency.shape[0]
labels_row = labels[:n_row]
labels_col = labels[n_row:]
[26]:
image = svg_bigraph(biadjacency, names_row, names_col, labels_row, labels_col)
SVG(image)
[26]:
../../_images/tutorials_hierarchy_ward_30_0.svg