You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_pathkernel.ipynb 4.5 kB

7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 10,
  6. "metadata": {},
  7. "outputs": [],
  8. "source": [
  9. "import sys\n",
  10. "import pathlib\n",
  11. "sys.path.insert(0, \"../py-graph/\")\n",
  12. "\n",
  13. "\n",
  14. "import networkx as nx\n",
  15. "import numpy as np\n",
  16. "import time\n",
  17. "\n",
  18. "from utils.utils import getSPGraph\n",
  19. "\n",
  20. "\n",
  21. "def pathkernel(*args):\n",
  22. " \"\"\"Calculate shortest-path kernels between graphs.\n",
  23. " \n",
  24. " Parameters\n",
  25. " ----------\n",
  26. " Gn : List of NetworkX graph\n",
  27. " List of graphs between which the kernels are calculated.\n",
  28. " /\n",
  29. " G1, G2 : NetworkX graphs\n",
  30. " 2 graphs between which the kernel is calculated.\n",
  31. " \n",
  32. " Return\n",
  33. " ------\n",
  34. " Kmatrix/Kernel : Numpy matrix/int\n",
  35. " Kernel matrix, each element of which is the sp kernel between 2 praphs. / SP Kernel between 2 graphs.\n",
  36. " \n",
  37. " References\n",
  38. " ----------\n",
  39. " [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",
  40. " \"\"\"\n",
  41. " if len(args) == 1: # for a list of graphs\n",
  42. " Gn = args[0]\n",
  43. " \n",
  44. " Kmatrix = np.zeros((len(Gn), len(Gn)))\n",
  45. " \n",
  46. " Sn = [] # get shortest path graphs of Gn\n",
  47. " for i in range(0, len(Gn)):\n",
  48. " Sn.append(getSPGraph(Gn[i]))\n",
  49. "\n",
  50. " start_time = time.time()\n",
  51. " for i in range(0, len(Gn)):\n",
  52. " for j in range(i, len(Gn)):\n",
  53. " for e1 in Sn[i].edges(data = True):\n",
  54. " for e2 in Sn[j].edges(data = True): \n",
  55. " 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",
  56. " Kmatrix[i][j] += 1\n",
  57. " Kmatrix[j][i] += (0 if i == j else 1)\n",
  58. "\n",
  59. " print(\"--- shortest path kernel matrix of size %d built in %s seconds ---\" % (len(Gn), (time.time() - start_time)))\n",
  60. " \n",
  61. " return Kmatrix\n",
  62. " \n",
  63. " else: # for only 2 graphs\n",
  64. " G1 = args[0]\n",
  65. " G2 = args[1]\n",
  66. " \n",
  67. " kernel = 0\n",
  68. " \n",
  69. " for e1 in G1.edges(data = True):\n",
  70. " for e2 in G2.edges(data = True): \n",
  71. " 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",
  72. " kernel += 1\n",
  73. "\n",
  74. " print(\"--- shortest path kernel built in %s seconds ---\" % (time.time() - start_time))\n",
  75. " \n",
  76. " return kernel"
  77. ]
  78. },
  79. {
  80. "cell_type": "code",
  81. "execution_count": 11,
  82. "metadata": {},
  83. "outputs": [
  84. {
  85. "name": "stdout",
  86. "output_type": "stream",
  87. "text": [
  88. "--- shortest path kernel matrix of size 185 built in 34.96375751495361 seconds ---\n",
  89. "[[ 3. 1. 3. ..., 1. 1. 1.]\n",
  90. " [ 1. 6. 1. ..., 0. 0. 3.]\n",
  91. " [ 3. 1. 3. ..., 1. 1. 1.]\n",
  92. " ..., \n",
  93. " [ 1. 0. 1. ..., 55. 21. 7.]\n",
  94. " [ 1. 0. 1. ..., 21. 55. 7.]\n",
  95. " [ 1. 3. 1. ..., 7. 7. 55.]]\n"
  96. ]
  97. }
  98. ],
  99. "source": [
  100. "from utils.graphfiles import loadDataset\n",
  101. "\n",
  102. "dataset, y = loadDataset(\"/home/ljia/Documents/research-repo/datasets/acyclic/Acyclic/dataset_bps.ds\")\n",
  103. "G1 = dataset[12]\n",
  104. "G2 = dataset[20]\n",
  105. "Kmatrix = spkernel(dataset)\n",
  106. "\n",
  107. "print(Kmatrix)"
  108. ]
  109. },
  110. {
  111. "cell_type": "code",
  112. "execution_count": null,
  113. "metadata": {},
  114. "outputs": [],
  115. "source": []
  116. }
  117. ],
  118. "metadata": {
  119. "kernelspec": {
  120. "display_name": "Python 3",
  121. "language": "python",
  122. "name": "python3"
  123. },
  124. "language_info": {
  125. "codemirror_mode": {
  126. "name": "ipython",
  127. "version": 3
  128. },
  129. "file_extension": ".py",
  130. "mimetype": "text/x-python",
  131. "name": "python",
  132. "nbconvert_exporter": "python",
  133. "pygments_lexer": "ipython3",
  134. "version": "3.5.2"
  135. }
  136. },
  137. "nbformat": 4,
  138. "nbformat_minor": 2
  139. }

A Python package for graph kernels, graph edit distances and graph pre-image problem.