Beispiel #1
0
def test_polynomial_kernel(sample_dim, num_samples,
                           poly_degree, poly_intercept):
    """Tests specific for Polynomial kernel."""

    poly = PolyKernel(degree=poly_degree, b=poly_intercept, skip_input_checks=False)
    _test_for_all_kernels(poly, sample_dim)
    _test_func_is_valid_kernel(poly, sample_dim, num_samples)
    DEFINED_KERNEL_FUNCS
from kernelmethods import KernelMatrix, KMAccessError, KernelMethodsException
from kernelmethods.base import ConstantKernelMatrix
from kernelmethods.operations import is_PSD

num_samples = np.random.randint(30, 100)
sample_dim = np.random.randint(3, 10)  # 2
target_label_set = [1, 2]

num_samples_two = np.random.randint(30, 100)
sample_two_dim = sample_dim

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

poly = PolyKernel(degree=2, skip_input_checks=True)
# suffix 1 to indicate one sample case
km1 = KernelMatrix(poly)
km1.attach_to(sample_data)

max_num_elements = max_num_ker_eval = num_samples * (num_samples + 1) / 2


def test_symmetry():

    if not np.isclose(km1.full, km1.full.T).all():
        print('KM not symmetric')


def test_PSD():
Beispiel #3
0
    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:
        raise SyntaxError('empty set creation failed.')
Beispiel #4
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