def __init__(self, k_func=GaussianKernel(), learner_id='SVC', normalized=False): """ Constructor for the KernelMachine class. Parameters ---------- k_func : KernelFunction The kernel function the kernel machine bases itself on learner_id : str Identifier for the estimator to be built based on the kernel function. Options: ``SVC`` and ``SVR``. Default: ``SVC`` (classifier version of SVM) normalized : flag Flag to indicate whether to keep the kernel matrix normalized. Default: False """ self.k_func = k_func self.learner_id = learner_id self.normalized = normalized self._estimator, self.param_grid = get_estimator(self.learner_id)
def test_gaussian_kernel(sample_dim, num_samples, sigma): """Tests specific for Gaussian kernel.""" gaussian = GaussianKernel(sigma=sigma, skip_input_checks=False) _test_for_all_kernels(gaussian, sample_dim) _test_func_is_valid_kernel(gaussian, sample_dim, num_samples)
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()
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
return _ensure_min_eps(np.sqrt(1.0 / (2 * gamma))) def gamma_from_sigma(sigma=0.1): return _ensure_min_eps(1.0 / (2 * sigma**2)) for name, ds_path in zip(ds_names, ds_paths): time_stamp = strftime("%H:%M:%S", gmtime()) X, y = load_svmlight_file(ds_path) X = X.toarray() print('\n{:10} {:20} {}'.format(time_stamp, name, X.shape)) gamma = 0.1 skl_svm = SVC(C=1.0, kernel='rbf', gamma=gamma) ss_cv1 = ShuffleSplit(n_splits=20, train_size=0.8, test_size=0.2) scores_skl = cross_val_score(skl_svm, X, y, cv=ss_cv1) ker_func = GaussianKernel(sigma=sigma_from_gamma(gamma)) km_svm = KernelMachine(k_func=ker_func, learner_id='SVM', normalized=False) ss_cv2 = ShuffleSplit(n_splits=20, train_size=0.8, test_size=0.2) scores_km = cross_val_score(km_svm, X, y, cv=ss_cv2) print('\tSKLearn Accuracy: {:.4f} +/- {:.4f}' ''.format(np.mean(scores_skl), np.std(scores_skl))) print('\tKM SVM Accuracy: {:.4f} +/- {:.4f}' ''.format(np.mean(scores_km), np.std(scores_km)))