0

I have created an adjacency matrix in excel spreadsheet, and now I need to calculate the page rank of each page by using teleportation constant T=0.15 and using the following formula:

PR(W)=T/N+(1-T)(PR(W1)/O(W1)+PR(W2)/O(W2)+...PR(Wn)/O(WN))

I believe the pseudo-code to perform this action should be something like this:

x=formula
for i in range(len(matrix):
 if i=1, then i==x
   else
    return 0
print(i?)

I know it is far from being perfect, and I am still trying to work this through.

I also suppose that I need firstly declare create a separate program code for the formula itself and declare its variables.

Would this be a a satisfying solution to implement this idea with Python or R? Or there is a better method of calculating page rank?

I came across various possible solution in the web, but they weren't very helpful in relation with my particular case.

v0id
  • 29
  • 5

1 Answers1

0

If your data isn't especially big, and I assume it isn't, since it's in an excel spreadsheet, you can simply compute the first eigenvector of a prepared matrix A. Let your adjacency matrix be named M. The code in R is as follows:

n = ncol(M)
B = matrix(1/n, n, n) # the teleportation matrix
A = 0.85 * M + 0.15 * B
ranks = eigen(A)$vectors[1] # your PageRanks

If your matrix is bigger, then you can use for example function page_rank from package igraph

  • This looks nice, thank you very much! Although, I have some error messages: n = ncol(m) B = matrix(1/n, n, n) # the teleportation matrix > A = 0.85*m+0.15*B Warning messages: 1: In Ops.factor(left, right) : ‘*’ not meaningful for factors 2: In Ops.factor(left, right) : ‘*’ not meaningful for factors ranks = eigen(A)$vectors[1] # your PageRanks Error in eigen(A) : non-square matrix in 'eigen' I don't have much experience with R, so I am struggling to find an answer to this particular case. The matrix is 500x500 whit 1 and 0. – v0id Dec 12 '16 at 00:37
  • please ignore my last comment, that was my own mistake regarding data now I have this output: ` ranks = eigen(A)$vectors[500] # your PageRanks > n = ncol(R) > B = matrix(1/n, n, n) # the teleportation matrix > A = 0.85 * R + 0.15 * B > ranks = eigen(A)$vectors[500] # your PageRanks > print(ranks) [1] -0.0002985959+0i` that actually works, but I need a PageRanks for each page, ideally it would look like: `Iteration ... ` or as a table As I understand, $vectors[1] is an array which stores the general result and is represented in ranks – v0id Dec 12 '16 at 03:06