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 Propagation
from sknetwork.visualization import svg_graph, visualize_bigraph

Graphs

[4]:
graph = karate_club(metadata=True)
adjacency = graph.adjacency
position = graph.position
labels_true = graph.labels
[5]:
labels = {i: labels_true[i] for i in [0, 33]}
[6]:
propagation = Propagation()
labels_pred = propagation.fit_predict(adjacency, labels)
[7]:
image = svg_graph(adjacency, position, labels=labels_pred, seeds=labels)
SVG(image)
[7]:
../../_images/tutorials_classification_propagation_9_0.svg
[8]:
# probability distribution over labels
label = 1
probs = propagation.predict_proba()
scores = probs[:,label]
[9]:
image = svg_graph(adjacency, position, scores=scores, seeds=labels)
SVG(image)
[9]:
../../_images/tutorials_classification_propagation_11_0.svg

Directed graphs

[10]:
graph = painters(metadata=True)
adjacency = graph.adjacency
position = graph.position
names = graph.names
[11]:
rembrandt = 5
klimt = 6
cezanne = 11
labels = {cezanne: 0, rembrandt: 1, klimt: 2}
[12]:
propagation = Propagation()
labels_pred = propagation.fit_predict(adjacency, labels)
[13]:
image = svg_graph(adjacency, position, names, labels=labels_pred, seeds=labels)
SVG(image)
[13]:
../../_images/tutorials_classification_propagation_16_0.svg
[14]:
# probability distribution over labels
probs = propagation.predict_proba(())
scores = probs[:,0]
[15]:
image = svg_graph(adjacency, position, names, scores=scores, seeds=[cezanne])
SVG(image)
[15]:
../../_images/tutorials_classification_propagation_18_0.svg

Bipartite graphs

[16]:
graph = movie_actor(metadata=True)
biadjacency = graph.biadjacency
names_row = graph.names_row
names_col = graph.names_col
[17]:
inception = 0
drive = 3
budapest = 8
[18]:
labels_row = {inception: 0, drive: 1, budapest: 2}
[19]:
propagation = Propagation()
propagation.fit(biadjacency, labels_row)
labels_row_pred = propagation.labels_row_
labels_col_pred = propagation.labels_col_
[20]:
image = visualize_bigraph(biadjacency, names_row, names_col, labels_row_pred, labels_col_pred, seeds_row=labels_row)
SVG(image)
[20]:
../../_images/tutorials_classification_propagation_24_0.svg
[21]:
# probability distribution over labels
probs_row = propagation.predict_proba()
probs_col = propagation.predict_proba(columns=True)
[22]:
scores_row = probs_row[:,1]
scores_col = probs_col[:,1]
[23]:
image = visualize_bigraph(biadjacency, names_row, names_col, scores_row=scores_row, scores_col=scores_col,
                    seeds_row=labels_row)
SVG(image)

[23]:
../../_images/tutorials_classification_propagation_27_0.svg