Author: Not specified Language: python
Description: Not specified Timestamp: 2018-05-21 03:06:26 +0000
View raw paste Child paste by: Not specified Reply
  1. # Define the Kernel here
  2. def gaussian_kernel(x, y, sigma=0.9):
  3.     """Returns the gaussian similarity of arrays `x` and `y` with
  4.    kernel width parameter `sigma` (set to 1 by default)."""
  5.     # A np.newaxis is to add dimension 1D --> 2D or 2D --> 3D. Helpful in broadcasting.
  6.     #Axis = 2, specifies the axis of x along which to compute the vector norms. Along the rows.
  7.     # Helpful Example can be found here -->. https://stackoverflow.com/questions/29241056/how-does-numpy-newaxis-work-and-when-to-use-it?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
  8.     if np.ndim(x) == 1 and np.ndim(y) == 1:
  9.         result = np.exp(- np.linalg.norm(x - y) / (2 * sigma ** 2))
  10.     elif (np.ndim(x) > 1 and np.ndim(y) == 1) or (np.ndim(x) == 1 and np.ndim(y) > 1):
  11.         result = np.exp(- np.linalg.norm(x - y, axis=1) / (2 * sigma ** 2))
  12.     elif np.ndim(x) > 1 and np.ndim(y) > 1:
  13.         result = np.exp(- np.linalg.norm(x[:, np.newaxis] - y[np.newaxis, :], axis=2) / (2 * sigma ** 2))
  14.     return result
View raw paste Child paste by: Not specified Reply