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.

kernels.py 3.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. """Those who are not graph kernels. We can be kernels for nodes or edges!
  2. These kernels are defined between pairs of vectors.
  3. """
  4. import numpy as np
  5. def deltakernel(x, y):
  6. """Delta kernel. Return 1 if x == y, 0 otherwise.
  7. Parameters
  8. ----------
  9. x, y : any
  10. Two parts to compare.
  11. Return
  12. ------
  13. kernel : integer
  14. Delta kernel.
  15. References
  16. ----------
  17. [1] H. Kashima, K. Tsuda, and A. Inokuchi. Marginalized kernels between
  18. labeled graphs. In Proceedings of the 20th International Conference on
  19. Machine Learning, Washington, DC, United States, 2003.
  20. """
  21. return x == y #(1 if condition else 0)
  22. def gaussiankernel(x, y, gamma=None):
  23. """Gaussian kernel.
  24. Compute the rbf (gaussian) kernel between x and y:
  25. K(x, y) = exp(-gamma ||x-y||^2).
  26. Read more in the `User Guide of scikit-learn library <https://scikit-learn.org/stable/modules/metrics.html#rbf-kernel>`__.
  27. Parameters
  28. ----------
  29. x, y : array
  30. gamma : float, default None
  31. If None, defaults to 1.0 / n_features
  32. Returns
  33. -------
  34. kernel : float
  35. """
  36. if gamma is None:
  37. gamma = 1.0 / len(x)
  38. # xt = np.array([float(itm) for itm in x]) # @todo: move this to dataset or datafile to speed up.
  39. # yt = np.array([float(itm) for itm in y])
  40. # kernel = xt - yt
  41. # kernel = kernel ** 2
  42. # kernel = np.sum(kernel)
  43. # kernel *= -gamma
  44. # kernel = np.exp(kernel)
  45. # return kernel
  46. return np.exp((np.sum(np.subtract(x, y) ** 2)) * -gamma)
  47. def polynomialkernel(x, y, d=1, c=0):
  48. """Polynomial kernel.
  49. Compute the polynomial kernel between x and y:
  50. K(x, y) = <x, y> ^d + c.
  51. Parameters
  52. ----------
  53. x, y : array
  54. d : integer, default 1
  55. c : float, default 0
  56. Returns
  57. -------
  58. kernel : float
  59. """
  60. return np.dot(x, y) ** d + c
  61. def linearkernel(x, y):
  62. """Polynomial kernel.
  63. Compute the polynomial kernel between x and y:
  64. K(x, y) = <x, y>.
  65. Parameters
  66. ----------
  67. x, y : array
  68. d : integer, default 1
  69. c : float, default 0
  70. Returns
  71. -------
  72. kernel : float
  73. """
  74. return np.dot(x, y)
  75. def kernelsum(k1, k2, d11, d12, d21=None, d22=None, lamda1=1, lamda2=1):
  76. """Sum of a pair of kernels.
  77. k = lamda1 * k1(d11, d12) + lamda2 * k2(d21, d22)
  78. Parameters
  79. ----------
  80. k1, k2 : function
  81. A pair of kernel functions.
  82. d11, d12:
  83. Inputs of k1. If d21 or d22 is None, apply d11, d12 to both k1 and k2.
  84. d21, d22:
  85. Inputs of k2.
  86. lamda1, lamda2: float
  87. Coefficients of the product.
  88. Return
  89. ------
  90. kernel : integer
  91. """
  92. if d21 == None or d22 == None:
  93. kernel = lamda1 * k1(d11, d12) + lamda2 * k2(d11, d12)
  94. else:
  95. kernel = lamda1 * k1(d11, d12) + lamda2 * k2(d21, d22)
  96. return kernel
  97. def kernelproduct(k1, k2, d11, d12, d21=None, d22=None, lamda=1):
  98. """Product of a pair of kernels.
  99. k = lamda * k1(d11, d12) * k2(d21, d22)
  100. Parameters
  101. ----------
  102. k1, k2 : function
  103. A pair of kernel functions.
  104. d11, d12:
  105. Inputs of k1. If d21 or d22 is None, apply d11, d12 to both k1 and k2.
  106. d21, d22:
  107. Inputs of k2.
  108. lamda: float
  109. Coefficient of the product.
  110. Return
  111. ------
  112. kernel : integer
  113. """
  114. if d21 == None or d22 == None:
  115. kernel = lamda * k1(d11, d12) * k2(d11, d12)
  116. else:
  117. kernel = lamda * k1(d11, d12) * k2(d21, d22)
  118. return kernel
  119. if __name__ == '__main__':
  120. o = polynomialkernel([1, 2], [3, 4], 2, 3)

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