Spring

This notebook illustrates the 2D embedding of a graph through the force-directed algorithm.

[1]:
from IPython.display import SVG
[2]:
import numpy as np
[3]:
from sknetwork.data import karate_club, painters
from sknetwork.embedding import Spring
from sknetwork.visualization import svg_graph, svg_digraph

Graphs

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

Embedding

[5]:
spring = Spring()
embedding = spring.fit_transform(adjacency)
embedding.shape
[5]:
(34, 2)
[6]:
image = svg_graph(adjacency, embedding, labels=labels)
[7]:
SVG(image)
[7]:
../../_images/tutorials_embedding_spring_10_0.svg

Digraphs

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

Embedding

[9]:
spring = Spring()
embedding = spring.fit_transform(adjacency)
embedding.shape
[9]:
(14, 2)
[10]:
image = svg_digraph(adjacency, embedding, names=names)
[11]:
SVG(image)
[11]:
../../_images/tutorials_embedding_spring_16_0.svg