Esempio n. 1
0
def test_alignment_centered():
    km1 = KernelMatrix(kernel=LinearKernel())
    km1.attach_to(gen_random_sample(num_samples, sample_dim))

    km2 = KernelMatrix(kernel=LinearKernel())
    km2.attach_to(gen_random_sample(num_samples, sample_dim))

    km3_bad_size = KernelMatrix(kernel=LinearKernel())
    km3_bad_size.attach_to(gen_random_sample(num_samples + 2, sample_dim))

    with raises(ValueError):
        alignment_centered(km1.full, km3_bad_size.full)

    # bad type : must be ndarray
    with raises(TypeError):
        alignment_centered(km1, km2.full)

    # bad type : must be ndarray
    with raises(TypeError):
        alignment_centered(km1.full, km2)

    for flag in (True, False):
        _ = alignment_centered(km1.full, km2.full, centered_already=flag)

    with raises(ValueError):
        _ = alignment_centered(np.zeros((10, 10)), randn(10, 10),
                               value_if_zero_division='raise')

    return_val_requested = 'random_set_value'
    with warns(UserWarning):
        ret_value = alignment_centered(randn(10, 10),
                                       np.zeros((10, 10)),
                                       value_if_zero_division=return_val_requested)
    if ret_value != return_val_requested:
        raise ValueError('Not returning the value requested in case of error!')
Esempio n. 2
0
def test_size_property_mismatch():

    ks = KernelSet(num_samples=sample_data.shape[0] + 1)
    lin = KernelMatrix(LinearKernel(skip_input_checks=True))
    lin.attach_to(sample_data)
    with raises(KMSetAdditionError):
        ks.append(lin)
def test_attributes():

    km = KernelMatrix(LinearKernel())
    km.set_attr('name', 'linear')
    assert km.get_attr('name') == 'linear'
    assert km.get_attr('noname', '404') == '404'
    km.set_attr('weight', 42)

    kma = km.attributes()
    for attr in ('name', 'weight'):
        assert attr in kma
Esempio n. 4
0
def test_linear_kernel(sample_dim, num_samples):
    """Tests specific for Linear kernel."""

    linear = LinearKernel(skip_input_checks=False)
    _test_for_all_kernels(linear, sample_dim)
    _test_func_is_valid_kernel(linear, sample_dim, num_samples)
Esempio n. 5
0
from kernelmethods.base import KMSetAdditionError, KernelMatrix, KernelSet, \
    BaseKernelFunction
from kernelmethods.numeric_kernels import GaussianKernel, LinearKernel, PolyKernel
from kernelmethods.sampling import make_kernel_bucket

num_samples = 50  # 9
sample_dim = 3  # 2
target_label_set = [1, 2]

sample_data = np.random.rand(num_samples, sample_dim)
target_labels = np.random.choice(target_label_set, (num_samples, 1))

IdealKM = target_labels.dot(target_labels.T)

rbf = KernelMatrix(GaussianKernel(sigma=10, skip_input_checks=True))
lin = KernelMatrix(LinearKernel(skip_input_checks=True))
poly = KernelMatrix(PolyKernel(degree=2, skip_input_checks=True))

# lin.attach_to(sample_data)
# rbf.attach_to(sample_data)
# poly.attach_to(sample_data)

kset = KernelSet([lin, poly, rbf])
print(kset)


def test_creation():

    try:
        ks = KernelSet()
    except:
Esempio n. 6
0
def test_normalize():

    km = KernelMatrix(kernel=LinearKernel())
    km.attach_to(gen_random_sample(num_samples, sample_dim))
    km.normalize()
Esempio n. 7
0
def test_centering():

    km = KernelMatrix(kernel=LinearKernel())
    km.attach_to(gen_random_sample(num_samples, sample_dim))
    km.center()
Esempio n. 8
0
                                           LinearKernel, PolyKernel)
from kernelmethods.sampling import make_kernel_bucket
from kernelmethods.tests.test_numeric_kernels import _test_for_all_kernels

default_feature_dim = 10
range_feature_dim = [10, 500]
range_num_samples = [50, 500]
num_samples = np.random.randint(20)
sample_dim = np.random.randint(10)
range_polynomial_degree = [2, 10]  # degree=1 is tested in LinearKernel()

np.random.seed(42)

# choosing skip_input_checks=False will speed up test runs
# default values for parameters
SupportedKernels = (GaussianKernel(), PolyKernel(), LinearKernel(),
                    LaplacianKernel())
num_tests_psd_kernel = 3


def gen_random_array(dim):
    """To better control precision and type of floats"""

    # TODO input sparse arrays for test
    return np.random.rand(dim)


def gen_random_sample(num_samples, sample_dim):
    """To better control precision and type of floats"""

    # TODO input sparse arrays for test
Esempio n. 9
0
    def __init__(
        self,
        poly_degree_values=cfg.default_degree_values_poly_kernel,
        rbf_sigma_values=cfg.default_sigma_values_gaussian_kernel,
        laplace_gamma_values=cfg.default_gamma_values_laplacian_kernel,
        sigmoid_gamma_values=cfg.default_gamma_values_sigmoid_kernel,
        sigmoid_offset_values=cfg.default_offset_values_sigmoid_kernel,
        name='KernelBucket',
        normalize_kernels=True,
        skip_input_checks=False,
    ):
        """
        Constructor.

        Parameters
        ----------
        poly_degree_values : Iterable
            List of values for the degree parameter of the PolyKernel. One
            KernelMatrix will be added to the bucket for each value.

        rbf_sigma_values : Iterable
            List of values for the sigma parameter of the GaussianKernel. One
            KernelMatrix will be added to the bucket for each value.

        laplace_gamma_values : Iterable
            List of values for the gamma parameter of the LaplacianKernel. One
            KernelMatrix will be added to the bucket for each value.

        sigmoid_gamma_values : Iterable
            List of values for the gamma parameter of the SigmoidKernel. One
            KernelMatrix will be added to the bucket for each value.

        sigmoid_offset_values : Iterable
            List of values for the offset parameter of the SigmoidKernel. One
            KernelMatrix will be added to the bucket for each value.

        name : str
            String to identify the purpose or type of the bucket of kernels.
            Also helps easily distinguishing it from other buckets.

        normalize_kernels : bool
            Flag to indicate whether the kernel matrices need to be normalized

        skip_input_checks : bool
            Flag to indicate whether checks on input data (type, format etc) can
            be skipped. This helps save a tiny bit of runtime for expert uses when
            data types and formats are managed thoroughly in numpy. Default:
            False. Disable this only when you know exactly what you're doing!

        """

        if isinstance(normalize_kernels, bool):
            self._norm_kernels = normalize_kernels
        else:
            raise TypeError('normalize_kernels must be bool')

        if isinstance(skip_input_checks, bool):
            self._skip_input_checks = skip_input_checks
        else:
            raise TypeError('skip_input_checks must be bool')

        # start with the addition of kernel matrix for linear kernel
        init_kset = [
            KernelMatrix(LinearKernel(), normalized=self._norm_kernels),
        ]
        super().__init__(km_list=init_kset, name=name)
        # not attached to a sample yet
        self._num_samples = None

        self.add_parametrized_kernels(PolyKernel, 'degree', poly_degree_values)
        self.add_parametrized_kernels(GaussianKernel, 'sigma',
                                      rbf_sigma_values)
        self.add_parametrized_kernels(LaplacianKernel, 'gamma',
                                      laplace_gamma_values)
        self.add_parametrized_kernels(SigmoidKernel, 'gamma',
                                      sigmoid_gamma_values)
        self.add_parametrized_kernels(SigmoidKernel, 'offset',
                                      sigmoid_offset_values)
Esempio n. 10
0
                                 ensure_ndarray_2D,
                                 get_callable_name, not_symmetric)

default_feature_dim = 10
range_feature_dim = [10, 500]
range_num_samples = [50, 500]
num_samples = np.random.randint(20)
sample_dim = np.random.randint(10)

range_polynomial_degree = [2, 10] # degree=1 is tested in LinearKernel()

np.random.seed(42)

# choosing skip_input_checks=False will speed up test runs
# default values for parameters
SupportedKernels = (GaussianKernel(), PolyKernel(), LinearKernel(),
                    LaplacianKernel())
num_tests_psd_kernel = 3


def test_check_input_arrays():

    with raises(ValueError):
        check_input_arrays(np.random.rand(10, 5), np.random.rand(5, 4))

    with raises(ValueError):
        check_input_arrays(np.random.rand(10), np.random.rand(5))

    # from scipy.sparse import csr_matrix
    # s1 = csr_matrix((3,4))
    # s2 = csr_matrix((3, 4))