Propagation

This notebook illustrates the classification of the nodes of a graph by label propagation.

[1]:
from IPython.display import SVG
[2]:
import numpy as np
[3]:
from sknetwork.data import karate_club, painters, movie_actor
from sknetwork.classification import BiPropagation, Propagation
from sknetwork.visualization import svg_graph, svg_digraph, svg_bigraph

Graphs

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

Classification

[5]:
seeds = {i: labels_true[i] for i in [0, 33]}
[6]:
propagation = Propagation()
labels_pred = propagation.fit_transform(adjacency, seeds)
[7]:
image = svg_graph(adjacency, position, labels=labels_pred, seeds=seeds)
[8]:
SVG(image)
[8]:
../../_images/tutorials_classification_propagation_11_0.svg

Soft classification

[9]:
membership = propagation.membership_
[10]:
scores = membership[:,1].toarray().ravel()
[11]:
image = svg_graph(adjacency, position, scores=scores, seeds=seeds)
[12]:
SVG(image)
[12]:
../../_images/tutorials_classification_propagation_16_0.svg

Digraphs

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

Classification

[14]:
rembrandt = 5
klimt = 6
cezanne = 11
seeds = {cezanne: 0, rembrandt: 1, klimt: 2}
[15]:
propagation = Propagation()
labels = propagation.fit_transform(adjacency, seeds)
[16]:
image = svg_digraph(adjacency, position, names, labels, seeds=seeds)
[17]:
SVG(image)
[17]:
../../_images/tutorials_classification_propagation_23_0.svg

Soft classification

[18]:
membership = propagation.membership_
[19]:
scores = membership[:,0].toarray().ravel()
[20]:
image = svg_digraph(adjacency, position, names, scores=scores, seeds=[cezanne])
[21]:
SVG(image)
[21]:
../../_images/tutorials_classification_propagation_28_0.svg

Bipartite graphs

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

Classification

[23]:
inception = 0
drive = 3
budapest = 8
[24]:
seeds_row = {inception: 0, drive: 1, budapest: 2}
[25]:
bipropagation = BiPropagation()
labels_row = bipropagation.fit_transform(biadjacency, seeds_row)
labels_col = bipropagation.labels_col_
[26]:
image = svg_bigraph(biadjacency, names_row, names_col, labels_row, labels_col, seeds_row=seeds_row)
[27]:
SVG(image)
[27]:
../../_images/tutorials_classification_propagation_36_0.svg

Soft classification

[28]:
membership_row = bipropagation.membership_row_
membership_col = bipropagation.membership_col_
[29]:
scores_row = membership_row[:,1].toarray().ravel()
scores_col = membership_col[:,1].toarray().ravel()
[30]:
image = svg_bigraph(biadjacency, names_row, names_col, scores_row=scores_row, scores_col=scores_col,
                    seeds_row=seeds_row)
[31]:
SVG(image)
[31]:
../../_images/tutorials_classification_propagation_41_0.svg