Created
March 31, 2024 02:29
-
-
Save daviddamilola/b169c46bcbf4ba4d805ac85c2b1f27ad to your computer and use it in GitHub Desktop.
Page Ranking Algorigthm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import numpy.linalg as la | |
from readonly.PageRankFunctions import * | |
np.set_printoptions(suppress=True) | |
def pageRank(linkMatrix, d) : | |
n = linkMatrix.shape[0] | |
M = d * linkMatrix + (1-d)/n * np.ones([n, n]) # np.ones() is the J matrix, with ones for each entry. | |
r = 100 * np.ones(n) / n # Sets up this vector (n entries of 1/n × 100 each) | |
lastR = r | |
r = M @ r | |
i = 0 | |
while la.norm(lastR - r) > 0.01 : | |
lastR = r | |
r = M @ r | |
i += 1 | |
return r | |
L = np.array([[0, 1/2, 1/3, 0, 0, 0 ], | |
[1/3, 0, 0, 0, 1/2, 0 ], | |
[1/3, 1/2, 0, 1, 0, 1/2 ], | |
[1/3, 0, 1/3, 0, 1/2, 1/2 ], | |
[0, 0, 0, 0, 0, 0 ], | |
[0, 0, 1/3, 0, 0, 0 ]]) | |
eVals, eVecs = la.eig(L) # 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] | |
100 * np.real(r / np.sum(r)) | |
%pylab notebook | |
r = pageRank(generate_internet(100), 0.9) | |
plt.bar(arange(r.shape[0]), r); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment