Skip to content

Commit 1f94d75

Browse files
committed
added personalized PageRank example
1 parent 8322002 commit 1f94d75

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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.GraphBase.personalized_pagerank` method, and demonstrate the effects on a grid graph.
9+
"""
10+
11+
import matplotlib.cm as cm
12+
import matplotlib.pyplot as plt
13+
import numpy as np
14+
15+
import igraph as ig
16+
17+
18+
# %%
19+
# We define a function that plots the graph on a Matplotlib axis, along with
20+
# its personalized PageRank values. The function also generates a
21+
# color bar on the side to see how the values change.
22+
# We use `Matplotlib's Normalize class <https://matplotlib.org/stable/api/_as_gen/matplotlib.colors.Normalize.html>`_
23+
# to set the colors and ensure that our color bar range is correct.
24+
def plot_pagerank(graph: ig.Graph, p_pagerank: list[float]):
25+
"""Plots personalized PageRank values on a grid graph with a colorbar.
26+
27+
Parameters
28+
----------
29+
graph : ig.Graph
30+
graph to plot
31+
p_pagerank : list[float]
32+
calculated personalized PageRank values
33+
"""
34+
# Create the axis for matplotlib
35+
_, ax = plt.subplots(figsize=(8, 8))
36+
37+
# Create a matplotlib colormap
38+
# coolwarm goes from blue (lowest value) to red (highest value)
39+
cmap = cm.coolwarm
40+
41+
# Normalize the PageRank values for colormap
42+
normalized_pagerank = ig.rescale(p_pagerank)
43+
44+
graph.vs["color"] = [cmap(pr) for pr in normalized_pagerank]
45+
graph.vs["size"] = ig.rescale(p_pagerank, (20, 40))
46+
graph.es["color"] = "gray"
47+
graph.es["width"] = 1.5
48+
49+
# Plot the graph
50+
ig.plot(graph, target=ax, layout=graph.layout_grid())
51+
52+
# Add a colorbar
53+
sm = cm.ScalarMappable(norm=plt.Normalize(min(p_pagerank), max(p_pagerank)), cmap=cmap)
54+
plt.colorbar(sm, ax=ax, label="Personalized PageRank")
55+
56+
plt.title("Graph with Personalized PageRank")
57+
plt.show()
58+
59+
60+
# %%
61+
# First, we generate a graph, e.g. a Lattice Graph, which basically is a ``dim x dim`` grid:
62+
dim = 5
63+
grid_size = (dim, dim) # dim rows, dim columns
64+
g = ig.Graph.Lattice(dim=grid_size, circular=False)
65+
66+
# %%
67+
# Then we initialize the ``reset_vector`` (it's length should be equal to the number of vertices in the graph):
68+
reset_vector = np.zeros(g.vcount())
69+
70+
# %%
71+
# Then we set the nodes to prioritize, for example nodes with indices ``0`` and ``6``:
72+
reset_vector[0] = 1
73+
reset_vector[6] = 1
74+
75+
# %%
76+
# Then we calculate the personalized PageRank:
77+
personalized_page_rank = g.personalized_pagerank(weights=None, damping=0.85, reset=reset_vector)
78+
79+
# %%
80+
# Finally, we plot the graph with the personalized PageRank values:
81+
plot_pagerank(g, personalized_page_rank)

0 commit comments

Comments
 (0)