Spectral

This notebook illustrates the spectral embedding of a graph.

[1]:
from IPython.display import SVG
[2]:
import numpy as np
from scipy import sparse
[3]:
from sknetwork.data import karate_club, painters, movie_actor
from sknetwork.embedding import Spectral
from sknetwork.visualization import visualize_graph, visualize_bigraph

Graphs

[4]:
graph = karate_club(metadata=True)
adjacency = graph.adjacency
labels = graph.labels
[5]:
# embedding in dimension 2
spectral = Spectral(2)
embedding = spectral.fit_transform(adjacency)
[6]:
# visualization
image = visualize_graph(adjacency, position=embedding, labels=labels)
SVG(image)
[6]:
../../_images/tutorials_embedding_spectral_8_0.svg

Directed graphs

[7]:
graph = painters(metadata=True)
adjacency = graph.adjacency
position = graph.position
names = graph.names
[8]:
# embedding
spectral = Spectral()
embedding = spectral.fit_transform(adjacency)
[9]:
image = visualize_graph(adjacency, position=embedding, names=names)
SVG(image)
[9]:
../../_images/tutorials_embedding_spectral_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]:
# embedding
spectral = Spectral()
spectral.fit(biadjacency)
[11]:
Spectral(n_components=2, decomposition='rw', regularization=-1, normalized=True)
[12]:
embedding_row = spectral.embedding_row_
embedding_col = spectral.embedding_col_
[13]:
image = visualize_bigraph(biadjacency, names_row, names_col,
                    position_row=embedding_row, position_col=embedding_col,
                    color_row='blue', color_col='red')
SVG(image)

[13]:
../../_images/tutorials_embedding_spectral_17_0.svg