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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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^Ty)^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 kernelsum(k1, k2, d11, d12, d21=None, d22=None, lamda1=1, lamda2=1):
  61. """Sum of a pair of kernels.
  62. k = lamda1 * k1(d11, d12) + lamda2 * k2(d21, d22)
  63. Parameters
  64. ----------
  65. k1, k2 : function
  66. A pair of kernel functions.
  67. d11, d12:
  68. Inputs of k1. If d21 or d22 is None, apply d11, d12 to both k1 and k2.
  69. d21, d22:
  70. Inputs of k2.
  71. lamda1, lamda2: float
  72. Coefficients of the product.
  73. Return
  74. ------
  75. kernel : integer
  76. """
  77. if d21 == None or d22 == None:
  78. kernel = lamda1 * k1(d11, d12) + lamda2 * k2(d11, d12)
  79. else:
  80. kernel = lamda1 * k1(d11, d12) + lamda2 * k2(d21, d22)
  81. return kernel
  82. def kernelproduct(k1, k2, d11, d12, d21=None, d22=None, lamda=1):
  83. """Product of a pair of kernels.
  84. k = lamda * k1(d11, d12) * k2(d21, d22)
  85. Parameters
  86. ----------
  87. k1, k2 : function
  88. A pair of kernel functions.
  89. d11, d12:
  90. Inputs of k1. If d21 or d22 is None, apply d11, d12 to both k1 and k2.
  91. d21, d22:
  92. Inputs of k2.
  93. lamda: float
  94. Coefficient of the product.
  95. Return
  96. ------
  97. kernel : integer
  98. """
  99. if d21 == None or d22 == None:
  100. kernel = lamda * k1(d11, d12) * k2(d11, d12)
  101. else:
  102. kernel = lamda * k1(d11, d12) * k2(d21, d22)
  103. return kernel
  104. if __name__ == '__main__':
  105. o = polynomialkernel([1, 2], [3, 4], 2, 3)

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