# 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, svg_bigraph


## Graphs

[4]:

graph = karate_club(metadata=True)
position = graph.position
labels_true = graph.labels

[5]:

labels = {i: labels_true[i] for i in [0, 33]}

[6]:

propagation = Propagation()

[7]:

image = svg_graph(adjacency, position, labels=labels_pred, seeds=labels)
SVG(image)

[7]:

[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]:


## Directed graphs

[10]:

graph = painters(metadata=True)
position = graph.position
names = graph.names

[11]:

rembrandt = 5
klimt = 6
cezanne = 11
labels = {cezanne: 0, rembrandt: 1, klimt: 2}

[12]:

propagation = Propagation()

[13]:

image = svg_graph(adjacency, position, names, labels=labels_pred, seeds=labels)
SVG(image)

[13]:

[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]:


## Bipartite graphs

[16]:

graph = movie_actor(metadata=True)
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()
labels_row_pred = propagation.labels_row_
labels_col_pred = propagation.labels_col_

[20]:

image = svg_bigraph(biadjacency, names_row, names_col, labels_row_pred, labels_col_pred, seeds_row=labels_row)
SVG(image)

[20]:

[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 = svg_bigraph(biadjacency, names_row, names_col, scores_row=scores_row, scores_col=scores_col,
seeds_row=labels_row)
SVG(image)

[23]: