|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- {
- "cells": [
- {
- "cell_type": "code",
- "execution_count": 10,
- "metadata": {},
- "outputs": [],
- "source": [
- "import sys\n",
- "import pathlib\n",
- "sys.path.insert(0, \"../py-graph/\")\n",
- "\n",
- "\n",
- "import networkx as nx\n",
- "import numpy as np\n",
- "import time\n",
- "\n",
- "from utils.utils import getSPGraph\n",
- "\n",
- "\n",
- "def pathkernel(*args):\n",
- " \"\"\"Calculate shortest-path kernels between graphs.\n",
- " \n",
- " Parameters\n",
- " ----------\n",
- " Gn : List of NetworkX graph\n",
- " List of graphs between which the kernels are calculated.\n",
- " /\n",
- " G1, G2 : NetworkX graphs\n",
- " 2 graphs between which the kernel is calculated.\n",
- " \n",
- " Return\n",
- " ------\n",
- " Kmatrix/Kernel : Numpy matrix/int\n",
- " Kernel matrix, each element of which is the sp kernel between 2 praphs. / SP Kernel between 2 graphs.\n",
- " \n",
- " References\n",
- " ----------\n",
- " [1] Borgwardt KM, Kriegel HP. Shortest-path kernels on graphs. InData Mining, Fifth IEEE International Conference on 2005 Nov 27 (pp. 8-pp). IEEE.\n",
- " \"\"\"\n",
- " if len(args) == 1: # for a list of graphs\n",
- " Gn = args[0]\n",
- " \n",
- " Kmatrix = np.zeros((len(Gn), len(Gn)))\n",
- " \n",
- " Sn = [] # get shortest path graphs of Gn\n",
- " for i in range(0, len(Gn)):\n",
- " Sn.append(getSPGraph(Gn[i]))\n",
- "\n",
- " start_time = time.time()\n",
- " for i in range(0, len(Gn)):\n",
- " for j in range(i, len(Gn)):\n",
- " for e1 in Sn[i].edges(data = True):\n",
- " for e2 in Sn[j].edges(data = True): \n",
- " if e1[2]['cost'] != 0 and e1[2]['cost'] == e2[2]['cost'] and ((e1[0] == e2[0] and e1[1] == e2[1]) or (e1[0] == e2[1] and e1[1] == e2[0])):\n",
- " Kmatrix[i][j] += 1\n",
- " Kmatrix[j][i] += (0 if i == j else 1)\n",
- "\n",
- " print(\"--- shortest path kernel matrix of size %d built in %s seconds ---\" % (len(Gn), (time.time() - start_time)))\n",
- " \n",
- " return Kmatrix\n",
- " \n",
- " else: # for only 2 graphs\n",
- " G1 = args[0]\n",
- " G2 = args[1]\n",
- " \n",
- " kernel = 0\n",
- " \n",
- " for e1 in G1.edges(data = True):\n",
- " for e2 in G2.edges(data = True): \n",
- " if e1[2]['cost'] != 0 and e1[2]['cost'] == e2[2]['cost'] and ((e1[0] == e2[0] and e1[1] == e2[1]) or (e1[0] == e2[1] and e1[1] == e2[0])):\n",
- " kernel += 1\n",
- "\n",
- " print(\"--- shortest path kernel built in %s seconds ---\" % (time.time() - start_time))\n",
- " \n",
- " return kernel"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 11,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "--- shortest path kernel matrix of size 185 built in 34.96375751495361 seconds ---\n",
- "[[ 3. 1. 3. ..., 1. 1. 1.]\n",
- " [ 1. 6. 1. ..., 0. 0. 3.]\n",
- " [ 3. 1. 3. ..., 1. 1. 1.]\n",
- " ..., \n",
- " [ 1. 0. 1. ..., 55. 21. 7.]\n",
- " [ 1. 0. 1. ..., 21. 55. 7.]\n",
- " [ 1. 3. 1. ..., 7. 7. 55.]]\n"
- ]
- }
- ],
- "source": [
- "from utils.graphfiles import loadDataset\n",
- "\n",
- "dataset, y = loadDataset(\"/home/ljia/Documents/research-repo/datasets/acyclic/Acyclic/dataset_bps.ds\")\n",
- "G1 = dataset[12]\n",
- "G2 = dataset[20]\n",
- "Kmatrix = spkernel(dataset)\n",
- "\n",
- "print(Kmatrix)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": []
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 3",
- "language": "python",
- "name": "python3"
- },
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 3
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython3",
- "version": "3.5.2"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
|