扩散

此笔记本演示了通过 热扩散 进行回归任务。

[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

[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]:
../../_images/tutorials_regression_diffusion_8_0.svg

有向图

[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]:
../../_images/tutorials_regression_diffusion_13_0.svg

二部图

[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]:
../../_images/tutorials_regression_diffusion_18_0.svg

由于种子在电影上,您需要进行偶数次迭代才能获得非平凡的电影排名。 这是由于图的二部结构。

[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]:
../../_images/tutorials_regression_diffusion_21_0.svg