Louvain embedding

This notebook illustrates the spectral embedding of a graph obtained by looking at the edges between a node and each cluster in a clustering obtained by using the Louvain method for bipartite graphs.

[1]:
import numpy as np
[2]:
from sknetwork.data import karate_club, painters, movie_actor
from sknetwork.embedding import LouvainEmbedding, BiLouvainEmbedding, cosine_modularity

Graphs

[3]:
graph = karate_club(metadata=True)
adjacency = graph.adjacency
labels = graph.labels

Embedding

[4]:
louvain = LouvainEmbedding()
embedding = louvain.fit_transform(adjacency)
embedding.shape
[4]:
(34, 7)

Predict

[5]:
# find the embedding of a new node
adjacency_vector = np.zeros(adjacency.shape[0], dtype = int)
adjacency_vector[:5] = np.ones(5, dtype = int)
[6]:
embedding_vector = louvain.predict(adjacency_vector)

Metrics

[7]:
embedding
[7]:
<34x7 sparse matrix of type '<class 'numpy.float64'>'
        with 75 stored elements in Compressed Sparse Row format>
[8]:
adjacency
[8]:
<34x34 sparse matrix of type '<class 'numpy.bool_'>'
        with 156 stored elements in Compressed Sparse Row format>
[9]:
cosine_modularity(adjacency, embedding)
[9]:
0.15385367663099692

Digraphs

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

Embedding

[11]:
louvain = LouvainEmbedding()
embedding = louvain.fit_transform(adjacency)
embedding.shape
[11]:
(14, 5)

Metrics

[12]:
cosine_modularity(adjacency, embedding)
[12]:
0.1690350595626824

Bigraphs

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

Embedding

[14]:
bilouvain = BiLouvainEmbedding()
bilouvain.fit(biadjacency)
[14]:
BiLouvainEmbedding(resolution=1.0, merge_isolated=True, modularity='dugue', tol_optimization=0.001, tol_aggregation=0.001, n_aggregations=-1, shuffle_nodes=False)
[15]:
embedding_row = bilouvain.embedding_row_
embedding_row.shape
[15]:
(15, 5)
[16]:
embedding_col = bilouvain.embedding_col_
embedding_col.shape
[16]:
(16, 5)