|
|
|
|
1.首先明確igraph能做什麼,爲何使用。 | |
2.安裝。 | |
3.測試。 | |
|
|
Installing collected packages: python-igraph | |
Running setup.py install for python-igraph | |
Successfully installed python-igraph-0.7.1-4 | |
vonzhou@CHOWN:~$ python | |
Python 2.7.3 (default, Dec 18 2014, 19:03:52) | |
[GCC 4.6.3] on linux2 | |
Type "help", "copyright", "credits" or "license" for more information. | |
>>> import igraph | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
ImportError: No module named igraph | |
|
|
主要是igraph安裝在了其他版本下面,SO下面這樣。 | |
vonzhou@CHOWN:~$ python3 | |
Python 3.2.3 (default, Feb 27 2014, 21:33:50) | |
[GCC 4.6.3] on linux2 | |
Type "help", "copyright", "credits" or "license" for more information. | |
>>> igraph | |
<module 'igraph' from '/usr/local/lib/python3.2/dist-packages/igraph/__init__.py'> | |
>>> from igraph import * | |
>>> g=Graph(1) | |
>>> g | |
<igraph.Graph object at 0x9e4cf2c> | |
>>> print(g) | |
IGRAPH U--- 1 0 -- | |
>>> g.add_vertices(2) | |
>>> print(g) | |
IGRAPH U--- 3 0 -- | |
>>> g.add_edges([(0,1),(1,2)]) | |
>>> print(g) | |
IGRAPH U--- 3 2 -- | |
+ edges: | |
0--1 1--2 | |
>>> f=open("test.net","w") | |
>>> g.write_pajek(f) | |
>>> | |
|
|
所以一個簡單的Pajek .net file 就生成了,下一步就是編碼從數據庫中取出對應的數據,然後構造成Graph, 繼而得到最終可供分析的文件,比較簡單。 |
|
|
|
|
|
1.安裝mysql:sudo apt-get install mysql-server; | |
登錄:mysql -uroot -p | |
2.導入已有數據:source <path to you sql file>,靜靜等候導入完成; | |
3.導入完成後,明確表結構: | |
show tables; | |
desc tablename; | |
|
|
4.下面在python程序中進行具體的業務處理。 | |
(1)從數據庫把需要的數據導出到文件中; | |
(2)具體由數據生成Graph,從而可以得到我們需要的Pajek .net文件。這裏面的難點在於:不能以ID的 最大值作爲vertex的數目,構造Graph,因爲值太大,所以只能用ID作爲vertex的“name”屬性,但是 add edge的時候又需要具體的vertex id所以需要我們自己使用一個字典保存二者之間的映射關係; |
|
(3)慢慢等待,畢竟是30M的文件,有1391718行(通過wc -l file命令); | |
5.得到.net file 之後,用Pajek工具導入,可以看到網絡圖,進行自己需要的分析。 | |
|
|
代碼在這裏。 | |
|
|
|
|
參考: | |
1.Tutorial — python-igraph v0.6 documentation |
python-igraph 小試牛刀
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.