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 2.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. Use sklearn.metrics.pairwise.rbf_kernel instead.
  24. Compute the rbf (gaussian) kernel between X and Y:
  25. K(x, y) = exp(-gamma ||x-y||^2)
  26. for each pair of rows x in X and y in Y.
  27. Read more in the :ref:`User Guide <rbf_kernel>`.
  28. Parameters
  29. ----------
  30. X : array of shape (n_features)
  31. Y : array of shape (n_features)
  32. gamma : float, default None
  33. If None, defaults to 1.0 / n_features
  34. Returns
  35. -------
  36. kernel : integer
  37. """
  38. if gamma is None:
  39. gamma = 1.0 / len(x)
  40. xt = np.array([float(itm) for itm in x])
  41. yt = np.array([float(itm) for itm in y])
  42. kernel = xt - yt
  43. kernel = kernel ** 2
  44. kernel = np.sum(kernel)
  45. kernel *= -gamma
  46. kernel = np.exp(kernel)
  47. return kernel
  48. def kernelsum(k1, k2, d11, d12, d21=None, d22=None, lamda1=1, lamda2=1):
  49. """Sum of a pair of kernels.
  50. k = lamda1 * k1(d11, d12) + lamda2 * k2(d21, d22)
  51. Parameters
  52. ----------
  53. k1, k2 : function
  54. A pair of kernel functions.
  55. d11, d12:
  56. Inputs of k1. If d21 or d22 is None, apply d11, d12 to both k1 and k2.
  57. d21, d22:
  58. Inputs of k2.
  59. lamda1, lamda2: float
  60. Coefficients of the product.
  61. Return
  62. ------
  63. kernel : integer
  64. """
  65. if d21 == None or d22 == None:
  66. kernel = lamda1 * k1(d11, d12) + lamda2 * k2(d11, d12)
  67. else:
  68. kernel = lamda1 * k1(d11, d12) + lamda2 * k2(d21, d22)
  69. return kernel
  70. def kernelproduct(k1, k2, d11, d12, d21=None, d22=None, lamda=1):
  71. """Product of a pair of kernels.
  72. k = lamda * k1(d11, d12) * k2(d21, d22)
  73. Parameters
  74. ----------
  75. k1, k2 : function
  76. A pair of kernel functions.
  77. d11, d12:
  78. Inputs of k1. If d21 or d22 is None, apply d11, d12 to both k1 and k2.
  79. d21, d22:
  80. Inputs of k2.
  81. lamda: float
  82. Coefficient of the product.
  83. Return
  84. ------
  85. kernel : integer
  86. """
  87. if d21 == None or d22 == None:
  88. kernel = lamda * k1(d11, d12) * k2(d11, d12)
  89. else:
  90. kernel = lamda * k1(d11, d12) * k2(d21, d22)
  91. return kernel

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