链接预测
此笔记本演示了通过嵌入空间中的最近邻居来预测图的链接。大多数链接应该是图的实际边。
[1]:
from IPython.display import SVG
[2]:
import numpy as np
[3]:
from sknetwork.data import karate_club, painters, movie_actor
from sknetwork.linkpred import NNLinker
from sknetwork.embedding import Spectral
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]:
image = visualize_graph(adjacency, position)
SVG(image)
[5]:
[6]:
linker = NNLinker(n_neighbors=3)
links = linker.fit_predict(adjacency)
[7]:
image = visualize_graph(links, position, directed=False, display_edge_weight=False)
SVG(image)
[7]:
[8]:
linker = NNLinker(threshold=0.5)
links = linker.fit_predict(adjacency)
[9]:
image = visualize_graph(links, position, directed=False, display_edge_weight=False)
SVG(image)
[9]:
[10]:
# Nearest neighbors in embedding space
linker = NNLinker(n_neighbors=5, threshold=0.5, embedding_method=Spectral(2))
links = linker.fit_predict(adjacency)
[11]:
image = visualize_graph(links, position, directed=False, display_edge_weight=False)
SVG(image)
[11]:
有向图
[12]:
graph = painters(metadata=True)
adjacency = graph.adjacency
position = graph.position
names = graph.names
[13]:
image = visualize_graph(adjacency, position, names)
SVG(image)
[13]:
[14]:
linker = NNLinker(embedding_method=Spectral(3))
links = linker.fit_predict(adjacency)
[15]:
image = visualize_graph(links, position, names, directed=True, display_edge_weight=False)
SVG(image)
[15]:
二部图
[16]:
graph = movie_actor(metadata=True)
biadjacency = graph.biadjacency
names_row = graph.names_row
names_col = graph.names_col
[17]:
image = visualize_bigraph(biadjacency, names_row, names_col)
SVG(image)
[17]:
[18]:
linker = NNLinker(n_neighbors=5, threshold=0.5, embedding_method=Spectral(3))
links = linker.fit_predict(biadjacency)
[19]:
image = visualize_bigraph(links, names_row, names_col, display_edge_weight=False)
SVG(image)
[19]: