Skip to content

Commit 8a7a1aa

Browse files
committed
added changes requested in PR
1 parent b1b4efa commit 8a7a1aa

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

doc/examples_sphinx-gallery/personalized_pagerank.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,24 @@
88
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.
99
"""
1010

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
1117
import matplotlib.cm as cm
1218
import matplotlib.pyplot as plt
1319
import numpy as np
1420

15-
import igraph as ig
16-
17-
1821
# %%
1922
# We define a function that plots the graph on a Matplotlib axis, along with
2023
# its personalized PageRank values. The function also generates a
2124
# color bar on the side to see how the values change.
2225
# We use `Matplotlib's Normalize class <https://matplotlib.org/stable/api/_as_gen/matplotlib.colors.Normalize.html>`_
2326
# to set the colors and ensure that our color bar range is correct.
27+
28+
2429
def plot_pagerank(graph: ig.Graph, p_pagerank: list[float]):
2530
"""Plots personalized PageRank values on a grid graph with a colorbar.
2631
@@ -54,6 +59,7 @@ def plot_pagerank(graph: ig.Graph, p_pagerank: list[float]):
5459
plt.colorbar(sm, ax=ax, label="Personalized PageRank")
5560

5661
plt.title("Graph with Personalized PageRank")
62+
plt.axis("equal")
5763
plt.show()
5864

5965

@@ -68,14 +74,23 @@ def plot_pagerank(graph: ig.Graph, p_pagerank: list[float]):
6874
reset_vector = np.zeros(g.vcount())
6975

7076
# %%
71-
# Then we set the nodes to prioritize, for example nodes with indices ``0`` and ``6``:
77+
# Then we set the nodes to prioritize, for example nodes with indices ``0`` and ``18``:
7278
reset_vector[0] = 1
73-
reset_vector[6] = 1
79+
reset_vector[18] = 0.65
7480

7581
# %%
7682
# Then we calculate the personalized PageRank:
77-
personalized_page_rank = g.personalized_pagerank(weights=None, damping=0.85, reset=reset_vector)
83+
personalized_page_rank = g.personalized_pagerank(damping=0.85, reset=reset_vector)
7884

7985
# %%
8086
# Finally, we plot the graph with the personalized PageRank values:
8187
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)