Louvain Hierarchy

This notebook illustrates the embedding of a graph by the hierarchical Louvain algorithm.

[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 HLouvainEmbedding, BiHLouvainEmbedding
from sknetwork.visualization import svg_graph, svg_digraph, svg_bigraph

Graphs

[4]:
graph = karate_club(metadata=True)
adjacency = graph.adjacency
labels = graph.labels
[5]:
louvain = HLouvainEmbedding(2)
embedding = louvain.fit_transform(adjacency)
embedding.shape
[5]:
(34, 2)
[6]:
image = svg_graph(adjacency, embedding, labels=labels)
[7]:
SVG(image)
[7]:
../../_images/tutorials_embedding_louvain_hierarchy_9_0.svg

Digraphs

[8]:
graph = painters(metadata=True)
adjacency = graph.adjacency
position = graph.position
names = graph.names
[9]:
bilouvain = BiHLouvainEmbedding(2)
embedding = bilouvain.fit_transform(adjacency)
embedding.shape
[9]:
(14, 2)
[10]:
image = svg_digraph(adjacency, embedding, names=names)
[11]:
SVG(image)
[11]:
../../_images/tutorials_embedding_louvain_hierarchy_14_0.svg

Bigraphs

[12]:
graph = movie_actor(metadata=True)
biadjacency = graph.biadjacency
names_row = graph.names_row
names_col = graph.names_col
[13]:
bilouvain = BiHLouvainEmbedding()
bilouvain.fit(biadjacency)
[13]:
BiHLouvainEmbedding(n_components=2, scale=0.1)
[14]:
embedding_row = bilouvain.embedding_row_
embedding_row.shape
[14]:
(15, 2)
[15]:
embedding_col = bilouvain.embedding_col_
embedding_col.shape
[15]:
(16, 2)
[16]:
image = svg_bigraph(biadjacency, names_row, names_col,
                    position_row=embedding_row, position_col=embedding_col,
                    color_row='blue', color_col='red')
[17]:
SVG(image)
[17]:
../../_images/tutorials_embedding_louvain_hierarchy_21_0.svg