图核graph kernel方法Python工具包graphkernels的安装和使用

来源:互联网 发布:淘宝店铺招牌素材图 编辑:程序博客网 时间:2024/05/16 16:58

图核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 gkimport IPython as ipimport numpy as np# Load datamutag_list = np.load("data.mutag")#array of igraph objects### ALL KERNELS COMPUTEK1 = 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的形式存储下来。

原创粉丝点击