PageRank
此笔记本演示了使用 PageRank 对图中节点进行排序。
[1]:
from IPython.display import SVG
[2]:
import numpy as np
[3]:
from sknetwork.data import karate_club, painters, movie_actor
from sknetwork.ranking import PageRank
from sknetwork.visualization import visualize_graph, visualize_bigraph
图
[4]:
graph = karate_club(metadata=True)
adjacency = graph.adjacency
position = graph.position
[5]:
# PageRank
pagerank = PageRank()
scores = pagerank.fit_predict(adjacency)
[6]:
image = visualize_graph(adjacency, position, scores=np.log(scores))
SVG(image)
[6]:
[7]:
# personalized PageRank
weights = {1: 1, 10: 1}
scores = pagerank.fit_predict(adjacency, weights)
[8]:
image = visualize_graph(adjacency, position, scores=np.log(scores), seeds=weights)
SVG(image)
[8]:
有向图
[9]:
graph = painters(metadata=True)
adjacency = graph.adjacency
names = graph.names
position = graph.position
[10]:
# PageRank
pagerank = PageRank()
scores = pagerank.fit_predict(adjacency)
[11]:
image = visualize_graph(adjacency, position, scores=np.log(scores), names=names)
SVG(image)
[11]:
[12]:
# personalized PageRank
cezanne = 11
weights = {cezanne:1}
scores = pagerank.fit_predict(adjacency, weights)
[13]:
image = visualize_graph(adjacency, position, names, scores=np.log(scores + 1e-6), seeds=weights)
SVG(image)
[13]:
二部图
[14]:
graph = movie_actor(metadata=True)
biadjacency = graph.biadjacency
names_row = graph.names_row
names_col = graph.names_col
[15]:
pagerank = PageRank()
[16]:
drive = 3
aviator = 9
weights_row={drive: 1, aviator: 1}
[17]:
pagerank.fit(biadjacency, weights_row)
scores_row = pagerank.scores_row_
scores_col = pagerank.scores_col_
[18]:
image = visualize_bigraph(biadjacency, names_row, names_col,
scores_row=np.log(scores_row), scores_col=np.log(scores_col), seeds_row=weights_row)
SVG(image)
[18]: