Immediate page rank calculation (with commentary on all lines)

Intense and easy. I think it's important to know that things that seem difficult can actually be fierce and easy.

Install what you need

Since it's a big deal, I also installed the surrounding libraries. I was able to install it without any particular addiction.

Preparation


brew update #I don't care if homebrew is rusty
brew install gfortran #I think it was necessary to put in scipy
easy_install pip #Installation of python library management tools
pip install numpy #Installation of libraries for scientific calculations
pip install scipy #Installation of libraries for advanced scientific calculations
pip install networkx #Installation of graph processing library

Code to output page rank

Output page rank


# -*- coding: utf-8 -*-
import networkx as nx#Import the networkx you just put in

g = nx.DiGraph()#Create an object to represent a directed graph
g.add_edge("user1","user3")#Vote from user1 to user3
g.add_edge("user2","user3")#Vote from user2 to user3
g.add_edge("user2","user1")#Vote from user2 to user1
g.add_edge("user3","user1")#Vote from user3 to user1
g.add_edge("user4","user2")#Vote from user4 to user2
g.add_edge("user1","user4")#Vote from user1 to user4
print nx.pagerank_scipy(g,alpha=0.85)#Damping factor 0.Output page rank as 85
#{'user4': 0.18597431523080427, 'user2': 0.19557861112876537, 'user3': 0.2690949378990845, 'user1': 0.3493521357413458}

Recommended Posts

Immediate page rank calculation (with commentary on all lines)
Perform path calculation on 2D grid with Networkx