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 visualize_graph

Graphs

[4]:
graph = karate_club(metadata=True)
adjacency = graph.adjacency
labels = graph.labels
[5]:
spring = Spring(2)
embedding = spring.fit_transform(adjacency)
[6]:
image = visualize_graph(adjacency, position=embedding, labels=labels)
SVG(image)
[6]:
../../_images/tutorials_embedding_spring_8_0.svg

Directed graphs

[7]:
graph = painters(metadata=True)
adjacency = graph.adjacency
names = graph.names
[8]:
spring = Spring(2)
embedding = spring.fit_transform(adjacency)
embedding.shape
[8]:
(14, 2)
[9]:
image = visualize_graph(adjacency, position=embedding, names=names)
SVG(image)
[9]:
../../_images/tutorials_embedding_spring_12_0.svg