Paris

This notebook illustrates the hierarchical clustering of graphs by the Paris algorithm.

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

Graphs

[4]:
graph = karate_club(metadata=True)
adjacency = graph.adjacency
position = graph.position

Hierarchy

[5]:
paris = Paris()
dendrogram = paris.fit_transform(adjacency)
[6]:
image = svg_dendrogram(dendrogram)
[7]:
SVG(image)
[7]:
../../_images/tutorials_hierarchy_paris_10_0.svg

Cuts of the dendrogram

[8]:
labels = cut_straight(dendrogram)
print(labels)
[1 1 1 1 1 1 1 1 0 1 1 1 1 1 0 0 1 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0]
[9]:
n_clusters = 4
labels, dendrogram_aggregate = cut_straight(dendrogram, n_clusters, return_dendrogram=True)
print(labels)
[0 0 0 0 3 3 3 0 1 0 3 0 0 0 1 1 3 0 1 0 1 0 1 2 2 2 2 2 2 2 1 2 1 1]
[10]:
_, counts = np.unique(labels, return_counts=True)
[11]:
image = svg_dendrogram(dendrogram_aggregate, names=counts, rotate_names=False)
[12]:
SVG(image)
[12]:
../../_images/tutorials_hierarchy_paris_16_0.svg
[13]:
image = svg_graph(adjacency, position, labels=labels)
[14]:
SVG(image)
[14]:
../../_images/tutorials_hierarchy_paris_18_0.svg

Metrics

[15]:
dasgupta_score(adjacency, dendrogram)
[15]:
0.6655354449472097
[16]:
tree_sampling_divergence(adjacency, dendrogram)
[16]:
0.4867262842323475

Digraphs

[17]:
graph = painters(metadata=True)
adjacency = graph.adjacency
position = graph.position
names = graph.names

Hierarchy

[18]:
paris = Paris()
dendrogram = paris.fit_transform(adjacency)
[19]:
image = svg_dendrogram(dendrogram, names, n_clusters=3, rotate=True)
[20]:
SVG(image)
[20]:
../../_images/tutorials_hierarchy_paris_27_0.svg

Cuts of the dendrogram

[21]:
# cut with 3 clusters
labels = cut_straight(dendrogram, n_clusters = 3)
print(labels)
[0 0 1 0 1 1 2 0 0 1 0 0 0 2]
[22]:
image = svg_digraph(adjacency, position, names=names, labels=labels)
[23]:
SVG(image)
[23]:
../../_images/tutorials_hierarchy_paris_31_0.svg

Metrics

[24]:
dasgupta_score(adjacency, dendrogram)
[24]:
0.5842857142857143
[25]:
tree_sampling_divergence(adjacency, dendrogram)
[25]:
0.4685697020629489

Bigraphs

[26]:
graph = movie_actor(metadata=True)
biadjacency = graph.biadjacency
names_row = graph.names_row
names_col = graph.names_col

Hierarchy

[27]:
biparis = BiParis()
biparis.fit(biadjacency)
dendrogram_row = biparis.dendrogram_row_
dendrogram_col = biparis.dendrogram_col_
dendrogram_full = biparis.dendrogram_full_
[28]:
image = svg_dendrogram(dendrogram_row, names_row, n_clusters=4, rotate=True)
[29]:
SVG(image)
[29]:
../../_images/tutorials_hierarchy_paris_40_0.svg
[30]:
image = svg_dendrogram(dendrogram_col, names_col, n_clusters=4, rotate=True)
[31]:
SVG(image)
[31]:
../../_images/tutorials_hierarchy_paris_42_0.svg

Cuts of the dendrogram

[32]:
labels = cut_straight(dendrogram_full, n_clusters = 4)
n_row = biadjacency.shape[0]
labels_row = labels[:n_row]
labels_col = labels[n_row:]
[33]:
image = svg_bigraph(biadjacency, names_row, names_col, labels_row, labels_col)
[34]:
SVG(image)
[34]:
../../_images/tutorials_hierarchy_paris_46_0.svg