Louvain

This notebook illustrates the embedding of a graph through Louvain clustering.

[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 LouvainEmbedding, BiLouvainEmbedding
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 = LouvainEmbedding()
embedding = louvain.fit_transform(adjacency)
[6]:
embedding.shape
[6]:
(34, 4)
[7]:
position = embedding[:, :2]
[8]:
image = svg_graph(adjacency, position, labels=labels)
[9]:
SVG(image)
[9]:
../../_images/tutorials_embedding_louvain_embedding_11_0.svg

Digraphs

[10]:
graph = painters(metadata=True)
adjacency = graph.adjacency
names = graph.names
[11]:
louvain = LouvainEmbedding()
embedding = louvain.fit_transform(adjacency)
embedding.shape
[11]:
(14, 3)
[12]:
position = embedding[:, :2]
[13]:
image = svg_digraph(adjacency, position, names=names)
[14]:
SVG(image)
[14]:
../../_images/tutorials_embedding_louvain_embedding_17_0.svg

Bigraphs

[15]:
graph = movie_actor(metadata=True)
biadjacency = graph.biadjacency
names_row = graph.names_row
names_col = graph.names_col
[16]:
bilouvain = BiLouvainEmbedding()
bilouvain.fit(biadjacency)
[16]:
BiLouvainEmbedding(resolution=1.0, modularity='dugue', tol_optimization=0.001, tol_aggregation=0.001, n_aggregations=-1, shuffle_nodes=False, isolated_nodes='remove')
[17]:
embedding_row = bilouvain.embedding_row_
embedding_col = bilouvain.embedding_col_
[18]:
position_row = embedding_row[:, :2]
position_col = embedding_col[:, :2]
[19]:
image = svg_bigraph(biadjacency, names_row, names_col,
                    position_row=position_row, position_col=position_col,
                    color_row='blue', color_col='red')
[20]:
SVG(image)
[20]:
../../_images/tutorials_embedding_louvain_embedding_24_0.svg