forked from SleepyBag/Statistical-Learning-Methods
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PageRank.py
45 lines (41 loc) · 1.2 KB
/
PageRank.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import numpy as np
import sys
import os
from pathlib import Path
sys.path.append(str(Path(os.path.abspath(__file__)).parent.parent))
from utils import *
def pageRank(graph, d, max_iteration=1000, epsilon=1e-8):
"""
given a n * n link graph
graph[i, j] = 1 means that there is a link from i to j
d is the proportion of neighbours in the definition of page rank
return the probablisitic for a user visiting each page
"""
n, _ = graph.shape
p = np.ones(n) / n
graph /= (graph.sum(axis=-1, keepdims=True) + epsilon)
graph = graph.T
for i in range(max_iteration):
pre_p = p
p = d * graph @ p + (1 - d) / n
if max(p - pre_p) < epsilon:
break
return p
if __name__ == '__main__':
def demonstrate(graph, d, desc):
print(desc)
p = pageRank(graph, d=d)
print('The probability of each node is', np.round(p, 2))
graph = np.array(
[[0, 1, 1, 1],
[1, 0, 0, 1],
[0, 0, 1, 0],
[0, 1, 1, 0]]
).astype(float)
demonstrate(graph, .8, 'Example 1')
graph = np.array(
[[0, 1, 1],
[0, 0, 1],
[1, 0, 0]]
).astype(float)
demonstrate(graph, .85, 'Example 2')