SVD

This notebook illustrates the embedding of a graph through the 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 SVD, 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
[5]:
svd = SVD(2)
embedding = svd.fit_transform(adjacency)
[6]:
image = svg_graph(adjacency, embedding, labels=labels)
SVG(image)
[6]:
../../_images/tutorials_embedding_svd_8_0.svg

Directed graphs

[7]:
graph = painters(metadata=True)
adjacency = graph.adjacency
names = graph.names
[8]:
svd = SVD(2)
embedding = svd.fit_transform(adjacency)
[9]:
image = svg_digraph(adjacency, embedding, names=names)
SVG(image)
[9]:
../../_images/tutorials_embedding_svd_12_0.svg

Bipartite graphs

[10]:
graph = movie_actor(metadata=True)
biadjacency = graph.biadjacency
names_row = graph.names_row
names_col = graph.names_col
[11]:
svd = SVD(2, normalized=False)
svd.fit(biadjacency)
[11]:
SVD(n_components=2, regularization=None, factor_singular=0.0, normalized=False, solver=LanczosSVD(n_iter=None, tol=0.0))
[12]:
embedding_row = svd.embedding_row_
embedding_col = svd.embedding_col_
[13]:
image = svg_bigraph(biadjacency, names_row, names_col,
                    position_row=embedding_row, position_col=embedding_col,
                    color_row='blue', color_col='red', scale=1.5)
SVG(image)
[13]:
../../_images/tutorials_embedding_svd_17_0.svg