def rbf_kernel(self, x, y, gamma):
"""
Custom sigmoid kernel function, similarities of vectors using a radial basis function kernel
:param x: array of input vectors
:param y: array of input vectors
:param gamma: reach factor
:returns:
- rbfk: radial basis of the kernel's inner product
"""
mat1 = np.mat(x) #convert to readable matrices
mat2 = np.mat(y)
trnorms1 = np.mat([(v * v.T)[0, 0] for v in mat1]).T #norm matrices
trnorms2 = np.mat([(v * v.T)[0, 0] for v in mat2]).T
k1 = trnorms1 * np.mat(np.ones((mat2.shape[0], 1), dtype=np.float64)).T #dot products of y and y transposed and x and x transposed
k2 = np.mat(np.ones((mat1.shape[0], 1), dtype=np.float64)) * trnorms2.T
rbfk = k1 + k2 #sum products together
rbfk -= 2 * np.mat(mat1 * mat2.T) #dot product of x and y transposed
rbfk *= - 1./(2 * np.power(gamma, 2)) #radial basis
np.exp(rbfk,rbfk)
return np.array(rbfk)
评论列表
文章目录