diff --git a/sklearn/gaussian_process/tests/test_gpc.py b/sklearn/gaussian_process/tests/test_gpc.py index d429018cb36b3..16b2507e45f18 100644 --- a/sklearn/gaussian_process/tests/test_gpc.py +++ b/sklearn/gaussian_process/tests/test_gpc.py @@ -34,8 +34,7 @@ def f(x): def test_predict_consistent(): - """ Check binary predict decision has also predicted probability above 0.5. - """ + # Check binary predict decision has also predicted probability above 0.5. for kernel in kernels: gpc = GaussianProcessClassifier(kernel=kernel).fit(X, y) assert_array_equal(gpc.predict(X), @@ -43,7 +42,7 @@ def test_predict_consistent(): def test_lml_improving(): - """ Test that hyperparameter-tuning improves log-marginal likelihood. """ + # Test that hyperparameter-tuning improves log-marginal likelihood. for kernel in kernels: if kernel == fixed_kernel: continue @@ -53,7 +52,7 @@ def test_lml_improving(): def test_lml_precomputed(): - """ Test that lml of optimized kernel is stored correctly. """ + # Test that lml of optimized kernel is stored correctly. for kernel in kernels: gpc = GaussianProcessClassifier(kernel=kernel).fit(X, y) assert_almost_equal(gpc.log_marginal_likelihood(gpc.kernel_.theta), @@ -61,7 +60,7 @@ def test_lml_precomputed(): def test_converged_to_local_maximum(): - """ Test that we are in local maximum after hyperparameter-optimization.""" + # Test that we are in local maximum after hyperparameter-optimization. for kernel in kernels: if kernel == fixed_kernel: continue @@ -76,7 +75,7 @@ def test_converged_to_local_maximum(): def test_lml_gradient(): - """ Compare analytic and numeric gradient of log marginal likelihood. """ + # Compare analytic and numeric gradient of log marginal likelihood. for kernel in kernels: gpc = GaussianProcessClassifier(kernel=kernel).fit(X, y) @@ -91,10 +90,8 @@ def test_lml_gradient(): def test_random_starts(): - """ - Test that an increasing number of random-starts of GP fitting only - increases the log marginal likelihood of the chosen theta. - """ + # Test that an increasing number of random-starts of GP fitting only + # increases the log marginal likelihood of the chosen theta. n_samples, n_features = 25, 2 np.random.seed(0) rng = np.random.RandomState(0) @@ -115,7 +112,7 @@ def test_random_starts(): def test_custom_optimizer(): - """ Test that GPC can use externally defined optimizers. """ + # Test that GPC can use externally defined optimizers. # Define a dummy optimizer that simply tests 50 random hyperparameters def optimizer(obj_func, initial_theta, bounds): rng = np.random.RandomState(0) @@ -140,7 +137,7 @@ def optimizer(obj_func, initial_theta, bounds): def test_multi_class(): - """ Test GPC for multi-class classification problems. """ + # Test GPC for multi-class classification problems. for kernel in kernels: gpc = GaussianProcessClassifier(kernel=kernel) gpc.fit(X, y_mc) @@ -153,7 +150,7 @@ def test_multi_class(): def test_multi_class_n_jobs(): - """ Test that multi-class GPC produces identical results with n_jobs>1. """ + # Test that multi-class GPC produces identical results with n_jobs>1. for kernel in kernels: gpc = GaussianProcessClassifier(kernel=kernel) gpc.fit(X, y_mc) diff --git a/sklearn/gaussian_process/tests/test_gpr.py b/sklearn/gaussian_process/tests/test_gpr.py index 98b2d63f6dd09..e62a2c1b14d31 100644 --- a/sklearn/gaussian_process/tests/test_gpr.py +++ b/sklearn/gaussian_process/tests/test_gpr.py @@ -36,7 +36,7 @@ def f(x): def test_gpr_interpolation(): - """Test the interpolating property for different kernels.""" + # Test the interpolating property for different kernels. for kernel in kernels: gpr = GaussianProcessRegressor(kernel=kernel).fit(X, y) y_pred, y_cov = gpr.predict(X, return_cov=True) @@ -46,7 +46,7 @@ def test_gpr_interpolation(): def test_lml_improving(): - """ Test that hyperparameter-tuning improves log-marginal likelihood. """ + # Test that hyperparameter-tuning improves log-marginal likelihood. for kernel in kernels: if kernel == fixed_kernel: continue @@ -56,7 +56,7 @@ def test_lml_improving(): def test_lml_precomputed(): - """ Test that lml of optimized kernel is stored correctly. """ + # Test that lml of optimized kernel is stored correctly. for kernel in kernels: gpr = GaussianProcessRegressor(kernel=kernel).fit(X, y) assert_equal(gpr.log_marginal_likelihood(gpr.kernel_.theta), @@ -64,7 +64,7 @@ def test_lml_precomputed(): def test_converged_to_local_maximum(): - """ Test that we are in local maximum after hyperparameter-optimization.""" + # Test that we are in local maximum after hyperparameter-optimization. for kernel in kernels: if kernel == fixed_kernel: continue @@ -79,7 +79,7 @@ def test_converged_to_local_maximum(): def test_solution_inside_bounds(): - """ Test that hyperparameter-optimization remains in bounds""" + # Test that hyperparameter-optimization remains in bounds# for kernel in kernels: if kernel == fixed_kernel: continue @@ -95,7 +95,7 @@ def test_solution_inside_bounds(): def test_lml_gradient(): - """ Compare analytic and numeric gradient of log marginal likelihood. """ + # Compare analytic and numeric gradient of log marginal likelihood. for kernel in kernels: gpr = GaussianProcessRegressor(kernel=kernel).fit(X, y) @@ -110,7 +110,7 @@ def test_lml_gradient(): def test_prior(): - """ Test that GP prior has mean 0 and identical variances.""" + # Test that GP prior has mean 0 and identical variances. for kernel in kernels: gpr = GaussianProcessRegressor(kernel=kernel) @@ -125,7 +125,7 @@ def test_prior(): def test_sample_statistics(): - """ Test that statistics of samples drawn from GP are correct.""" + # Test that statistics of samples drawn from GP are correct. for kernel in kernels: gpr = GaussianProcessRegressor(kernel=kernel).fit(X, y) @@ -140,14 +140,14 @@ def test_sample_statistics(): def test_no_optimizer(): - """ Test that kernel parameters are unmodified when optimizer is None.""" + # Test that kernel parameters are unmodified when optimizer is None. kernel = RBF(1.0) gpr = GaussianProcessRegressor(kernel=kernel, optimizer=None).fit(X, y) assert_equal(np.exp(gpr.kernel_.theta), 1.0) def test_predict_cov_vs_std(): - """ Test that predicted std.-dev. is consistent with cov's diagonal.""" + # Test that predicted std.-dev. is consistent with cov's diagonal. for kernel in kernels: gpr = GaussianProcessRegressor(kernel=kernel).fit(X, y) y_mean, y_cov = gpr.predict(X2, return_cov=True) @@ -156,7 +156,7 @@ def test_predict_cov_vs_std(): def test_anisotropic_kernel(): - """ Test that GPR can identify meaningful anisotropic length-scales. """ + # Test that GPR can identify meaningful anisotropic length-scales. # We learn a function which varies in one dimension ten-times slower # than in the other. The corresponding length-scales should differ by at # least a factor 5 @@ -171,10 +171,8 @@ def test_anisotropic_kernel(): def test_random_starts(): - """ - Test that an increasing number of random-starts of GP fitting only - increases the log marginal likelihood of the chosen theta. - """ + # Test that an increasing number of random-starts of GP fitting only + # increases the log marginal likelihood of the chosen theta. n_samples, n_features = 25, 2 np.random.seed(0) rng = np.random.RandomState(0) @@ -197,11 +195,10 @@ def test_random_starts(): def test_y_normalization(): - """ Test normalization of the target values in GP + # Test normalization of the target values in GP - Fitting non-normalizing GP on normalized y and fitting normalizing GP - on unnormalized y should yield identical results - """ + # Fitting non-normalizing GP on normalized y and fitting normalizing GP + # on unnormalized y should yield identical results y_mean = y.mean(0) y_norm = y - y_mean for kernel in kernels: @@ -226,7 +223,7 @@ def test_y_normalization(): def test_y_multioutput(): - """ Test that GPR can deal with multi-dimensional target values""" + # Test that GPR can deal with multi-dimensional target values y_2d = np.vstack((y, y * 2)).T # Test for fixed kernel that first dimension of 2d GP equals the output @@ -269,7 +266,7 @@ def test_y_multioutput(): def test_custom_optimizer(): - """ Test that GPR can use externally defined optimizers. """ + # Test that GPR can use externally defined optimizers. # Define a dummy optimizer that simply tests 50 random hyperparameters def optimizer(obj_func, initial_theta, bounds): rng = np.random.RandomState(0) @@ -294,7 +291,7 @@ def optimizer(obj_func, initial_theta, bounds): def test_duplicate_input(): - """ Test GPR can handle two different output-values for the same input. """ + # Test GPR can handle two different output-values for the same input. for kernel in kernels: gpr_equal_inputs = \ GaussianProcessRegressor(kernel=kernel, alpha=1e-2) diff --git a/sklearn/gaussian_process/tests/test_kernels.py b/sklearn/gaussian_process/tests/test_kernels.py index 116fad8ddaf93..c51b89eeaa18d 100644 --- a/sklearn/gaussian_process/tests/test_kernels.py +++ b/sklearn/gaussian_process/tests/test_kernels.py @@ -3,7 +3,6 @@ # Author: Jan Hendrik Metzen # License: BSD 3 clause -from collections import Hashable from sklearn.externals.funcsigs import signature import numpy as np @@ -50,7 +49,7 @@ def test_kernel_gradient(): - """ Compare analytic and numeric gradient of kernels. """ + # Compare analytic and numeric gradient of kernels. for kernel in kernels: K, K_gradient = kernel(X, eval_gradient=True) @@ -70,7 +69,7 @@ def eval_kernel_for_theta(theta): def test_kernel_theta(): - """ Check that parameter vector theta of kernel is set correctly. """ + # Check that parameter vector theta of kernel is set correctly. for kernel in kernels: if isinstance(kernel, KernelOperator) \ or isinstance(kernel, Exponentiation): # skip non-basic kernels @@ -111,8 +110,8 @@ def test_kernel_theta(): assert_array_equal(K_gradient[..., :i], K_gradient_new[..., :i]) if i + 1 < len(kernel.hyperparameters): - assert_equal(theta[i+1:], new_kernel.theta[i:]) - assert_array_equal(K_gradient[..., i+1:], + assert_equal(theta[i + 1:], new_kernel.theta[i:]) + assert_array_equal(K_gradient[..., i + 1:], K_gradient_new[..., i:]) # Check that values of theta are modified correctly @@ -126,7 +125,7 @@ def test_kernel_theta(): def test_auto_vs_cross(): - """ Auto-correlation and cross-correlation should be consistent. """ + # Auto-correlation and cross-correlation should be consistent. for kernel in kernels: if kernel == kernel_white: continue # Identity is not satisfied on diagonal @@ -136,7 +135,7 @@ def test_auto_vs_cross(): def test_kernel_diag(): - """ Test that diag method of kernel returns consistent results. """ + # Test that diag method of kernel returns consistent results. for kernel in kernels: K_call_diag = np.diag(kernel(X)) K_diag = kernel.diag(X) @@ -144,7 +143,7 @@ def test_kernel_diag(): def test_kernel_operator_commutative(): - """ Adding kernels and multiplying kernels should be commutative. """ + # Adding kernels and multiplying kernels should be commutative. # Check addition assert_almost_equal((RBF(2.0) + 1.0)(X), (1.0 + RBF(2.0))(X)) @@ -155,7 +154,7 @@ def test_kernel_operator_commutative(): def test_kernel_anisotropic(): - """ Anisotropic kernel should be consistent with isotropic kernels.""" + # Anisotropic kernel should be consistent with isotropic kernels. kernel = 3.0 * RBF([0.5, 2.0]) K = kernel(X) @@ -176,7 +175,7 @@ def test_kernel_anisotropic(): def test_kernel_stationary(): - """ Test stationarity of kernels.""" + # Test stationarity of kernels. for kernel in kernels: if not kernel.is_stationary(): continue @@ -185,7 +184,7 @@ def test_kernel_stationary(): def check_hyperparameters_equal(kernel1, kernel2): - """Check that hyperparameters of two kernels are equal""" + # Check that hyperparameters of two kernels are equal for attr in set(dir(kernel1) + dir(kernel2)): if attr.startswith("hyperparameter_"): attr_value1 = getattr(kernel1, attr) @@ -194,7 +193,7 @@ def check_hyperparameters_equal(kernel1, kernel2): def test_kernel_clone(): - """ Test that sklearn's clone works correctly on kernels. """ + # Test that sklearn's clone works correctly on kernels. bounds = (1e-5, 1e5) for kernel in kernels: kernel_cloned = clone(kernel) @@ -219,7 +218,8 @@ def test_kernel_clone(): params = kernel.get_params() # RationalQuadratic kernel is isotropic. isotropic_kernels = (ExpSineSquared, RationalQuadratic) - if 'length_scale' in params and not isinstance(kernel, isotropic_kernels): + if 'length_scale' in params and not isinstance(kernel, + isotropic_kernels): length_scale = params['length_scale'] if np.iterable(length_scale): params['length_scale'] = length_scale[0] @@ -232,11 +232,12 @@ def test_kernel_clone(): assert_equal(kernel_cloned_clone.get_params(), kernel_cloned.get_params()) assert_not_equal(id(kernel_cloned_clone), id(kernel_cloned)) - yield check_hyperparameters_equal, kernel_cloned, kernel_cloned_clone + yield (check_hyperparameters_equal, kernel_cloned, + kernel_cloned_clone) def test_matern_kernel(): - """ Test consistency of Matern kernel for special values of nu. """ + # Test consistency of Matern kernel for special values of nu. K = Matern(nu=1.5, length_scale=1.0)(X) # the diagonal elements of a matern kernel are 1 assert_array_almost_equal(np.diag(K), np.ones(X.shape[0])) @@ -255,7 +256,7 @@ def test_matern_kernel(): def test_kernel_versus_pairwise(): - """Check that GP kernels can also be used as pairwise kernels.""" + # Check that GP kernels can also be used as pairwise kernels. for kernel in kernels: # Test auto-kernel if kernel != kernel_white: @@ -272,7 +273,7 @@ def test_kernel_versus_pairwise(): def test_set_get_params(): - """Check that set_params()/get_params() is consistent with kernel.theta.""" + # Check that set_params()/get_params() is consistent with kernel.theta. for kernel in kernels: # Test get_params() index = 0 @@ -282,7 +283,7 @@ def test_set_get_params(): continue size = hyperparameter.n_elements if size > 1: # anisotropic kernels - assert_almost_equal(np.exp(kernel.theta[index:index+size]), + assert_almost_equal(np.exp(kernel.theta[index:index + size]), params[hyperparameter.name]) index += size else: @@ -297,9 +298,9 @@ def test_set_get_params(): continue size = hyperparameter.n_elements if size > 1: # anisotropic kernels - kernel.set_params(**{hyperparameter.name: [value]*size}) - assert_almost_equal(np.exp(kernel.theta[index:index+size]), - [value]*size) + kernel.set_params(**{hyperparameter.name: [value] * size}) + assert_almost_equal(np.exp(kernel.theta[index:index + size]), + [value] * size) index += size else: kernel.set_params(**{hyperparameter.name: value}) @@ -308,7 +309,7 @@ def test_set_get_params(): def test_repr_kernels(): - """Smoke-test for repr in kernels.""" + # Smoke-test for repr in kernels. for kernel in kernels: repr(kernel)