Skip to content

Commit e182398

Browse files
authored
docs: add personalized PageRank gallery example
1 parent e159d7e commit e182398

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
"""
2+
.. _tutorials-personalized_pagerank:
3+
4+
===============================
5+
Personalized PageRank on a grid
6+
===============================
7+
8+
This example demonstrates how to calculate and visualize personalized PageRank on a grid. We use the :meth:`igraph.Graph.personalized_pagerank` method, and demonstrate the effects on a grid graph.
9+
"""
10+
11+
# %%
12+
# .. note::
13+
#
14+
# The PageRank score of a vertex reflects the probability that a random walker will be at that vertex over the long run. At each step the walker has a 1 - damping chance to restart the walk and pick a starting vertex according to the probabilities defined in the reset vector.
15+
16+
import igraph as ig
17+
import matplotlib.cm as cm
18+
import matplotlib.pyplot as plt
19+
import numpy as np
20+
21+
# %%
22+
# We define a function that plots the graph on a Matplotlib axis, along with
23+
# its personalized PageRank values. The function also generates a
24+
# color bar on the side to see how the values change.
25+
# We use `Matplotlib's Normalize class <https://matplotlib.org/stable/api/_as_gen/matplotlib.colors.Normalize.html>`_
26+
# to set the colors and ensure that our color bar range is correct.
27+
28+
29+
def plot_pagerank(graph: ig.Graph, p_pagerank: list[float]):
30+
"""Plots personalized PageRank values on a grid graph with a colorbar.
31+
32+
Parameters
33+
----------
34+
graph : ig.Graph
35+
graph to plot
36+
p_pagerank : list[float]
37+
calculated personalized PageRank values
38+
"""
39+
# Create the axis for matplotlib
40+
_, ax = plt.subplots(figsize=(8, 8))
41+
42+
# Create a matplotlib colormap
43+
# coolwarm goes from blue (lowest value) to red (highest value)
44+
cmap = cm.coolwarm
45+
46+
# Normalize the PageRank values for colormap
47+
normalized_pagerank = ig.rescale(p_pagerank)
48+
49+
graph.vs["color"] = [cmap(pr) for pr in normalized_pagerank]
50+
graph.vs["size"] = ig.rescale(p_pagerank, (20, 40))
51+
graph.es["color"] = "gray"
52+
graph.es["width"] = 1.5
53+
54+
# Plot the graph
55+
ig.plot(graph, target=ax, layout=graph.layout_grid())
56+
57+
# Add a colorbar
58+
sm = cm.ScalarMappable(norm=plt.Normalize(min(p_pagerank), max(p_pagerank)), cmap=cmap)
59+
plt.colorbar(sm, ax=ax, label="Personalized PageRank")
60+
61+
plt.title("Graph with Personalized PageRank")
62+
plt.axis("equal")
63+
plt.show()
64+
65+
66+
# %%
67+
# First, we generate a graph, e.g. a Lattice Graph, which basically is a ``dim x dim`` grid:
68+
dim = 5
69+
grid_size = (dim, dim) # dim rows, dim columns
70+
g = ig.Graph.Lattice(dim=grid_size, circular=False)
71+
72+
# %%
73+
# Then we initialize the ``reset_vector`` (it's length should be equal to the number of vertices in the graph):
74+
reset_vector = np.zeros(g.vcount())
75+
76+
# %%
77+
# Then we set the nodes to prioritize, for example nodes with indices ``0`` and ``18``:
78+
reset_vector[0] = 1
79+
reset_vector[18] = 0.65
80+
81+
# %%
82+
# Then we calculate the personalized PageRank:
83+
personalized_page_rank = g.personalized_pagerank(damping=0.85, reset=reset_vector)
84+
85+
# %%
86+
# Finally, we plot the graph with the personalized PageRank values:
87+
plot_pagerank(g, personalized_page_rank)
88+
89+
90+
# %%
91+
# Alternatively, we can play around with the ``damping`` parameter:
92+
personalized_page_rank = g.personalized_pagerank(damping=0.45, reset=reset_vector)
93+
94+
# %%
95+
# Here we can see the same plot with the new damping parameter:
96+
plot_pagerank(g, personalized_page_rank)

0 commit comments

Comments
 (0)