You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
# 9.3 动手实现PageRank
理解了PageRank算法原理之后, 想要动手实现PageRank算法其实不难。代码如下:
```python
from numpy import *
# 构造转移矩阵, 其中a为有向图的邻接矩阵
def graphMove ( a ):
b = transpose ( a ) # b为a的转置矩阵
c = zeros (( a . shape ), dtype = float )
for i in range ( a . shape [ 0 ]):
for j in range ( a . shape [ 1 ]):
c [ i ][ j ] = a [ i ][ j ] / ( b [ j ] . sum ()) # 完成初始化分配
return c
# 初始化V0
def firstPr ( c ):
pr = zeros (( c . shape [ 0 ], 1 ), dtype = float )
for i in range ( c . shape [ 0 ]):
pr [ i ] = float ( 1 ) / c . shape [ 0 ]
return pr
# 计算pageRank值
def pageRank ( p , m , v ):
# 判断pr矩阵是否收敛,(v == p*dot(m,v) + (1-p)*v).all()判断前后的pr矩阵是否相等, 若相等则停止循环
while (( v == p * dot ( m , v ) + (
1 - p ) * v ) . all () == False ):
v = p * dot ( m , v ) + ( 1 - p ) * v
return v
if __name__ == "__main__" :
# 网页的邻接矩阵
a = array ([[ 0 , 1 , 1 , 0 ],
[ 1 , 0 , 0 , 1 ],
[ 1 , 0 , 0 , 1 ],
[ 1 , 1 , 0 , 0 ]], dtype = float )
M = graphMove ( a )
pr = firstPr ( M )
# 上网者查看当前网页的概率
p = 0.8
# 计算V
print ( pageRank ( p , M , pr ))
```