Kung Fu Kung Fu

Fujian White Crane Kung Fu

Zope Zope

What I have and am doing with Zope

Photos Photos

Photoalbum, both old and new.

Receptsamlingen Receptsamlingen

In Swedish only. About my "Collection of Recipes" website.

Contact me Contact me

My contact details and how to contact me.

  Mobile version of this page Mobile version of this page


 

Google PageRank algorithm in Python

pagerank, pageranker, pagerank algorithm

21st of March 2004

There are many articles on the net about how the PageRank algorithm works that all copy from the original paper written by the very founders of Google Larry Page and Sergey Brin. Google itself also has a very good article that explain it with no formulas or numerical explanations. Basically PageRank is like social networks. If you're mentioned by someone important, your importance increases and the people you mention gets upped as well.

We recently had a coursework in discrete mathematics to calculate PageRank values for all web pages in a web matrix. To be able to do this you have to do many simplifications and you're limited in terms of complexity to keep it possible to do "by hand". I wrote a little program that calculates the PageRank for any web with no simplifications. The outcome is that I can quickly calculate the PageRank values for each page.

Here's how to use it:

 from PageRank import PageRanker
 web = ((0, 1, 0, 0),
        (0, 0, 1, 0),
        (0, 0, 0, 1),
        (1, 0, 0, 0))

 pr = PageRanker(0.85, web)
 pr.improve_guess(100)
 print pr.getPageRank()

Think of the entries in the matrix as A to D along every row and which page it has a link to along the column. In the above example it means that A has a link to B, B as a link to C, C has a link to D, D has a link to A. Graph 1

The PageRank values when you run 100 iterations with no random jumps is:

 [ 0.25  0.25  0.25  0.25 ]

Pretty obvious isn't it. One complication with the PageRank algorithm is that even if every page has an outgoing link, you don't always cover everything by just following links. That's why to sometimes need to random start over again from a randomly selected webpage. This is we we use 8.5 in the above example. That qualitativly means that there's a 15% chance that you randomly start on a random webpage and iterate from there.

Let's have a "more complex" web model:

 web = ((0, 1, 0, 0),
        (0, 0, 1, 0),
        (0, 1, 0, 1),
        (1, 1, 0, 0))

Graph 2

Running the algorithm again we find:

 [ 0.14285725  0.28571447  0.28571419  0.2857141 ]

Notice how page B has the same PageRank as C and D even though page B has two links coming in to it. This is because it spreads it popularity to other pages. It also matters that the initial guess is that every page is equal initially.

Enough said, download the script yourself and make sure you have Python and the numarray Python module installed.


Comment

21 comments so far
Peter - 22nd March 2004  [«« Reply to this]
Here's another interesting and more applicative pagerank calculator:
http://www.top25web.com/pagerank.php
Anonymous - 30th March 2004  [«« Reply to this]
bug: the first matrix has two edges for C
Peter - 30th March 2004   [«« Reply to this]
Cheers. Fixed it.
George Hotelling - 21st April 2004  [«« Reply to this]
Does Google's patent prevent this from being used in site search engines, like as an addon for http://www.zope.org/Members/ajung/TextIndexNG/ ?
Peter - 21st April 2004   [«« Reply to this]
I don't know actually. It's such a smart thing that makes you wonder how other search engines would do for sorting.
Anonymous - 13th May 2004  [«« Reply to this]
Here's another pagerank calculator:
http://www.pagerank.net/pagerank-checker/
Michelle - 12th June 2004  [«« Reply to this]
The most accurate PageRank Checker (ofcourse, for people without ability to use Google Toolbar) Tool I found is this one: http://www.freelance-help.com/google-ranking-report/page-rank.php
Anonymous - 20th June 2004  [«« Reply to this]
Here you can get more information about the official Google PageRank checksum (parameter "ch" in the Google Toolbar), needed for requests of the PageRank values. You can test this script with your own domains and can even download it.

http://pagerank-checksum.homelinux.com/
Marry - 15th September 2005  [«« Reply to this]
Hi there

I have been given the task of getting links for our websites that have good page rank on the links directories.
In addition we have many categories so your site will be place on an appropriate page.

If you would like to trade links please send me your website details.

P.S.: I got your e-mail publicly listed on your webpage . Our apologies if you do not wish to take part in a link exchange.

Best Regards,
Marry Tailor
Peter Bengtsson - 15th September 2005   [«« Reply to this]
no thanks. I'd rather not have a link exchange with an unrelated site. I'm sure google can notice what is going on an suspect spam.

If you're going to have a link exchange, do that with sites that mention similar keywords.
asr - 25th March 2006  [«« Reply to this]
seems to be wrong( to me ).
in _improve shouldn't
invariantmatrix multiply with _linkedmatrix and then add itself to _randmatrix ??
caykoylu - 29th January 2007  [«« Reply to this]
http://www.googlepagerankchecker.com
sandorf - 19th March 2007  [«« Reply to this]
Bug: the program doesn't work when any of the web pages has no outgoing links, i.e. all-zero row in webmatrix.
Mahmoud - 28th March 2007   [«« Reply to this]
Dear all
please tell me the no of iteration can be get the accurate result
and i want the example of use the eginevalue to determine the Page Rank
thanks
Mahmoud - 28th March 2007   [«« Reply to this]
dear all
i want reaal data to compute Page Rank
Mahmoud - 28th March 2007  [«« Reply to this]
I want Real Data to compute Page Rank
Wing Wong - 20th May 2007  [«« Reply to this]
Sweet. Got a friend who's doing some python coding at http://www.wtwconsulting.com/ . Guess it'll be a good time to ring him up and maybe setup a test. I tried to download it to run against my site at http://wingedpower.com/ , but it doesn't work for me. :(
termo - 3rd January 2008  [«« Reply to this]
Bug: in the second example B should have better PageRank than the others.
termo - 4th January 2008  [«« Reply to this]
I think there is a bug because in the second example B should have better rank than ther others.
Here is an implementation in pure python without using numaarray and the complete N*N matrix. Enjoy!
http://mechanicalpoetry.blogspot.com/2008/01/scalable-pagerank-in-pure-python-76.html
hakan - 15th June 2008  [«« Reply to this]
thank you

http://www.pyromus.com
michael - 23rd June 2008  [«« Reply to this]
Interesting article.

http://www.birminghamfriends.co.uk
 
Name:
Email:
hide my email address.

Your email address will be encoded to prevent email-extraction spiders from reading it so you won't get spammed if you decide to show your email address.