Diffusion
This notebook illustrates a regression task by heat diffusion.
[1]:
from IPython.display import SVG
[2]:
import numpy as np
[3]:
from sknetwork.data import karate_club, painters, movie_actor
from sknetwork.regression import Diffusion
from sknetwork.visualization import visualize_graph, visualize_bigraph
Graphs
[4]:
graph = karate_club(metadata=True)
adjacency = graph.adjacency
position = graph.position
labels_true = graph.labels
[5]:
# heat diffusion
diffusion = Diffusion()
values = {0: 0, 33: 1}
values_pred = diffusion.fit_predict(adjacency, values)
[6]:
image = visualize_graph(adjacency, position, scores=values_pred, seeds=values)
SVG(image)
[6]:
Directed graphs
[7]:
graph = painters(metadata=True)
adjacency = graph.adjacency
position = graph.position
names = graph.names
[8]:
picasso = 0
manet = 3
[9]:
diffusion = Diffusion()
values = {picasso: 1, manet: 1}
values_pred = diffusion.fit_predict(adjacency, values, init=0)
[10]:
image = visualize_graph(adjacency, position, names, scores=values_pred, seeds=values)
SVG(image)
[10]:
Bipartite graphs
[11]:
graph = movie_actor(metadata=True)
biadjacency = graph.biadjacency
names_row = graph.names_row
names_col = graph.names_col
[12]:
drive = 3
aviator = 9
[13]:
diffusion = Diffusion()
values_row = {drive: 0, aviator: 1}
diffusion.fit(biadjacency, values_row=values_row)
values_row_pred = diffusion.values_row_
values_col_pred = diffusion.values_col_
[14]:
image = visualize_bigraph(biadjacency, names_row, names_col, scores_row=values_row_pred, scores_col=values_col_pred,
seeds_row=values_row)
SVG(image)
[14]:
Since seeds are on movies, you need an even number of iterations to get non-trivial ranking of movies. This is due to the bipartite structure of the graph.
[15]:
# changing the number of iterations
diffusion = Diffusion(n_iter=4)
values_row = {drive: 0, aviator: 1}
diffusion.fit(biadjacency, values_row=values_row)
values_row_pred = diffusion.values_row_
values_col_pred = diffusion.values_col_
[16]:
image = visualize_bigraph(biadjacency, names_row, names_col, scores_row=values_row_pred, scores_col=values_col_pred,
seeds_row=values_row)
SVG(image)
[16]: