def test_kernel_values(self): self.assertEqual(kernel.linear_kernel(np.array([1, 0]), np.array([0, 0])), 0) self.assertEqual(kernel.linear_kernel(np.ones(10), np.ones(10)), 10) desc = np.zeros(2).reshape(2, 1) self.assertEqual(kernel.gaussian_kernel(desc, desc, 10)[0, 0], 1) desc1 = np.array([sqrt(2)*3, 0]).reshape(2, 1) desc2 = np.zeros(2).reshape(2, 1) self.assertAlmostEqual(kernel.gaussian_kernel(desc1, desc2, 3)[0, 0], exp(-1), 7)
def predict(relevance_vectors, X, mean, kernel_choice): prediction = [] for xi in range(len(X)): phi_x = 0 for ri in range(len(relevance_vectors)): if kernel_choice == "gaussian": phi_x += mean[ri + 1] * (kernel.gaussian( X[xi], relevance_vectors[ri])) + mean[0] elif kernel_choice == "linear": t1 = X[xi] phi_x += mean[ri + 1] * (kernel.linear_kernel( X[xi], relevance_vectors[ri])) + mean[0] elif kernel_choice == "polynomial": t1 = X[xi] phi_x += mean[ri + 1] * (kernel.polynomial_kernel( X[xi], relevance_vectors[ri])) + mean[0] elif kernel_choice == "linear_spline": phi_x += mean[ri + 1] * (kernel.linear_spline( X[xi], relevance_vectors[ri])) + mean[0] elif kernel_choice == "rbf": phi_x += mean[ri + 1] * (kernel.rbf( X[xi], relevance_vectors[ri])) # + mean[0] phi_x += mean[0] prediction.append(phi_x) return prediction
def Relevance_Vector_Classification_Prediction (Xtest, relevance_vectors, weightMaxPosteriori, kernel_choice): Psum = 0 res = [] for xi in range(len(Xtest)): for ri in range (len(relevance_vectors)): if kernel_choice=="gaussian": Psum+=weightMaxPosteriori[ri+1]*(kernel.gaussian(Xtest[xi], relevance_vectors[ri])) + weightMaxPosteriori[0] elif kernel_choice=="linear": Psum+=weightMaxPosteriori[ri+1]*(kernel.linear_kernel(Xtest[xi], relevance_vectors[ri])) + weightMaxPosteriori[0] elif kernel_choice=="polynomial": Psum+=weightMaxPosteriori[ri+1]*(kernel.polynomial_kernel(Xtest[xi], relevance_vectors[ri])) + weightMaxPosteriori[0] elif kernel_choice=="linear_spline": Psum+=weightMaxPosteriori[ri+1]*(kernel.linear_spline(Xtest[xi], relevance_vectors[ri])) + weightMaxPosteriori[0] y = sigmoid_function(Psum) if y >0.5: res.append(1) elif y<= 0.5: res.append(0) return res
def __linear(self, data, dx, dy): shifted = self.__neighbor(data, dx, dy) linear_kernel(data, dx, dy, shifted[1:, 1:]) return shifted
def test_linear_energy_matrix_shape(self): desc1 = np.ones((50, 5)) desc2 = np.ones((400, 5)) shape = np.shape(kernel.linear_kernel(desc1, desc2)) self.assertEqual(shape, (50, 400))