栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

图核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的形式存储下来。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/833934.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号