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, BiPageRank
from sknetwork.visualization import svg_graph, svg_digraph, svg_bigraph

Graphs

[4]:
graph = karate_club(metadata=True)
adjacency = graph.adjacency
position = graph.position

Ranking

[5]:
pagerank = PageRank()
scores = pagerank.fit_transform(adjacency)
[6]:
image = svg_graph(adjacency, position, scores=np.log(scores))
[7]:
SVG(image)
[7]:
../../_images/tutorials_ranking_pagerank_10_0.svg

Ranking with personalization

[8]:
seeds = {1: 1, 10: 1}
[9]:
scores = pagerank.fit_transform(adjacency, seeds)
[10]:
image = svg_graph(adjacency, position, scores=np.log(scores), seeds=seeds)
[11]:
SVG(image)
[11]:
../../_images/tutorials_ranking_pagerank_15_0.svg

Digraphs

[12]:
graph = painters(metadata=True)
adjacency = graph.adjacency
names = graph.names
position = graph.position

Ranking

[13]:
pagerank = PageRank()
scores = pagerank.fit_transform(adjacency)
[14]:
image = svg_digraph(adjacency, position, scores=np.log(scores), names=names)
[15]:
SVG(image)
[15]:
../../_images/tutorials_ranking_pagerank_21_0.svg

Ranking with personalization

[16]:
cezanne = 11
seeds = {cezanne:1}
[17]:
scores = pagerank.fit_transform(adjacency, seeds)
[18]:
image = svg_digraph(adjacency, position, names, scores=np.log(scores + 1e-6), seeds=seeds)
[19]:
SVG(image)
[19]:
../../_images/tutorials_ranking_pagerank_26_0.svg

Bigraphs

[20]:
graph = movie_actor(metadata=True)
biadjacency = graph.biadjacency
names_row = graph.names_row
names_col = graph.names_col

Ranking

[21]:
bipagerank = BiPageRank()
[22]:
drive = 3
aviator = 9
seeds_row={drive: 1, aviator: 1}
[23]:
bipagerank.fit(biadjacency, seeds_row)
scores_row = bipagerank.scores_row_
scores_col = bipagerank.scores_col_
[24]:
image = svg_bigraph(biadjacency, names_row, names_col,
                    scores_row=np.log(scores_row), scores_col=np.log(scores_col), seeds_row=seeds_row)
[25]:
SVG(image)
[25]:
../../_images/tutorials_ranking_pagerank_34_0.svg