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.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 :ref:`User Guide <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])
  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. def polynomialkernel(x, y, d=1, c=0):
  47. """Polynomial kernel.
  48. Compute the polynomial kernel between x and y:
  49. K(x, y) = <x, y> ^d + c.
  50. Parameters
  51. ----------
  52. x, y : array
  53. d : integer, default 1
  54. c : float, default 0
  55. Returns
  56. -------
  57. kernel : float
  58. """
  59. return np.dot(x, y) ** d + c
  60. def linearkernel(x, y):
  61. """Polynomial kernel.
  62. Compute the polynomial kernel between x and y:
  63. K(x, y) = <x, y>.
  64. Parameters
  65. ----------
  66. x, y : array
  67. d : integer, default 1
  68. c : float, default 0
  69. Returns
  70. -------
  71. kernel : float
  72. """
  73. return np.dot(x, y)
  74. def kernelsum(k1, k2, d11, d12, d21=None, d22=None, lamda1=1, lamda2=1):
  75. """Sum of a pair of kernels.
  76. k = lamda1 * k1(d11, d12) + lamda2 * k2(d21, d22)
  77. Parameters
  78. ----------
  79. k1, k2 : function
  80. A pair of kernel functions.
  81. d11, d12:
  82. Inputs of k1. If d21 or d22 is None, apply d11, d12 to both k1 and k2.
  83. d21, d22:
  84. Inputs of k2.
  85. lamda1, lamda2: float
  86. Coefficients of the product.
  87. Return
  88. ------
  89. kernel : integer
  90. """
  91. if d21 == None or d22 == None:
  92. kernel = lamda1 * k1(d11, d12) + lamda2 * k2(d11, d12)
  93. else:
  94. kernel = lamda1 * k1(d11, d12) + lamda2 * k2(d21, d22)
  95. return kernel
  96. def kernelproduct(k1, k2, d11, d12, d21=None, d22=None, lamda=1):
  97. """Product of a pair of kernels.
  98. k = lamda * k1(d11, d12) * k2(d21, d22)
  99. Parameters
  100. ----------
  101. k1, k2 : function
  102. A pair of kernel functions.
  103. d11, d12:
  104. Inputs of k1. If d21 or d22 is None, apply d11, d12 to both k1 and k2.
  105. d21, d22:
  106. Inputs of k2.
  107. lamda: float
  108. Coefficient of the product.
  109. Return
  110. ------
  111. kernel : integer
  112. """
  113. if d21 == None or d22 == None:
  114. kernel = lamda * k1(d11, d12) * k2(d11, d12)
  115. else:
  116. kernel = lamda * k1(d11, d12) * k2(d21, d22)
  117. return kernel
  118. if __name__ == '__main__':
  119. o = polynomialkernel([1, 2], [3, 4], 2, 3)

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