Exemplo n.º 1
0
 def check_multidim_input(self):
     kernels = [
         LinearKernel(),
         PolynomialKernel(degree=6, gamma=1.0, coef0=1.0),
         SigmoidKernel(gamma=0.2, coef0=0.3),
         RBFKernel(gamma=1.0),
         CustomKernel(kernelfunc)
         ]
     args = [
         N.random.randn(10),
         N.random.randn(1, 10),
         N.random.randn(5, 10)
         ]
     for kernel in kernels:
         self.assert_(type(repr(kernel)) is str)
         for i, x in enumerate(args):
             zshape0 = N.atleast_2d(x).shape[0]
             for y in args[i:]:
                 zshape1 = N.atleast_2d(y).shape[0]
                 z = kernel(x, y)
                 self.assertEqual(z.shape[0], zshape0)
                 self.assertEqual(z.shape[1], zshape1)
                 u = kernel(y, x)
                 assert_array_equal(u.squeeze(), z.squeeze())
Exemplo n.º 2
0
    def check_combine(self):
        kernel = LinearKernel()

        y1 = N.random.randn(10)
        x1 = N.random.randn(len(y1), 10)
        origdata = LibSvmRegressionDataSet(y1, x1)
        pcdata = origdata.precompute(kernel)

        y2 = N.random.randn(5)
        x2 = N.random.randn(len(y2), x1.shape[1])
        moredata = LibSvmRegressionDataSet(y2, x2)
        morepcdata = pcdata.combine(moredata)

        expt_grammat = N.empty((len(y1) + len(y2),)*2)
        x = N.vstack([x1,x2])
        for i, xi in enumerate(x):
            for j, xj in enumerate(x):
                expt_grammat[i, j] = kernel(xi, xj)
        for i, row in enumerate(morepcdata.grammat):
            valuerow = row[1:]['value']
            assert_array_almost_equal(valuerow, expt_grammat[i])
Exemplo n.º 3
0
    def check_precompute(self):
        degree, gamma, coef0 = 4, 3.0, 2.0
        kernels = [
            LinearKernel(),
            PolynomialKernel(degree, gamma, coef0),
            RBFKernel(gamma),
            SigmoidKernel(gamma, coef0)
            ]
        y = N.random.randn(10)
        x = N.random.randn(len(y), 10)
        origdata = LibSvmRegressionDataSet(y, x)

        for kernel in kernels:
            # calculate expected Gram matrix
            expt_grammat = N.empty((len(y),)*2)
            for i, xi in enumerate(x):
                for j, xj in enumerate(x):
                    expt_grammat[i, j] = kernel(xi, xj)
            # get a new dataset containing the precomputed data
            pcdata = origdata.precompute(kernel)
            for i, row in enumerate(pcdata.grammat):
                valuerow = row[1:]['value']
                assert_array_almost_equal(valuerow, expt_grammat[i])
Exemplo n.º 4
0
 def check_custom_kernel(self):
     kernel = CustomKernel(kernelfunc)
     x = N.array([2.])
     self.assertAlmostEqual(kernel(x, x), 32.0)
Exemplo n.º 5
0
 def check_rbf_kernel(self):
     kernel = RBFKernel(gamma=1.0)
     x, y = N.array([2.]), N.array([3.])
     self.assertAlmostEqual(kernel(x, y), N.exp(-1.))
Exemplo n.º 6
0
 def check_sigmoid_kernel(self):
     kernel = SigmoidKernel(gamma=0.2, coef0=0.3)
     x = N.array([2.])
     self.assertAlmostEqual(kernel(x, x), 0.80049902)
Exemplo n.º 7
0
 def check_polynomial_kernel(self):
     kernel = PolynomialKernel(degree=6, gamma=1.0, coef0=1.0)
     x = N.array([2.])
     self.assertAlmostEqual(kernel(x, x), 15625.)
Exemplo n.º 8
0
 def check_linear_kernel(self):
     kernel = LinearKernel()
     x = N.array([2.])
     self.assertAlmostEqual(kernel(x, x), 4.)