def test_make_bucket(): with warns(UserWarning): _ = make_kernel_bucket(kset) with raises(ValueError): _ = make_kernel_bucket('blah_invalid_strategy') # ensure correct values work for strategy in kernel_bucket_strategies: _ = make_kernel_bucket(strategy=strategy)
def test_optimal_kernel_estimators(): train_data, labels = make_classification(n_features=sample_dim, n_classes=2, n_samples=n_training) test_data = gen_random_sample(n_testing, sample_dim) # creating the smallest bucket, just with linear kernel, to speed up tests kb = make_kernel_bucket(strategy='linear_only') for OKEstimator in ( OptimalKernelSVC, OptimalKernelSVR, ): try: ok_est = OKEstimator(k_bucket=kb) except: raise RuntimeError('Unable to instantiate OptimalKernelSVR!') # disabling sklearn checks to avoid headaches with their internal checks _test_estimator_can_fit_predict(ok_est) for invalid_value in (np.random.randint(10), 10.1, ('tuple')): with raises(ValueError): ok_est = OKEstimator(k_bucket=invalid_value) ok_est.fit(train_data, labels) ok_est = OKEstimator(k_bucket=kb) ok_est.set_params(k_bucket=kb)
def test_linear_comb(): kset = make_kernel_bucket('light') weights = randn(kset.size) kset.attach_to(sample_data) lc = linear_combination(kset, weights) with raises(ValueError): lc = linear_combination(kset, randn(kset.size + 1))
def test_linear_comb(): kset = make_kernel_bucket('light') weights = randn(kset.size) kset.attach_to(sample_data) lc = linear_combination(kset, weights) with raises(ValueError): lc = linear_combination(kset, randn(kset.size + 1)) zero_weights = np.zeros((kset.size,1)) lc0 = linear_combination(kset, zero_weights) if not np.isclose(lc0.max(), 0.0): raise ValueError('zero weights do not lead to zero KM!') with raises(RuntimeError): lc0 = linear_combination(kset, zero_weights, norm_weights=True)
def test_attributes(): kset.set_attr('name', 'linear') for km in kset: assert km.get_attr('name') == 'linear' assert km.get_attr('noname', '404') == '404' values = np.random.rand(kset.size) kset.set_attr('weight', values) for ii, km in enumerate(kset): assert km.get_attr('weight') == values[ii] kb = make_kernel_bucket() kb.attach_to(sample_data, attr_name='a', attr_value='b') # differing length with raises(ValueError): kb.set_attr('a', ['value'] * (kb.size - 1)) kb.get_attr('a')
def test_composite_kernels(): kset = make_kernel_bucket() kset.attach_to(gen_random_sample(num_samples, sample_dim)) for ck in (AverageKernel, SumKernel, WeightedAverageKernel, ProductKernel): if issubclass(ck, WeightedAverageKernel): result_km = ck(kset, np.random.rand(kset.size)) else: result_km = ck(kset) if not isinstance(result_km, CompositeKernel): raise TypeError(' Composite kernel {} not defined properly: ' 'it must be a child of {}' ''.format(result_km, CompositeKernel)) result_km.fit() reqd_attrs = ('composite_KM', 'full') for reqd in reqd_attrs: if not hasattr(result_km, reqd): raise TypeError('{} does not have attr {}'.format(result_km, reqd))
from kernelmethods.sampling import make_kernel_bucket from kernelmethods.ranking import find_optimal_kernel, rank_kernels, \ alignment_ranking, min_max_scale, CV_ranking, get_estimator import numpy as np from pytest import raises, warns kb = make_kernel_bucket() def test_misc(): raises(TypeError, find_optimal_kernel, 'bucket', None, None) with raises(NotImplementedError): rank_kernels(kb, None, method='align/corr')
def fit(self, X, y, sample_weight=None): """Estimate the optimal kernel, and fit a SVM based on the custom kernel. Parameters ---------- X : {array-like, sparse matrix}, shape (n_samples, n_features) Training vectors, where n_samples is the number of samples and n_features is the number of features. For kernel="precomputed", the expected shape of X is (n_samples, n_samples). y : array-like, shape (n_samples,) Target values (class labels in classification, real numbers in regression) sample_weight : array-like, shape (n_samples,) Per-sample weights. Rescale C per sample. Higher weights force the classifier to put more emphasis on these points. Returns ------- self : object Notes ------ If X and y are not C-ordered and contiguous arrays of np.float64 and X is not a scipy.sparse.csr_matrix, X and/or y may be copied. If X is a dense array, then the other methods will not support sparse matrices as input. """ if isinstance(self.k_bucket, str): try: # using a new internal variable to retain user supplied param self._k_bucket = make_kernel_bucket(self.k_bucket) except: raise ValueError('Input for k_func can only an instance of ' 'KernelBucket or a sampling strategy to generate ' 'one with make_kernel_bucket.' 'sampling strategy must be one of {}' ''.format(cfg.kernel_bucket_strategies)) elif isinstance(self.k_bucket, KernelBucket): self._k_bucket = deepcopy(self.k_bucket) else: raise ValueError('Input for k_func can only an instance of ' 'KernelBucket or a sampling strategy to generate ' 'one with make_kernel_bucket') self._train_X, self._train_y = check_X_y(X, y, y_numeric=True) self.opt_kernel_ = self._find_optimal_kernel() super().fit(X=self.opt_kernel_.full, y=self._train_y, sample_weight=sample_weight) # temporary hack to pass sklearn estimator checks till a bug is fixed # for more see: https://github.com/scikit-learn/scikit-learn/issues/14712 self.n_iter_ = 1 return self
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 np.random.rand(num_samples, sample_dim) kset = make_kernel_bucket('light') kset.attach_to(sample_data) def test_make_bucket(): with warns(UserWarning): _ = make_kernel_bucket(kset) with raises(ValueError): _ = make_kernel_bucket('blah_invalid_strategy') # ensure correct values work for strategy in kernel_bucket_strategies: _ = make_kernel_bucket(strategy=strategy)