Beispiel #1
0
def check_concatenation():
    #check with 2 HPK
    kl1 = HPK_generator(X_tr).make_a_list(n)
    kl2 = HPK_generator(X_tr).make_a_list(n)
    kl3 = kl1 + kl2
    assert_array_equal(kl1[1], kl3[1])
    assert_array_equal(kl1[7], kl3[7])
    assert_array_equal(kl2[1], kl3[n + 1])
    assert_array_equal(kl2[4], kl3[n + 4])
    #check with HPK and SFK
    del kl2, kl3, kl1
    kl1 = HPK_generator(X_tr).make_a_list(n)
    kl2 = SFK_generator(X_tr).make_a_list(n)
    kl3 = kl1 + kl2
    assert_array_equal(kl1[1], kl3[1])
    assert_array_equal(kl1[7], kl3[7])
    assert_array_equal(kl2[1], kl3[n + 1])
    assert_array_equal(kl2[4], kl3[n + 4])
    #check iadd
    del kl2, kl3, kl1
    kl1 = SFK_generator(X_tr).make_a_list(n)
    kl2 = kernel_list(X_tr)
    kl2 += kl1
    assert_array_equal(kl1[3], kl2[3])
    assert_array_equal(kl1[9], kl2[9])
    kl3 = HPK_generator(X_tr).make_a_list(n)
    kl2 += kl3
    assert_array_equal(kl2[4], kl1[4])
    assert_array_equal(kl2[4 + n], kl3[4])
    return
Beispiel #2
0
def check_toarray():
    kl = HPK_generator(X_tr).make_a_list(n)
    ka = kl.to_array()
    #check if to_array return an array with the same shape of the original kernel list
    assert_equal(kl.shape, ka.shape)
    #check if ka is a ndarray
    assert_equal(type(ka), np.ndarray)
    #check if ka and kl have the same kernels
    assert_array_equal(kl[1], ka[1])
    assert_array_equal(kl[9], ka[9])
    return
Beispiel #3
0
def check_shape():
    #check the shape with only tr
    kl = HPK_generator(X_tr).make_a_list(n)
    assert_equal(kl.shape, (n, X_tr.shape[0], X_tr.shape[0]))
    #check the shape with tr and test
    kl = HPK_generator(X_tr, X_te).make_a_list(n)
    assert_equal(kl.shape, (n, X_te.shape[0], X_tr.shape[0]))
    #check the shape with a klist generated from an other
    kr = kernel_list(X_tr, X_te, kl)
    assert_equal(kr.shape, (n, X_te.shape[0], X_tr.shape[0]))
    assert_equal(kl.shape, kr.shape)
    return
Beispiel #4
0
def check_init_from_klist():
    #check if a list generated from an other has the same kernels with only tr
    kl = HPK_generator(X_tr).make_a_list(n)
    kr = kernel_list(X_tr, X_tr, kl)
    assert_array_almost_equal(kl[0], kr[0])
    assert_array_almost_equal(kl[3], kr[3])
    #check if a list generated from an other has the same kernels with tr and test
    kl = HPK_generator(X_tr, X_te).make_a_list(n)
    kr = kernel_list(X_tr, X_te, kl)
    assert_array_almost_equal(kl[2], kr[2])
    assert_array_almost_equal(kl[7], kr[7])
    return
Beispiel #5
0
def check_generated():
    #check if kl can be generated by a generator with tr set
    kl = SFK_generator(X_tr).make_a_list(n)
    assert_equal(kl.__class__.__name__, 'kernel_list')
    kl = HPK_generator(X_tr).make_a_list(n)
    assert_equal(kl.__class__.__name__, 'kernel_list')
    #check if kl can be generated by a generator with tr and test as inoput
    kl = SFK_generator(X_tr, X_te).make_a_list(n)
    assert_equal(kl.__class__.__name__, 'kernel_list')
    kl = HPK_generator(X_tr, X_te).make_a_list(n)
    assert_equal(kl.__class__.__name__, 'kernel_list')
    #check if make_a_list(n) perform exactly n kernels in list
    assert_equal(len(kl), n)
    return
Beispiel #6
0
def check_HPKs():
    #check a training list
    kl = HPK_generator(X_tr).make_a_list(10)
    assert_array_almost_equal(kl[0], linear_kernel(X_tr))
    assert_array_almost_equal(
        kl[2], polynomial_kernel(X_tr, gamma=1, coef0=0, degree=3))
    assert_array_almost_equal(
        kl[5], polynomial_kernel(X_tr, gamma=1, coef0=0, degree=6))
    #check a test list
    kt = HPK_generator(X_tr, X_te).make_a_list(10)
    assert_array_almost_equal(kt[0], linear_kernel(X_te, X_tr))
    assert_array_almost_equal(
        kt[2], polynomial_kernel(X_te, X_tr, gamma=1, coef0=0, degree=3))
    assert_array_almost_equal(
        kt[5], polynomial_kernel(X_te, X_tr, gamma=1, coef0=0, degree=6))
Beispiel #7
0
def check_complex_operations():
    ##check multiplication and division with a scalar value
    #check if normal division works and it does not change the operands
    v = [i for i in range(1, 11)]
    kl1 = HPK_generator(X_tr).make_a_list(n)
    k = kl1[3].copy()
    kl2 = kl1 / v
    assert_array_almost_equal(kl2[9], kl1[9] / v[9])
    assert_array_almost_equal(kl2[0], kl1[0] / v[0])
    assert_array_almost_equal(kl2[6], kl1[6] / v[6])
    assert_array_almost_equal(kl2[3], k / v[3])
    #normal div does not affect first operand
    assert_array_almost_equal(kl1[3], k)

    #check if idiv works correctly
    kl1 /= v
    assert_array_almost_equal(kl1[2], kl2[2])
    #check if mul works correctly
    kl1 = SFK_generator(X_tr).make_a_list(n)
    k = kl1[1]
    kl2 = kl1 * v
    assert_array_almost_equal(kl2[1], k * v[1])
    kk = kl1[1]
    assert_array_almost_equal(k, kk)
    assert_array_almost_equal(kl2[6], kl1[6] * v[6])
    #check if imul works correctly
    kl1 *= v
    assert_array_almost_equal(kl1[2], kl2[2])
    return
Beispiel #8
0
def check_HPKs():
    kl = HPK_generator(X_tr).make_a_list(n)
    #check if the first kernel in list has degree 1 and correspond to linear kernel
    assert_array_almost_equal(kl[0], linear_kernel(X_tr))
    assert_array_almost_equal(
        kl[0], polynomial_kernel(X_tr, degree=1, coef0=0, gamma=1))
    #check if the 4-th element in list is a HPK with degree 4
    assert_array_almost_equal(kl[3], linear_kernel(X_tr)**4)
    assert_array_almost_equal(
        kl[3], polynomial_kernel(X_tr, degree=4, coef0=0, gamma=1))
    #the same test with tr and test sets
    kl = HPK_generator(X_tr, X_te).make_a_list(n)
    assert_array_almost_equal(kl[2], linear_kernel(X_te, X_tr)**3)
    assert_array_almost_equal(
        kl[2], polynomial_kernel(X_te, X_tr, degree=3, coef0=0, gamma=1))
    return
Beispiel #9
0
 def __init__(self, estimator=SVC(), step=1.0, generator=HPK_generator(n=10), multiclass_strategy='ova', max_iter=10000, tol=1e-9, verbose=False, n_folds=1, random_state=42,lam=0):
     super(self.__class__, self).__init__(estimator=estimator, generator=generator, multiclass_strategy=multiclass_strategy, how_to=summation, max_iter=max_iter, verbose=verbose)
     self.step = step
     self.lam = lam
     self.tol = tol
     self.n_folds = n_folds
     self.random_state = random_state
Beispiel #10
0
def check_getitem():
    #check if getitem return a valid kernel
    kl = HPK_generator(X_tr).make_a_list(n)
    k = kl[3]
    #check if k is an ndarray
    assert_equal(type(k), np.ndarray)
    #check the shape of the single kernel
    assert_equal(k.shape, (X_tr.shape[0], X_tr.shape[0]))
    #the same with test and SFK
    kl = SFK_generator(X_tr, X_te).make_a_list(n)
    k = kl[4]
    assert_equal(k.shape, (X_te.shape[0], X_tr.shape[0]))
    assert np.min(k) >= 0, 'error: kernel matrix with  a negative value'
    return
Beispiel #11
0
 def __init__(self,
              estimator=KOMD(lam=0.1),
              lam=0.1,
              generator=HPK_generator(n=10),
              multiclass_strategy='ova',
              max_iter=100,
              verbose=False):
     super(self.__class__,
           self).__init__(estimator=estimator,
                          generator=generator,
                          multiclass_strategy=multiclass_strategy,
                          how_to=summation,
                          max_iter=max_iter,
                          verbose=verbose)
     self.lam = lam
Beispiel #12
0
 def __init__(self,
              estimator=SVC(),
              step=1,
              generator=HPK_generator(n=10),
              multiclass_strategy='ova',
              max_iter=1000,
              tol=1e-9,
              verbose=False):
     super(self.__class__,
           self).__init__(estimator=estimator,
                          generator=generator,
                          multiclass_strategy=multiclass_strategy,
                          how_to=summation,
                          max_iter=max_iter,
                          verbose=verbose)
     self.step = step
     self.tol = tol
