GSVD

This notebook illustrates the embedding of a graph through the generalized singular value decomposition of the adjacency matrix.

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

Graphs

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

Embedding

[5]:
gsvd = GSVD(3, normalized=False)
embedding = gsvd.fit_transform(adjacency)
embedding.shape
[5]:
(34, 3)
[6]:
# skip first component
position = embedding[:,1:]
[7]:
image = svg_graph(adjacency, position, labels=labels)
[8]:
SVG(image)
[8]:
../../_images/tutorials_embedding_gsvd_11_0.svg

Metrics

[9]:
cosine_modularity(adjacency, embedding)
[9]:
0.293795138937445

Digraphs

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

Embedding

[11]:
gsvd = GSVD(3, normalized=False)
embedding = gsvd.fit_transform(adjacency)
embedding.shape
[11]:
(14, 3)
[12]:
# skip first component
position = embedding[:,1:]
[13]:
image = svg_digraph(adjacency, position, names=names)
[14]:
SVG(image)
[14]:
../../_images/tutorials_embedding_gsvd_20_0.svg

Metrics

[15]:
cosine_modularity(adjacency, embedding)
[15]:
0.3903213346351039

Bigraphs

Loading

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

Embedding

[17]:
gsvd = GSVD(3, normalized=False)
gsvd.fit(biadjacency)
[17]:
GSVD(n_components=3, regularization=None, relative_regularization=True, factor_row=0.5, factor_col=0.5, factor_singular=0.0, normalized=False, solver=LanczosSVD(maxiter=None, tol=0.0))
[18]:
embedding_row = gsvd.embedding_row_
embedding_row.shape
[18]:
(15, 3)
[19]:
embedding_col = gsvd.embedding_col_
embedding_col.shape
[19]:
(16, 3)
[20]:
# skip first component
position_row = embedding_row[:,1:]
position_col = embedding_col[:,1:]
[21]:
image = svg_bigraph(biadjacency, names_row, names_col,
                    position_row=embedding_row, position_col=embedding_col,
                    color_row='blue', color_col='red')
[22]:
SVG(image)
[22]:
../../_images/tutorials_embedding_gsvd_32_0.svg