PageRank

This notebook illustrates the ranking of the nodes of a graph by 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 svg_graph, svg_bigraph

Graphs

[4]:
graph = karate_club(metadata=True)
adjacency = graph.adjacency
position = graph.position
[5]:
# PageRank
pagerank = PageRank()
scores = pagerank.fit_predict(adjacency)
[6]:
image = svg_graph(adjacency, position, scores=np.log(scores))
SVG(image)
[6]:
../../_images/tutorials_ranking_pagerank_8_0.svg
[7]:
# personalized PageRank
seeds = {1: 1, 10: 1}
scores = pagerank.fit_predict(adjacency, seeds)
[8]:
image = svg_graph(adjacency, position, scores=np.log(scores), seeds=seeds)
SVG(image)
[8]:
../../_images/tutorials_ranking_pagerank_10_0.svg

Directed graphs

[9]:
graph = painters(metadata=True)
adjacency = graph.adjacency
names = graph.names
position = graph.position
[10]:
# PageRank
pagerank = PageRank()
scores = pagerank.fit_transform(adjacency)
[11]:
image = svg_graph(adjacency, position, scores=np.log(scores), names=names)
SVG(image)
[11]:
../../_images/tutorials_ranking_pagerank_14_0.svg
[12]:
# personalized PageRank
cezanne = 11
seeds = {cezanne:1}
scores = pagerank.fit_predict(adjacency, seeds)
[13]:
image = svg_graph(adjacency, position, names, scores=np.log(scores + 1e-6), seeds=seeds)
SVG(image)
[13]:
../../_images/tutorials_ranking_pagerank_16_0.svg

Bipartite graphs

[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
seeds_row={drive: 1, aviator: 1}
[17]:
pagerank.fit(biadjacency, seeds_row)
scores_row = pagerank.scores_row_
scores_col = pagerank.scores_col_
[18]:
image = svg_bigraph(biadjacency, names_row, names_col,
                    scores_row=np.log(scores_row), scores_col=np.log(scores_col), seeds_row=seeds_row)
SVG(image)
[18]:
../../_images/tutorials_ranking_pagerank_22_0.svg