Beispiel #13
0
import sys
import numpy as np
from numpy.testing import assert_array_equal, assert_array_almost_equal, assert_equal
from sklearn.datasets import load_iris, load_digits, load_breast_cancer
from sklearn.model_selection import KFold, ShuffleSplit
from MKLpy.lists import HPK_generator
from MKLpy.model_selection import *
from MKLpy.algorithms import EasyMKL

data = load_breast_cancer()  #digits()#iris()
X, Y = data.data, data.target
Y = np.array([1 if _y == Y[1] else -1 for _y in Y])
#Y = np.array([1 if _y < 5 else -1 for _y in Y])
n = X.shape[0]
y = np.array(list(range(n)))
KL = HPK_generator(X).make_a_list(10).to_array()


def check_cv3():
    return
    cv = KFold(3).split(Y)
    for train, test in cv:
        tr, te = cv3(train, test, 10)
        assert KL[tr].shape == (10, 100, 100)
        assert KL[te].shape == (10, 50, 100)


def check_train_test_split():
    #check shape with train_size (%)
    Ktr, Kte, Ytr, Yte = train_test_split(KL, Y, train_size=.3)
    assert Ktr.shape == (10, n * 0.3, n * 0.3), 'Shape error'
Beispiel #14
0
def check_edit():
    #check 'edit' with a simple transformation
    def stuff(X):
        return X * 2

    kl = HPK_generator(X_tr).make_a_list(10)
    K = kl[2]
    kl.edit(stuff)
    assert_array_almost_equal(K * 2, kl[2])
    #check with normalization
    kl = HPK_generator(X_tr).make_a_list(10)
    K = kl[2]
    kl.edit(kernel_normalization)
    assert_array_almost_equal(kernel_normalization(K), kl[2])
    #check with kernel centering
    kl = HPK_generator(X_tr).make_a_list(10)
    K = kl[2]
    kl.edit(kernel_centering)
    assert_array_almost_equal(kernel_centering(K), kl[2])
Beispiel #15
0
from MKLpy.algorithms.EasyMKL1 import EasyMKL as easy
from MKLpy.algorithms import EasyMKL as old
from MKLpy.lists import HPK_generator
from sklearn.datasets import load_digits
import numpy as np
from sklearn.model_selection import train_test_split as tts
from numpy.testing import assert_array_equal, assert_array_almost_equal, assert_equal
import sys
from sklearn.svm import SVC

data = load_digits()
X, Y = data.data, data.target
Y = [1 if y == Y[0] else -1 for y in Y]
Xtr, Xte, Ytr, Yte = tts(X, Y, train_size=200)
KL = HPK_generator(Xtr).make_a_list(10).to_array()
KLte = HPK_generator(Xtr, Xte).make_a_list(10).to_array()

clf_new = easy(lam=0, kernel='precomputed')
clf_old = old(lam=0, kernel='precomputed', tracenorm=False)
K_new = clf_new.arrange_kernel(KL, Ytr)
K_old = clf_old.arrange_kernel(KL, Ytr)

assert_array_almost_equal(K_new, K_old)

clf_new = easy(lam=0, kernel='precomputed').fit(KL, Ytr)
clf_old = old(lam=0, kernel='precomputed', tracenorm=False).fit(KL, Ytr)
y_new = clf_new.decision_function(KLte)
y_old = clf_old.decision_function(KLte)

assert_array_almost_equal(y_new, y_old)
Beispiel #16
0
from numpy.testing import assert_array_equal, assert_array_almost_equal, assert_equal
from MKLpy.lists import HPK_generator
from sklearn.datasets import load_iris
import numpy as np
import sys
from MKLpy.arrange import *
from sklearn.metrics.pairwise import polynomial_kernel as pk
from sklearn.model_selection import train_test_split

data = load_iris()
X, Y = data.data, data.target
Xtr, Xte, Ytr, Yte = train_test_split(X, Y, test_size=.3)

K_list = [pk(Xtr, degree=i, coef0=0, gamma=1) for i in range(1, 6)]
K_array = np.array(K_list)
K_klist = HPK_generator(Xtr).make_a_list(5)

K_list_te = [pk(Xte, Xtr, degree=i, coef0=0, gamma=1) for i in range(1, 6)]
K_array_te = np.array(K_list_te)
K_klist_te = HPK_generator(Xtr, Xte).make_a_list(5)


def check_summation():
    #check summation with 1 samples matrix
    truesum = K_list[0] + K_list[1] + K_list[2] + K_list[3] + K_list[4]
    sum_list = summation(K_list)
    sum_array = summation(K_array)
    sum_klist = summation(K_klist)
    assert_array_equal(sum_list, truesum)
    assert_array_equal(sum_list, sum_array)
    assert_array_equal(sum_list, sum_klist)