# 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, cut_straight, dasgupta_score, tree_sampling_divergence
from sknetwork.visualization import visualize_graph, visualize_bigraph, visualize_dendrogram


## Graphs

[4]:

graph = karate_club(metadata=True)
position = graph.position

[5]:

# hierarchical clustering
paris = Paris()

[6]:

image = visualize_dendrogram(dendrogram)
SVG(image)

[6]:

[7]:

# cuts of the dendrogram
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]

[8]:

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]

[9]:

_, counts = np.unique(labels, return_counts=True)

[10]:

# aggregate dendrogram
image = visualize_dendrogram(dendrogram_aggregate, names=counts, rotate_names=False)
SVG(image)

[10]:

[11]:

# corresponding clustering
SVG(image)

[11]:

[12]:

# metrics

[12]:

0.6655354449472097


## Directed graphs

[13]:

graph = painters(metadata=True)
position = graph.position
names = graph.names

[14]:

# hierarchical clustering
paris = Paris()

[15]:

image = visualize_dendrogram(dendrogram, names, n_clusters=3, rotate=True)
SVG(image)

[15]:

[16]:

# 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]

[17]:

image = visualize_graph(adjacency, position, names=names, labels=labels)
SVG(image)

[17]:

[18]:

# metrics

[18]:

0.5842857142857143


## Bipartite graphs

[19]:

graph = movie_actor(metadata=True)
names_row = graph.names_row
names_col = graph.names_col

[20]:

# hierarchical clustering
paris = Paris()
dendrogram_row = paris.dendrogram_row_
dendrogram_col = paris.dendrogram_col_
dendrogram_full = paris.dendrogram_full_

[21]:

image = visualize_dendrogram(dendrogram_row, names_row, n_clusters=4, rotate=True)
SVG(image)

[21]:

[22]:

image = visualize_dendrogram(dendrogram_col, names_col, n_clusters=4, rotate=True)
SVG(image)

[22]:

[23]:

# cuts
labels = cut_straight(dendrogram_full, n_clusters = 4)

[24]:

image = visualize_bigraph(biadjacency, names_row, names_col, labels_row, labels_col)

[24]: