0

The expression in python is: np.ones(len(A)) * (1 - d) / len(A) + d * A.T.dot(P)

What does this line mean?

Below is the complete code where this expression exist. This code is part of Page rank algorithm.

def pagerank(A, eps=0.0001, d=0.85):

    P = np.ones(len(A)) / len(A)

    while True:

        new_P = np.ones(len(A)) * (1 - d) / len(A) + d * A.T.dot(P)

        delta = abs(new_P - P).sum()

        if delta <= eps:

            return new_P

        P = new_P

 

results = pagerank(A)

 

print("Results:", results) # [ 0.13933698,  0.09044235,  0.1300934 ,  0.13148714,  0.08116465, 0.1305122 ,  0.09427366,  0.085402  ,  0.02301397,  0.09427366]

print(sum(results)) # 1.0

print([item[0] for item in sorted(enumerate(results), key=lambda item: -item[1])]) # [0, 3, 5, 2, 6, 9, 1, 7, 4, 8]

 

Michael
  • 810
  • 6
  • 18
akshit bhatia
  • 573
  • 6
  • 22
  • See [How to deal with questions of the type “I don't understand how this code works”?](https://meta.stackoverflow.com/questions/278797/how-to-deal-with-questions-of-the-type-i-dont-understand-how-this-code-works) on [meta] -- "explain how this code works" questions need to be **extremely** specific about exactly what parts of an operation you do and don't understand, and should ideally focus on a specific language or library operation. – Charles Duffy Dec 04 '18 at 04:23
  • ...questions that are about how *algorithms* work, vs language/library code, may be a better fit on other parts of the Stack Exchange network -- we stay very concrete here on SO; other parts of SE are a little more forgiving. Whereas if your real question is more about the math underlying an operation than a specific aspect of the code or algorithm implementing that math... well, there's a whole different SE site for that. – Charles Duffy Dec 04 '18 at 04:26
  • I meant to ask what does the np.ones(len(A))*(1-d)/len(A) refers to? – akshit bhatia Dec 04 '18 at 06:02
  • if we have np.ones()*4, it means that the array would be like: {1,1,1,1} but what the other way mean? Does it qualify to be a Stack overflow question? – akshit bhatia Dec 04 '18 at 06:03
  • Is the question what `len()` means? What passing an argument to `np.ones()` does? Something else? – Charles Duffy Dec 04 '18 at 14:26
  • @akshit: Do you have any idea on PageRank algorithm? – Nam Le Oct 16 '19 at 02:11

0 Answers0