Diffusion

This notebook illustrates the classification of the nodes of a graph by diffusion, based on the labels of a few nodes.

[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 DiffusionClassifier, BiDiffusionClassifier
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]:
diffusion = DiffusionClassifier()
labels_pred = diffusion.fit_transform(adjacency, seeds)
[7]:
precision = np.round(np.mean(labels_pred == labels_true), 2)
precision
[7]:
0.94
[8]:
image = svg_graph(adjacency, position, labels=labels_pred, seeds=seeds)
[9]:
SVG(image)
[9]:
../../_images/tutorials_classification_diffusion_12_0.svg

Soft classification

[10]:
scores = diffusion.score(1)
[11]:
image = svg_graph(adjacency, position, scores=scores, seeds=seeds)
[12]:
SVG(image)
[12]:
../../_images/tutorials_classification_diffusion_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}
[15]:
diffusion = DiffusionClassifier()
labels = diffusion.fit_transform(adjacency, seeds)
[16]:
image = svg_digraph(adjacency, position, names, labels, seeds=seeds)
[17]:
SVG(image)
[17]:
../../_images/tutorials_classification_diffusion_23_0.svg

Soft classification

[18]:
scores = diffusion.score(0)
[19]:
image = svg_digraph(adjacency, position, names=names, scores=scores, seeds=[cezanne])
[20]:
SVG(image)
[20]:
../../_images/tutorials_classification_diffusion_27_0.svg

Bigraphs

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

Classification

[22]:
inception = 0
drive = 3
[23]:
seeds_row = {inception: 0, drive: 1}
[24]:
bidiffusion = BiDiffusionClassifier()
bidiffusion.fit(biadjacency, seeds_row)
labels_row = bidiffusion.labels_row_
labels_col = bidiffusion.labels_col_
[25]:
image = svg_bigraph(biadjacency, names_row, names_col, labels_row, labels_col, seeds_row=seeds_row)
[26]:
SVG(image)
[26]:
../../_images/tutorials_classification_diffusion_35_0.svg

Soft classification

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