예제 #1
0
    def test_simple_kernel(self):
        """Test square_kernel_matrix and kernel_matrix of the _mock_kernel above."""
        X1 = [0.1, 0.4]
        X2 = [0.1, 0.3, 0.5]

        K1_expected = pnp.array([[1, 0.2], [0.2, 1]])
        K2_expected = pnp.array([[1, 0.2, 0.2], [0.2, 0.2, 0.2]])

        K1 = kern.square_kernel_matrix(X1, lambda x1, x2: _mock_kernel(x1, x2, []))
        K2 = kern.kernel_matrix(X1, X2, lambda x1, x2: _mock_kernel(x1, x2, []))

        assert np.array_equal(K1, K1_expected)
        assert np.array_equal(K2, K2_expected)
예제 #2
0
    def test_laplace_kernel(self):
        """Test square_kernel_matrix and kernel_matrix of the _laplace_kernel above."""
        X1 = [0.1, 0.4, 0.2]
        X2 = [0.0, 0.1, 0.3, 0.2]

        K1_expected = pnp.exp(-np.array([[0.0, 0.3, 0.1], [0.3, 0.0, 0.2], [0.1, 0.2, 0.0]]))
        K2_expected = pnp.exp(
            -np.array([[0.1, 0.0, 0.2, 0.1], [0.4, 0.3, 0.1, 0.2], [0.2, 0.1, 0.1, 0.0]])
        )

        K1 = kern.square_kernel_matrix(X1, _laplace_kernel, assume_normalized_kernel=False)
        K2 = kern.kernel_matrix(X1, X2, _laplace_kernel)

        assert np.allclose(K1, K1_expected)
        assert np.allclose(K2, K2_expected)