def check_simple_operations(): ##check multiplication and division with a scalar value #check if normal division works and it does not change the operands kl1 = SFK_generator(X_tr).make_a_list(n) k = kl1[1] kl2 = kl1 / 10 assert_array_almost_equal(kl2[1], k / 10) kk = kl1[1] assert_array_almost_equal(k, kk) assert_array_almost_equal(kl2[6], kl1[6] / 10) #check if idiv works correctly kl1 /= 10 assert_array_almost_equal(kl1[2], kl2[2]) #check if mul work correctly kl1 = SFK_generator(X_tr).make_a_list(n) k = kl1[1] kl2 = kl1 * 10 assert_array_almost_equal(kl2[1], k * 10) kk = kl1[1] assert_array_almost_equal(k, kk) assert_array_almost_equal(kl2[6], kl1[6] * 10) #check if imul works correctly kl1 *= 10 assert_array_almost_equal(kl1[2], kl2[2]) return
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
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
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
def check_SFKs2(): #check a training list kl = SFK_generator(X_tr, func='rbf').make_a_list(20) ka = kl.to_array() assert_array_almost_equal(kl[0], ka[0]) assert_array_almost_equal(kl[3], ka[3]) #check a test list #kt = SFK_generator(X_tr,X_te).make_a_list(10) kt = kernel_list(X_tr, X_te, kl) kb = kt.to_array() assert_array_almost_equal(kt[0], kb[0]) assert_array_almost_equal(kt[3], kb[3]) #test with the selected feature list fl = kl.__getitem__(3, 'feature') k = rbf_kernel(X_tr[:, fl]) assert_array_almost_equal(k, kl[3]) #test with the function in list ff = kl.__getitem__(3, 'func') k = ff(X_tr[:, fl], X_tr[:, fl]) assert_array_equal(k, kl[3]) #check that train and test use the same functions assert_array_equal(kl.__getitem__(2, 'feature'), kt.__getitem__(2, 'feature')) assert_array_equal(kl.__getitem__(7, 'feature'), kt.__getitem__(7, 'feature')) kt.T = X_tr #one trick to assert_array_equal(kt[1], kl[1])
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
def check_SFKs(): #check if SFK gen works with a default params kl = SFK_generator(X_tr).make_a_list(n) #check if the list of feature used has the same length defined as input kl = SFK_generator(X_tr, n_feature=3).make_a_list(n) fl = kl.__getitem__(0, 'feature') assert_equal(len(kl.__getitem__(0, 'feature')), 3) assert_equal(len(kl.__getitem__(6, 'feature')), 3) #check if a kernel is consistent with his kernel function and the relative feature vector kl = SFK_generator(X_tr, n_feature=2, func='rbf').make_a_list(n) assert_array_almost_equal( kl[2], rbf_kernel(X_tr[:, kl.__getitem__(2, 'feature')])) assert_array_almost_equal( kl[5], rbf_kernel(X_tr[:, kl.__getitem__(5, 'feature')])) #the same with also a test set kl = SFK_generator(X_tr, X_te, n_feature=2, func='rbf').make_a_list(n) assert_array_almost_equal( kl[3], rbf_kernel(X_te[:, kl.__getitem__(3, 'feature')], X_tr[:, kl.__getitem__(3, 'feature')])) assert_array_almost_equal( kl[8], rbf_kernel(X_te[:, kl.__getitem__(8, 'feature')], X_tr[:, kl.__getitem__(8, 'feature')])) #check if a kernel function with parameters works well kl = SFK_generator(X_tr, n_feature=2, func='rbf', params={ 'gamma': 1 }).make_a_list(n) assert_array_almost_equal( kl[2], rbf_kernel(X_tr[:, kl.__getitem__(2, 'feature')], gamma=1)) assert_array_almost_equal( kl[5], rbf_kernel(X_tr[:, kl.__getitem__(5, 'feature')], gamma=1)) #check if a callable works well kl = SFK_generator(X_tr, func=rbf_kernel).make_a_list(n) assert_array_almost_equal( kl[2], rbf_kernel(X_tr[:, kl.__getitem__(2, 'feature')])) #check if a callable with params works well kl = SFK_generator(X_tr, func=rbf_kernel, params={ 'gamma': 2 }).make_a_list(n) assert_array_almost_equal( kl[2], rbf_kernel(X_tr[:, kl.__getitem__(2, 'feature')], gamma=2)) return