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]