<jupyter_start><jupyter_text>This is our micro-internet. Imagine we have 100 Procrastinating Pats on our micro-internet, each viewing a single website at a time. Each minute the Pats follow a link on their website to another site on the micro-internet. After a while, the websites that are most linked to will have more Pats visiting them, and in the long run, each minute for every Pat that leaves a website, another will enter keeping the total numbers of Pats on each website constant. The PageRank is simply the ranking of websites by how many Pats they have on them at the end of this process.<jupyter_code>L = np.array([[0, 1/3, 1/3, 1/3],[1/2 ,0, 0, 1/2],[0, 0, 0, 1],[0, 1/2 , 1/2, 0]]).T
L
eVals, eVecs = la.eig(L) # Gets the eigenvalues and vectors
eVals #we only care about the principal eigenvector (the one with the largest eigenvalue, which will be 1 in this case)
order = np.absolute(eVals).argsort()[::-1] # Orders them by their eigenvalues
eVals = eVals[order]
eVecs = eVecs[:,order]
eVals
eVecs
r = eVecs[:, 0] # Sets r to be the principal eigenvector
100 * np.real(r / np.sum(r)) # Make this eigenvector sum to one, then multiply by 100 Procrastinating Pats<jupyter_output><empty_output><jupyter_text>We can see from this list, the number of Procrastinating Pats that we expect to find on each website after long times. Putting them in order of popularity (based on this metric), the PageRank of this micro-internet is:    D => 40 pats    C & B => 24 each    A => 12 In principle, we could use a linear algebra library, as above, to calculate the eigenvalues and vectors. And this would work for a small system. But this gets unmanagable for large systems. And since we only care about the principal eigenvector (the one with the largest eigenvalue, which will be 1 in this case), we can use the power iteration method which will scale better, and is faster for large systems.<jupyter_code>d = 0.5
n  = 4
M = d * L + (1-d)/n * np.ones([n, n])
M
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.")
r
np.sum(r)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0880107768298159, 'fraction_numerical': 0.02694207453973956, 'mean_word_length': 3.9183222958057393, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '55906', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 746, 'n_tokens_neox': 688, 'n_words': 349, 'path': 'PageRank-LA.ipynb', 'repo_name': 'vinpala/DL_blogs_PageRank', 'size': '2227'}