def test_apply_kernel_bias(self): """Check the kernel function correctly applies a bias.""" clf = BaseRVM(kernel='linear', bias_used=True) x = np.array([[1, 2], [3, 4]]) y = np.array([[5, 6], [7, 8]]) phi = clf._apply_kernel(x, y) target = np.array([[17, 23, 1], [39, 53, 1]]) np.testing.assert_array_equal(phi, target)
def test_apply_kernel_linear(self): """Check linear kernel is applied correctly.""" clf = BaseRVM(kernel='linear', bias_used=False) x = np.array([[1, 2], [3, 4]]) y = np.array([[5, 6], [7, 8]]) phi = clf._apply_kernel(x, y) target = np.array([[17, 23], [39, 53]]) np.testing.assert_array_equal(phi, target)
def test_apply_kernel_rbf(self): """Check RBF kernel is applied correctly.""" clf = BaseRVM(kernel='rbf', bias_used=False, coef1=0.5) x = np.array([[1, 2], [3, 4]]) y = np.array([[5, 6], [7, 8]]) phi = clf._apply_kernel(x, y) target = np.array([[1.12535175e-07, 2.31952283e-16], [1.83156389e-02, 1.12535175e-07]]) np.testing.assert_allclose(phi, target)
def test_apply_kernel_invalid(self): """Check that an invalid kernel choice raises an exception.""" clf = BaseRVM(kernel='wrong') x = np.array([[1, 2], [3, 4]]) y = np.array([[5, 6], [7, 8]]) try: clf._apply_kernel(x, y) except ValueError as error: self.assertEqual(str(error), "Kernel selection is invalid.") else: self.fail()
def setUp(self): """Set up some common test instances.""" self.param_test_clf = BaseRVM(kernel='linear', degree=1, coef1=2, coef0=3, n_iter=200, tol=1e-1, alpha=1e-2, threshold_alpha=1e3, beta=1e-3, beta_fixed=True, bias_used=False, verbose=True)
def test_apply_kernel_custom(self): """Check custom kernels are applied correctly.""" def custom(x, y): return 2 * x.dot(y.T) clf = BaseRVM(kernel=custom, bias_used=False) x = np.array([[1, 2], [3, 4]]) y = np.array([[5, 6], [7, 8]]) phi = clf._apply_kernel(x, y) target = np.array([[34, 46], [78, 106]]) np.testing.assert_array_equal(phi, target)
def test_apply_kernel_poly(self): """Check polynomial kernel is applied correctly.""" clf = BaseRVM(kernel='poly', bias_used=False, degree=2, coef1=1, coef0=0.5) x = np.array([[1, 2], [3, 4]]) y = np.array([[5, 6], [7, 8]]) phi = clf._apply_kernel(x, y) target = np.array([[306.25, 552.25], [1560.25, 2862.25]]) np.testing.assert_allclose(phi, target)
def test_apply_kernel_1D(self): """Check that _apply_kernel catches a non-2D phi.""" def custom(x, y): return np.array([1, 2]) clf = BaseRVM(kernel=custom, bias_used=False) x = np.array([[1, 2], [3, 4]]) y = np.array([[5, 6], [7, 8]]) try: clf._apply_kernel(x, y) except ValueError as error: self.assertEqual( str(error), "Custom kernel function did not return 2D matrix") else: self.fail()
def test_apply_kernel_row_mismatch(self): """Check that _apply_kernel catches a mismatch between input/output.""" def custom(x, y): return np.array([[1, 2]]) clf = BaseRVM(kernel=custom, bias_used=False) x = np.array([[1, 2], [3, 4]]) y = np.array([[5, 6], [7, 8]]) try: clf._apply_kernel(x, y) except ValueError as error: self.assertEqual( str(error), """Custom kernel function did not return matrix with rows""" """ equal to number of data points.""") else: self.fail()