Algorithm PageRank with eigenvalues and eigenvector

A python implementation with dampling factor for PageRank algorithm.

# PACKAGE
# Here are the imports again, just in case you need them.
# There is no need to edit or submit this cell.
import numpy as np
import numpy.linalg as la
import matplotlib as plt
np.set_printoptions(suppress=True)

# GRADED FUNCTION
# Complete this function to provide the PageRank for an arbitrarily sized internet.
# I.e. the principal eigenvector of the damped system, using the power iteration method.
# (Normalisation doesn't matter here)
# The functions inputs are the linkMatrix, and d the damping parameter - as defined in this worksheet.
# (The damping parameter, d, will be set by the function - no need to set this yourself.)
def pageRank(linkMatrix, d) :
    n = linkMatrix.shape[0]
    
    # create matrix for dampling factor.
    M = d * linkMatrix + (1-d)/n * np.ones([n, n])
    r = 100 * np.ones(n) / n # Sets up this vector (6 entries of 1/6 × 100 each)
    lastR = r
    r = M @ r
    i = 0
    while la.norm(lastR - r) > 0.01 :
        lastR = r
        r = M @ r
        i += 1
    print(str(i) + " iterations to convergence.")
    
    return r

# Do note, this is calculating the eigenvalues of the link matrix, L,
# without any damping. It may give different results that your pageRank function.
# If you wish, you could modify this cell to include damping.
# (There is no credit for this though)
def  pageRankByEigenTheory(linkMatrix, d):
	# create matrix for dampling factor.
	n = linkMatrix.shape[0]
	M = d * linkMatrix + (1-d)/n * np.ones([n, n])
	eVals, eVecs = la.eig(M)	# Gets the eigenvalues and vectors
	order = np.absolute(eVals).argsort()[::-1] # Orders them by their eigenvalues
	eVals = eVals[order]
	eVecs = eVecs[:,order]

	r = eVecs[:, 0]
	r = 100 * np.real(r / np.sum(r))
	return r


def sanity_check():
	# matrix x vector
	L = np.array([[ 1. ,  0. ,  0. ,  0.1,  0.1,  0. ,  0. ,  0. ,  0.1,  0.1],
       [ 0. ,  0. ,  0. ,  0.1,  0.1,  0. ,  0. ,  0. ,  0.1,  0.1],
       [ 0. ,  0. ,  0. ,  0.1,  0.1,  0. ,  0. ,  0. ,  0.1,  0.1],
       [ 0. ,  0.5,  0. ,  0.1,  0.1,  0. ,  0. ,  0. ,  0.1,  0.1],
       [ 0. ,  0. ,  1. ,  0.1,  0.1,  0.5,  0. ,  0. ,  0.1,  0.1],
       [ 0. ,  0. ,  0. ,  0.1,  0.1,  0.5,  0. ,  0. ,  0.1,  0.1],
       [ 0. ,  0. ,  0. ,  0.1,  0.1,  0. ,  0. ,  0. ,  0.1,  0.1],
       [ 0. ,  0. ,  0. ,  0.1,  0.1,  0. ,  1. ,  0. ,  0.1,  0.1],
       [ 0. ,  0. ,  0. ,  0.1,  0.1,  0. ,  0. ,  1. ,  0.1,  0.1],
       [ 0. ,  0.5,  0. ,  0.1,  0.1,  0. ,  0. ,  0. ,  0.1,  0.1]])

	print(pageRank(L, 0.5))
	print(pageRankByEigenTheory(L, 0.5))

if __name__ == "__main__":
    sanity_check()


Result:

8 iterations to convergence.
[14.36562648  7.18596744  7.18596744  8.98255483 13.17473197  9.58155719
  7.18596744 10.77914222 12.57593017  8.98255483]
[14.37125749  7.18562874  7.18562874  8.98203593 13.17365269  9.58083832
  7.18562874 10.77844311 12.5748503   8.98203593]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值