Page Rank 算法及使用 Python 的实现
PageRank 算法可用于网页。网页是有向图,而众所周知有向图由节点和连接两部分组成。页面就是节点,超链接就是连接,即两个节点之间的连接。
我们可以通过 PageRank 来判断各个页面的重要性,并且很准确。PageRank 的值是介于 0 和 1 之间的概率。
图中各个节点的 PageRank 值取决于与其连接的所有节点的 PageRank 值,且这些节点又周期性地连接到我们想要为其进行排名的那几个节点,因此我们使用收敛迭代法来为 PageRank 分配值。
示例代码
import numpy as np import scipy as sc import pandas as pd from fractions import Fraction def display_format(my_vector, my_decimal): return np.round((my_vector).astype(np.float), decimals=my_decimal) my_dp = Fraction(1,3) Mat = np.matrix([[0,0,1], [Fraction(1,2),0,0], [Fraction(1,2),1,0]]) Ex = np.zeros((3,3)) Ex[:] = my_dp beta = 0.7 Al = beta * Mat + ((1-beta) * Ex) r = np.matrix([my_dp, my_dp, my_dp]) r = np.transpose(r) previous_r = r for i in range(1,100): r = Al * r print (display_format(r,3)) if (previous_r==r).all(): break previous_r = r print ("Final:\n", display_format(r,3)) print ("sum", np.sum(r))
输出
[[0.333] [0.217] [0.45 ]] [[0.415] [0.217] [0.368]] [[0.358] [0.245] [0.397]] [[0.378] [0.225] [0.397]] [[0.378] [0.232] [0.39 ]] [[0.373] [0.232] [0.395]] [[0.376] [0.231] [0.393]] [[0.375] [0.232] [0.393]] [[0.375] [0.231] [0.394]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] [[0.375] [0.231] [0.393]] Final: [[0.375] [0.231] [0.393]] sum 0.9999999999999951
广告