圖核graph kernel方法Python工具包graphkernels的安裝和使用

圖核graph kernel是一種有效的圖結構相似度的近似度量方式,針對不同的圖結構(labeled graphs, weighted graphs, directed graphs, etc.) 有不同的Graph kernel

這裏,我們不介紹graph kernel的算法理論,只是簡單介紹下其Python工具包graphkernels的安裝和使用。


graphkernels是由ETH Machine Learning&Computational Biology Lab出品的,目前的版本是graphkernels 0.1.8


一.安裝

graphkernels的GitHub主頁上提供了兩種安裝方式:

1.通過pip安裝,pip install graphkernels,這種方式並不推薦,因爲工具包的依賴比較複雜,同時現在還是不穩定的版本,直接pip會出很多錯誤。

2.通過源碼安裝,先python setup.py build再 python setup.py install,這種推薦的安裝方法。


工具依賴及安裝:

1.SWIG,sudo apt-get install SWIG

2.a C++ compiler (e.g. gcc, XCode),一般的linux都自帶gcc

3.numpy,pip install numpy(值得注意的是graphkernels是由python2編寫的,如果你用的python和pip是python3的,需要通過python2來安裝

即pip2 install install numpy,下同)

4.igraph,pip install python-igraph

5.GKextCPy,這個包是這個工具作者自己寫的好像,直接pip會報錯,如:GKextCPy.h:17:29: fatal error: eigen3/Eigen/Core: 沒有那個文件或目錄。所以這裏也需要通過源碼安裝,GKextCPy 0.3.0

這裏報錯是因爲GKextCPy.h找不到Eigen/Core,這裏採取的辦法就是手動給它加上include_dirs。

首先,locate一下你linux裏面Eigen/Core的位置。

root@ace-virtual-machine:# locate Eigen/Core
/usr/local/lib/python2.7/dist-packages/external/eigen_archive/Eigen/Core
/usr/local/lib/python2.7/dist-packages/tensorflow/include/Eigen/Core
/usr/local/lib/python2.7/dist-packages/tensorflow/include/external/eigen_archive/Eigen/Core
/usr/local/lib/python2.7/dist-packages/tensorflow/include/third_party/eigen3/Eigen/Core
/usr/local/lib/python3.5/dist-packages/external/eigen_archive/Eigen/Core
/usr/local/lib/python3.5/dist-packages/tensorflow/include/Eigen/Core
/usr/local/lib/python3.5/dist-packages/tensorflow/include/external/eigen_archive/Eigen/Core
/usr/local/lib/python3.5/dist-packages/tensorflow/include/third_party/eigen3/Eigen/Core

然後,在GKextCPy源碼包的setpy.py中加上include_dirs=['/usr/local/lib/python2.7/dist-packages/tensorflow/include/'],

#!/usr/bin/env python
"""
setup.py file for SWIG
"""
#from distutils.core import setup, Extension
from setuptools import setup, Extension

GKextCPy_module = Extension('_GKextCPy', sources=['GKextCPy_wrap.cxx', 'GKextCPy.cpp'],swig_opts=['-c++'], extra_compile_args = ["-std=c++11"])

setup (name = 'GKextCPy',
       version = '0.3.0',
       author      = "Elisabetta Ghisu",
       description = """Graph Kernels: building the extension Python module. This is a wrapper package from C++ to Python.""",
       ext_modules = [GKextCPy_module],
       include_dirs=['/usr/local/lib/python2.7/dist-packages/tensorflow/include/'],
       py_modules = ["GKextCPy"],
       license = 'ETH Zurich',
       )

最後再python setup.py build 接着 python setup.py install,就把GKextCPy安裝好了。


graphkernels也是同上直接通過源碼安裝,graphkernels 0.1.8

先python setup.py build 再 python setup.py install就ok了。

至此,圖核graph kernel方法Python工具包graphkernels的安裝就完成了。


二.使用

graphkernels python package的使用比較簡單,主要分爲3個步驟:


  1. Import the packages

  1. Load the data
  1. Compute the kernels: example with the WL kernels
我們以package自帶的demo_mutag.py爲例介紹下:
import graphkernels.kernels as gk

import IPython as ip
import numpy as np

# Load data
mutag_list = np.load("data.mutag")#array of igraph objects

### ALL KERNELS COMPUTE
K1 = gk.CalculateEdgeHistKernel(mutag_list)
K2 = gk.CalculateVertexHistKernel(mutag_list) 
K3 = gk.CalculateVertexEdgeHistKernel(mutag_list)
K4 = gk.CalculateVertexVertexEdgeHistKernel(mutag_list)
K5 = gk.CalculateEdgeHistGaussKernel(mutag_list)
K6 = gk.CalculateVertexHistGaussKernel(mutag_list)
K7 = gk.CalculateVertexEdgeHistGaussKernel(mutag_list)
K8 = gk.CalculateGeometricRandomWalkKernel(mutag_list)
K9 = gk.CalculateExponentialRandomWalkKernel(mutag_list)
K10 = gk.CalculateKStepRandomWalkKernel(mutag_list)
K11 = gk.CalculateWLKernel(mutag_list)
K12 = gk.CalculateConnectedGraphletKernel(mutag_list, 4)
K13 = gk.CalculateGraphletKernel(mutag_list, 4)
K14 = gk.CalculateShortestPathKernel(mutag_list

The matrix K is the kernel matrix, obtained with the WL kernel, and therefore is a square matrix of size equal to the number of samples.

這裏的圖是用 igraph裏面的graph object描述,同時也可以將其用graphml的形式存儲下來。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章