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
from sknetwork.visualization import visualize_graph, visualize_bigraph

Graphs

[4]:
graph = karate_club(metadata=True)
adjacency = graph.adjacency
labels = graph.labels
[5]:
louvain = LouvainEmbedding()
embedding = louvain.fit_transform(adjacency)
embedding.shape
[5]:
(34, 4)
[6]:
position = embedding[:, :2]
[7]:
image = visualize_graph(adjacency, position=position, labels=labels)
[8]:
SVG(image)
[8]:
../../_images/tutorials_embedding_louvain_embedding_10_0.svg

Directed graphs

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

Bipartite graphs

[13]:
graph = movie_actor(metadata=True)
biadjacency = graph.biadjacency
names_row = graph.names_row
names_col = graph.names_col
[14]:
louvain = LouvainEmbedding()
louvain.fit(biadjacency)
[14]:
LouvainEmbedding(resolution=1, modularity='dugue', tol_optimization=0.001, tol_aggregation=0.001, n_aggregations=-1, shuffle_nodes=False, isolated_nodes='remove')
[15]:
embedding_row = louvain.embedding_row_
embedding_col = louvain.embedding_col_
[16]:
position_row = embedding_row[:, :2]
position_col = embedding_col[:, :2]
[17]:
image = visualize_bigraph(biadjacency, names_row, names_col,
                    position_row=position_row, position_col=position_col,
                    color_row='blue', color_col='red')
SVG(image)
[17]:
../../_images/tutorials_embedding_louvain_embedding_21_0.svg