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 visualize_graph, visualize_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 = visualize_graph(adjacency, position, scores=np.log(scores))
SVG(image)
[6]:
../../_images/tutorials_ranking_pagerank_8_0.svg
[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]:
../../_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_predict(adjacency)
[11]:
image = visualize_graph(adjacency, position, scores=np.log(scores), names=names)
SVG(image)
[11]:
../../_images/tutorials_ranking_pagerank_14_0.svg
[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]:
../../_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
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]:
../../_images/tutorials_ranking_pagerank_22_0.svg