Python-PageRank calculation & social graph visualization that even monkeys can do

I tried to easily create and implement a virtual environment with Vagrant + VirtualBox + Python + library.

I was dealing with social graphs and matrices with ruby, and it was often difficult to handle, so when I tried matrix calculation and graph visualization with Python, it was insanely easy and practical, so I summarized it.

About the Python library used

  1. Scipy scipy, the package has a variety of toolboxes for common problems in scientific computing. The range of application is different for each submodule. The range of application is, for example, complementation, integration, optimization, image processing, statistics, special functions, etc. scipy is compared to other standard scientific and technological computing libraries such as GSL (GNU Scientific Library for C and C ++) and Matlab's Toolbox. scipy is the core package of scientific computing routines in Python; it handles numpy arrays efficiently, so numpy and scipy work closely together.

  2. Numpy NumPy is an extension of the Python programming language that provides support for large multidimensional arrays and matrices, and a large high-level library of mathematical functions for manipulating them. NumPy is often used in scientific computing along with SciPy. In addition, since matrix operations can be performed at high speed, OpenCV (Computer Vision Library) now provides a Python interface using NumPy.

  3. NetworkX NetworkX is a library for network analysis and visualization of Python. The network referred to here is not limited to the actual communication network (model), but has a structure in which a value called weight is assigned to the sides of the graph. If you don't think about the weight, you can handle ordinary graphs because you can assume that all sides have the same weight. In other words, you can use NetworkX to do various things with your graph.

  4. matplotlib Matplotlib is a graph drawing library for the programming language Python and its scientific computing library NumPy. It provides an object-oriented API and has the ability to draw various types of graphs. Mainly 2D plots can be drawn, but 3D plot functions have also been added. You can save the drawn graph as an image in various formats (including various vector image formats), or you can incorporate the graph drawing function into an application made by a general GUI toolkit such as wxPython, Qt, GTK. .. It also has an interface called pylab that provides something like a MATLAB interaction environment. Matplotlib is distributed under the BSD-style license. (via http://ja.wikipedia.org/wiki/Matplotlib)


Environment

1. Create a virtual machine with Vagrant + VirtualBox and ssh login

http://qiita.com/gyhino@github/items/8c10249019cf1e9fce34

2. Python installation

3. Install pip

http://pip.readthedocs.org/en/latest/installing.html

4. Install what you need for Python

http://d.hatena.ne.jp/shinriyo/20120623/p1

5. Install Python library

sudo pip install numpy
sudo easy_install scipy
#sudo pip install scipy got an error so easy_I used install
sudo easy_install matplotlib
sudo pip install networkx

## What I tried ### 1. Calculation of page rank that even monkeys can do ### 2. Visualization of social graph that can be done even when sleeping --
### 1. Calculation of page rank that even monkeys can do The file that calculates the page rank with a simple graph and the execution method are described.

pagerank.py



# -*- coding: utf-8 -*-
import networkx as nx

#Create an instance of a directed graph
g = nx.DiGraph()

#Add a node * People often become nodes in the social graph
g.add_node(1)
g.add_node(2)
g.add_node(3)                                                                                                                     
g.add_node(4)
g.add_node(5)
g.add_node(6)
#Dare to make it easy to understand

#Add arrows between nodes * For social graphs, friendships, follow-ups, and likes! Such
g.add_edge(1,2)
g.add_edge(1,3)
g.add_edge(1,4)
g.add_edge(2,3)
g.add_edge(3,4)
g.add_edge(3,5)
g.add_edge(2,6)
g.add_edge(5,6)
g.add_edge(1,6)
#Dare to make it easy to understand

#Calculation of pagerank value
pr=nx.pagerank(g,alpha=0.85)

#Calculation of pagerank value(Use numpy)
prn=nx.pagerank_numpy(g,alpha=0.85)

#Calculation of pagerank value(Use scipy)
prc=nx.pagerank_scipy(g,alpha=0.85)

#Calculation result display
print("-----pagerank-----")
print(pr)

print("-----pagerank(numpy)-----")
print(prn)

print("-----pagerank(scipy)-----")
print(prc)

$ python pagerank.py

If you execute, you will get the following result.

#normal
{1: 0.09239221032272713, 2: 0.11202546652062978, 3: 0.1596362278548462, 4: 0.1798708491907905, 5: 0.1602375929928878, 6: 0.29583765311811816}

#numpy
{1: 0.09239208559289437, 2: 0.11202540378138424, 3: 0.15963620038847254, 4: 0.1798707889464852, 5: 0.16023747075799505, 6: 0.2958380505327687}

#scipy
{1: 0.09239221032272715, 2: 0.11202546652062981, 3: 0.15963622785484624, 4: 0.17987084919079052, 5: 0.16023759299288784, 6: 0.29583765311811827}

Obviously, the answer is almost the same.

--

2. Visualization of social graph that can be done even when sleeping

social_graph_visualization.py



# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
from matplotlib import animation
import networkx as nx
import random

#network
g = nx.Graph()
def get_fig(node_number):                                                                                                         
    g.add_node(node_number, Position=(random.randrange(0, 100), random.randrange(0, 100)))
    g.add_edge(node_number, random.choice(g.nodes()))
    nx.draw(g, pos=nx.get_node_attributes(g,'Position'))

fig = plt.figure(figsize=(10,8))

anim = animation.FuncAnimation(fig, get_fig, frames=100)
anim.save('graph_gifani.gif', writer='imagemagick', fps=10);

$ python social_graph_visualization.py

When you execute, the following gif ani should be saved in the folder.

GraphGifAnimation

--

Summary

It's funny! It ’s easy! !!

I've been wanting to make APIs and apps with Python for a while, so I'll make something interesting in the near future!

Recommended Posts

Python-PageRank calculation & social graph visualization that even monkeys can do
IOS application development with Python (kivy) that even monkeys can do
Automatic browser operation that even beginners can do
An introduction to Python that even monkeys can understand (Part 3)
An introduction to Python that even monkeys can understand (Part 2)