Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 394ee38

Browse files
Add PageRank file
1 parent 5555a2b commit 394ee38

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed

‎Page_Rank/Page_Rank.ipynb‎

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"### PageRank算法\n",
8+
"以下图所示的有向图为例,计算每个结点的PR:\n",
9+
"<img style=\"float: center;\" src=\"directed_graph.png\" width=\"20%\">"
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": 26,
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"import numpy as np\n",
19+
"\n",
20+
"\n",
21+
"n = 7 #有向图中一共有7个节点\n",
22+
"d = 0.85 #阻尼因子根据经验值确定,这里我们随意给一个值\n",
23+
"M = np.array([[0, 1/4, 1/3, 0, 0, 1/2, 0],\n",
24+
" [1/4, 0, 0, 1/5, 0, 0, 0],\n",
25+
" [0, 1/4, 0, 1/5, 1/4, 0, 0],\n",
26+
" [0, 0, 1/3, 0, 1/4, 0, 0],\n",
27+
" [1/4, 0, 0, 1/5, 0, 0, 0],\n",
28+
" [1/4, 1/4, 0, 1/5, 1/4, 0, 0],\n",
29+
" [1/4, 1/4, 1/3, 1/5, 1/4, 1/2, 0]]) #根据有向图中各节点的连接情况写出转移矩阵\n",
30+
"R0 = np.full((7, 1), 1/7) #设置初始向量R0,R0是一个7*1的列向量,因为有7个节点,我们把R0的每一个值都设为1/7\n",
31+
"eps = 0.000001 #设置计算精度"
32+
]
33+
},
34+
{
35+
"cell_type": "markdown",
36+
"metadata": {},
37+
"source": [
38+
"### 1. PageRank的迭代算法"
39+
]
40+
},
41+
{
42+
"cell_type": "code",
43+
"execution_count": 27,
44+
"metadata": {},
45+
"outputs": [],
46+
"source": [
47+
"t = 0 #用来累计迭代次数\n",
48+
"R = R0 #对R向量进行初始化\n",
49+
"judge = False #用来判断是否继续迭代\n",
50+
"while not judge:\n",
51+
" next_R = d * np.matmul(M, R) + (1 - d) / n * np.ones((7, 1)) #计算新的R向量\n",
52+
" diff = np.linalg.norm(R - next_R) #计算新的R向量与之前的R向量之间的距离,这里采用的是欧氏距离\n",
53+
" if diff < eps: #若两向量之间的距离足够小\n",
54+
" judge = True #则停止迭代\n",
55+
" R = next_R #更新R向量\n",
56+
" t += 1 #迭代次数加一\n",
57+
"R = R / np.sum(R) #对R向量进行规范化,保证其总和为1,表示各节点的概率分布"
58+
]
59+
},
60+
{
61+
"cell_type": "code",
62+
"execution_count": 28,
63+
"metadata": {},
64+
"outputs": [
65+
{
66+
"name": "stdout",
67+
"output_type": "stream",
68+
"text": [
69+
"迭代次数: 24\n",
70+
"PageRank: \n",
71+
" [[0.17030305]\n",
72+
" [0.10568394]\n",
73+
" [0.11441021]\n",
74+
" [0.10629792]\n",
75+
" [0.10568394]\n",
76+
" [0.15059975]\n",
77+
" [0.24702119]]\n"
78+
]
79+
}
80+
],
81+
"source": [
82+
"print('迭代次数:', t)\n",
83+
"print('PageRank: \\n', R)"
84+
]
85+
},
86+
{
87+
"cell_type": "markdown",
88+
"metadata": {},
89+
"source": [
90+
"### 1. PageRank的幂法"
91+
]
92+
},
93+
{
94+
"cell_type": "code",
95+
"execution_count": 29,
96+
"metadata": {},
97+
"outputs": [],
98+
"source": [
99+
"t = 0 #用来累计迭代次数\n",
100+
"x = R0 #对x向量进行初始化\n",
101+
"judge = False #用来判断是否继续迭代\n",
102+
"A = d * M + (1 - d) / n * np.eye(n) #计算A矩阵,其中np.eye(n)用来创建n阶单位阵E\n",
103+
"while not judge:\n",
104+
" next_y = np.matmul(A, x) #计算新的y向量\n",
105+
" next_x = next_y / np.linalg.norm(next_y) #对新的y向量规范化得到新的x向量\n",
106+
" diff = np.linalg.norm(x - next_x) #计算新的x向量与之前的x向量之间的距离,这里采用的是欧氏距离\n",
107+
" if diff < eps: #若两向量之间的距离足够小\n",
108+
" judge = True #则停止迭代\n",
109+
" R = x #得到R向量\n",
110+
" x = next_x #更新x向量\n",
111+
" t += 1 #迭代次数加一\n",
112+
"R = R / np.sum(R) #对R向量进行规范化,保证其总和为1,表示各节点的概率分布"
113+
]
114+
},
115+
{
116+
"cell_type": "code",
117+
"execution_count": 30,
118+
"metadata": {},
119+
"outputs": [
120+
{
121+
"name": "stdout",
122+
"output_type": "stream",
123+
"text": [
124+
"迭代次数: 25\n",
125+
"PageRank: \n",
126+
" [[0.18860772]\n",
127+
" [0.09038084]\n",
128+
" [0.0875305 ]\n",
129+
" [0.07523049]\n",
130+
" [0.09038084]\n",
131+
" [0.15604764]\n",
132+
" [0.31182196]]\n"
133+
]
134+
}
135+
],
136+
"source": [
137+
"print('迭代次数:', t)\n",
138+
"print('PageRank: \\n', R)"
139+
]
140+
},
141+
{
142+
"cell_type": "code",
143+
"execution_count": null,
144+
"metadata": {},
145+
"outputs": [],
146+
"source": []
147+
}
148+
],
149+
"metadata": {
150+
"kernelspec": {
151+
"display_name": "Python 3",
152+
"language": "python",
153+
"name": "python3"
154+
},
155+
"language_info": {
156+
"codemirror_mode": {
157+
"name": "ipython",
158+
"version": 3
159+
},
160+
"file_extension": ".py",
161+
"mimetype": "text/x-python",
162+
"name": "python",
163+
"nbconvert_exporter": "python",
164+
"pygments_lexer": "ipython3",
165+
"version": "3.7.3"
166+
}
167+
},
168+
"nbformat": 4,
169+
"nbformat_minor": 2
170+
}

‎Page_Rank/directed_graph.png‎

48.5 KB
Loading[フレーム]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /