Dirichlet

This notebook illustrates the classification of the nodes of a graph by the Dirichlet problem, 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 DirichletClassifier
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
[5]:
seeds = {i: labels_true[i] for i in [0, 33]}
[6]:
dirichlet = DirichletClassifier()
labels_pred = dirichlet.fit_transform(adjacency, seeds)
[7]:
precision = np.round(np.mean(labels_pred == labels_true), 2)
precision
[7]:
0.97
[8]:
image = svg_graph(adjacency, position, labels=labels_pred, seeds=seeds)
SVG(image)
[8]:
../../_images/tutorials_classification_dirichlet_10_0.svg
[9]:
# soft classification (here probability of label 1)
membership = dirichlet.membership_
scores = membership[:,1].toarray().ravel()
[10]:
image = svg_graph(adjacency, position, scores=scores, seeds=seeds)
SVG(image)
[10]:
../../_images/tutorials_classification_dirichlet_12_0.svg

Directed graphs

[11]:
graph = painters(metadata=True)
adjacency = graph.adjacency
position = graph.position
names = graph.names
[12]:
rembrandt = 5
klimt = 6
cezanne = 11
seeds = {cezanne: 0, rembrandt: 1, klimt: 2}
[13]:
dirichlet = DirichletClassifier()
labels = dirichlet.fit_transform(adjacency, seeds)
[14]:
image = svg_digraph(adjacency, position, names, labels=labels, seeds=seeds)
SVG(image)
[14]:
../../_images/tutorials_classification_dirichlet_17_0.svg
[15]:
# soft classification (here probability of label 0)
membership = dirichlet.membership_
scores = membership[:,0].toarray().ravel()
[16]:
image = svg_digraph(adjacency, position, names=names, scores=scores, seeds=[cezanne])
SVG(image)
[16]:
../../_images/tutorials_classification_dirichlet_19_0.svg

Bipartite graphs

[17]:
graph = movie_actor(metadata=True)
biadjacency = graph.biadjacency
names_row = graph.names_row
names_col = graph.names_col
[18]:
inception = 0
drive = 3
budapest = 8
[19]:
seeds_row = {inception: 0, drive: 1, budapest: 2}
[20]:
dirichlet = DirichletClassifier()
dirichlet.fit(biadjacency, seeds_row)
labels_row = dirichlet.labels_row_
labels_col = dirichlet.labels_col_
[21]:
image = svg_bigraph(biadjacency, names_row, names_col, labels_row, labels_col, seeds_row=seeds_row)
SVG(image)
[21]:
../../_images/tutorials_classification_dirichlet_25_0.svg
[22]:
# soft classification (here probability of label 1)
membership_row = dirichlet.membership_row_
membership_col = dirichlet.membership_col_
[23]:
scores_row = membership_row[:,1].toarray().ravel()
scores_col = membership_col[:,1].toarray().ravel()
[24]:
image = svg_bigraph(biadjacency, names_row, names_col, scores_row=scores_row, scores_col=scores_col,
                    seeds_row=seeds_row)
SVG(image)
[24]:
../../_images/tutorials_classification_dirichlet_28_0.svg