<s> """<STR_LIT>""" <EOL> from collections import Hashable <EOL> from sklearn . externals . funcsigs import signature <EOL> import numpy as np <EOL> from sklearn . gaussian_process . kernels import _approx_fprime <EOL> from sklearn . metrics . pairwise import PAIRWISE_KERNEL_FUNCTIONS , euclidean_distances , pairwise_kernels <EOL> from sklearn . gaussian_process . kernels import ( RBF , Matern , RationalQuadratic , ExpSineSquared , DotProduct , <EOL> ConstantKernel , WhiteKernel , PairwiseKernel , KernelOperator , <EOL> Exponentiation ) <EOL> from sklearn . base import clone <EOL> from sklearn . utils . testing import ( assert_equal , assert_almost_equal , <EOL> assert_not_equal , assert_array_equal , <EOL> assert_array_almost_equal ) <EOL> X = np . random . RandomState ( <NUM_LIT:0> ) . normal ( <NUM_LIT:0> , <NUM_LIT:1> , ( <NUM_LIT:5> , <NUM_LIT:2> ) ) <EOL> Y = np . random . RandomState ( <NUM_LIT:0> ) . normal ( <NUM_LIT:0> , <NUM_LIT:1> , ( <NUM_LIT:6> , <NUM_LIT:2> ) ) <EOL> kernel_white = RBF ( length_scale = <NUM_LIT> ) + WhiteKernel ( noise_level = <NUM_LIT> ) <EOL> kernels = [ RBF ( length_scale = <NUM_LIT> ) , RBF ( length_scale_bounds = ( <NUM_LIT:0.5> , <NUM_LIT> ) ) , <EOL> ConstantKernel ( constant_value = <NUM_LIT> ) , <EOL> <NUM_LIT> * RBF ( length_scale = <NUM_LIT> , length_scale_bounds = "<STR_LIT>" ) , <EOL> <NUM_LIT> * RBF ( length_scale = <NUM_LIT:0.5> ) , kernel_white , <EOL> <NUM_LIT> * RBF ( length_scale = [ <NUM_LIT:0.5> , <NUM_LIT> ] ) , <EOL> <NUM_LIT> * Matern ( length_scale = <NUM_LIT> , length_scale_bounds = "<STR_LIT>" ) , <EOL> <NUM_LIT> * Matern ( length_scale = <NUM_LIT:0.5> , nu = <NUM_LIT:0.5> ) , <EOL> <NUM_LIT> * Matern ( length_scale = <NUM_LIT> , nu = <NUM_LIT> ) , <EOL> <NUM_LIT> * Matern ( length_scale = <NUM_LIT> , nu = <NUM_LIT> ) , <EOL> <NUM_LIT> * Matern ( length_scale = [ <NUM_LIT:0.5> , <NUM_LIT> ] , nu = <NUM_LIT:0.5> ) , <EOL> <NUM_LIT> * Matern ( length_scale = [ <NUM_LIT> , <NUM_LIT:0.5> ] , nu = <NUM_LIT> ) , <EOL> <NUM_LIT> * Matern ( length_scale = [ <NUM_LIT:0.5> , <NUM_LIT:0.5> ] , nu = <NUM_LIT> ) , <EOL> RationalQuadratic ( length_scale = <NUM_LIT:0.5> , alpha = <NUM_LIT> ) , <EOL> ExpSineSquared ( length_scale = <NUM_LIT:0.5> , periodicity = <NUM_LIT> ) , <EOL> DotProduct ( sigma_0 = <NUM_LIT> ) , DotProduct ( sigma_0 = <NUM_LIT> ) ** <NUM_LIT:2> ] <EOL> for metric in PAIRWISE_KERNEL_FUNCTIONS : <EOL> if metric in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> continue <EOL> kernels . append ( PairwiseKernel ( gamma = <NUM_LIT:1.0> , metric = metric ) ) <EOL> def test_kernel_gradient ( ) : <EOL> """<STR_LIT>""" <EOL> for kernel in kernels : <EOL> K , K_gradient = kernel ( X , eval_gradient = True ) <EOL> assert_equal ( K_gradient . shape [ <NUM_LIT:0> ] , X . shape [ <NUM_LIT:0> ] ) <EOL> assert_equal ( K_gradient . shape [ <NUM_LIT:1> ] , X . shape [ <NUM_LIT:0> ] ) <EOL> assert_equal ( K_gradient . shape [ <NUM_LIT:2> ] , kernel . theta . shape [ <NUM_LIT:0> ] ) <EOL> def eval_kernel_for_theta ( theta ) : <EOL> kernel_clone = kernel . clone_with_theta ( theta ) <EOL> K = kernel_clone ( X , eval_gradient = False ) <EOL> return K <EOL> K_gradient_approx = _approx_fprime ( kernel . theta , eval_kernel_for_theta , <NUM_LIT> ) <EOL> assert_almost_equal ( K_gradient , K_gradient_approx , <NUM_LIT:4> ) <EOL> def test_kernel_theta ( ) : <EOL> """<STR_LIT>""" <EOL> for kernel in kernels : <EOL> if isinstance ( kernel , KernelOperator ) or isinstance ( kernel , Exponentiation ) : <EOL> continue <EOL> theta = kernel . theta <EOL> _ , K_gradient = kernel ( X , eval_gradient = True ) <EOL> init_sign = signature ( kernel . __class__ . __init__ ) . parameters . values ( ) <EOL> args = [ p . name for p in init_sign if p . name != '<STR_LIT>' ] <EOL> theta_vars = map ( lambda s : s . rstrip ( "<STR_LIT>" ) , <EOL> filter ( lambda s : s . endswith ( "<STR_LIT>" ) , args ) ) <EOL> assert_equal ( <EOL> set ( hyperparameter . name <EOL> for hyperparameter in kernel . hyperparameters ) , <EOL> set ( theta_vars ) ) <EOL> for i , hyperparameter in enumerate ( kernel . hyperparameters ) : <EOL> assert_equal ( theta [ i ] , <EOL> np . log ( getattr ( kernel , hyperparameter . name ) ) ) <EOL> for i , hyperparameter in enumerate ( kernel . hyperparameters ) : <EOL> params = kernel . get_params ( ) <EOL> params [ hyperparameter . name + "<STR_LIT>" ] = "<STR_LIT>" <EOL> kernel_class = kernel . __class__ <EOL> new_kernel = kernel_class ( ** params ) <EOL> _ , K_gradient_new = new_kernel ( X , eval_gradient = True ) <EOL> assert_equal ( theta . shape [ <NUM_LIT:0> ] , new_kernel . theta . shape [ <NUM_LIT:0> ] + <NUM_LIT:1> ) <EOL> assert_equal ( K_gradient . shape [ <NUM_LIT:2> ] , K_gradient_new . shape [ <NUM_LIT:2> ] + <NUM_LIT:1> ) <EOL> if i > <NUM_LIT:0> : <EOL> assert_equal ( theta [ : i ] , new_kernel . theta [ : i ] ) <EOL> assert_array_equal ( K_gradient [ ... , : i ] , <EOL> K_gradient_new [ ... , : i ] ) <EOL> if i + <NUM_LIT:1> < len ( kernel . hyperparameters ) : <EOL> assert_equal ( theta [ i + <NUM_LIT:1> : ] , new_kernel . theta [ i : ] ) <EOL> assert_array_equal ( K_gradient [ ... , i + <NUM_LIT:1> : ] , <EOL> K_gradient_new [ ... , i : ] ) <EOL> for i , hyperparameter in enumerate ( kernel . hyperparameters ) : <EOL> theta [ i ] = np . log ( <NUM_LIT> ) <EOL> kernel . theta = theta <EOL> assert_almost_equal ( getattr ( kernel , hyperparameter . name ) , <NUM_LIT> ) <EOL> setattr ( kernel , hyperparameter . name , <NUM_LIT> ) <EOL> assert_almost_equal ( kernel . theta [ i ] , np . log ( <NUM_LIT> ) ) <EOL> def test_auto_vs_cross ( ) : <EOL> """<STR_LIT>""" <EOL> for kernel in kernels : <EOL> if kernel == kernel_white : <EOL> continue <EOL> K_auto = kernel ( X ) <EOL> K_cross = kernel ( X , X ) <EOL> assert_almost_equal ( K_auto , K_cross , <NUM_LIT:5> ) <EOL> def test_kernel_diag ( ) : <EOL> """<STR_LIT>""" <EOL> for kernel in kernels : <EOL> K_call_diag = np . diag ( kernel ( X ) ) <EOL> K_diag = kernel . diag ( X ) <EOL> assert_almost_equal ( K_call_diag , K_diag , <NUM_LIT:5> ) <EOL> def test_kernel_operator_commutative ( ) : <EOL> """<STR_LIT>""" <EOL> assert_almost_equal ( ( RBF ( <NUM_LIT> ) + <NUM_LIT:1.0> ) ( X ) , <EOL> ( <NUM_LIT:1.0> + RBF ( <NUM_LIT> ) ) ( X ) ) <EOL> assert_almost_equal ( ( <NUM_LIT> * RBF ( <NUM_LIT> ) ) ( X ) , <EOL> ( RBF ( <NUM_LIT> ) * <NUM_LIT> ) ( X ) ) <EOL> def test_kernel_anisotropic ( ) : <EOL> """<STR_LIT>""" <EOL> kernel = <NUM_LIT> * RBF ( [ <NUM_LIT:0.5> , <NUM_LIT> ] ) <EOL> K = kernel ( X ) <EOL> X1 = np . array ( X ) <EOL> X1 [ : , <NUM_LIT:0> ] *= <NUM_LIT:4> <EOL> K1 = <NUM_LIT> * RBF ( <NUM_LIT> ) ( X1 ) <EOL> assert_almost_equal ( K , K1 ) <EOL> X2 = np . array ( X ) <EOL> X2 [ : , <NUM_LIT:1> ] /= <NUM_LIT:4> <EOL> K2 = <NUM_LIT> * RBF ( <NUM_LIT:0.5> ) ( X2 ) <EOL> assert_almost_equal ( K , K2 ) <EOL> kernel . theta = kernel . theta + np . log ( <NUM_LIT:2> ) <EOL> assert_array_equal ( kernel . theta , np . log ( [ <NUM_LIT> , <NUM_LIT:1.0> , <NUM_LIT> ] ) ) <EOL> assert_array_equal ( kernel . k2 . length_scale , [ <NUM_LIT:1.0> , <NUM_LIT> ] ) <EOL> def test_kernel_stationary ( ) : <EOL> """<STR_LIT>""" <EOL> for kernel in kernels : <EOL> if not kernel . is_stationary ( ) : <EOL> continue <EOL> K = kernel ( X , X + <NUM_LIT:1> ) <EOL> assert_almost_equal ( K [ <NUM_LIT:0> , <NUM_LIT:0> ] , np . diag ( K ) ) <EOL> def test_kernel_clone ( ) : <EOL> """<STR_LIT>""" <EOL> for kernel in kernels : <EOL> kernel_cloned = clone ( kernel ) <EOL> assert_equal ( kernel , kernel_cloned ) <EOL> assert_not_equal ( id ( kernel ) , id ( kernel_cloned ) ) <EOL> for attr in kernel . __dict__ . keys ( ) : <EOL> attr_value = getattr ( kernel , attr ) <EOL> attr_value_cloned = getattr ( kernel_cloned , attr ) <EOL> if attr . startswith ( "<STR_LIT>" ) : <EOL> assert_equal ( attr_value . name , attr_value_cloned . name ) <EOL> assert_equal ( attr_value . value_type , <EOL> attr_value_cloned . value_type ) <EOL> assert_array_equal ( attr_value . bounds , <EOL> attr_value_cloned . bounds ) <EOL> assert_equal ( attr_value . n_elements , <EOL> attr_value_cloned . n_elements ) <EOL> elif np . iterable ( attr_value ) : <EOL> for i in range ( len ( attr_value ) ) : <EOL> if np . iterable ( attr_value [ i ] ) : <EOL> assert_array_equal ( attr_value [ i ] , <EOL> attr_value_cloned [ i ] ) <EOL> else : <EOL> assert_equal ( attr_value [ i ] , attr_value_cloned [ i ] ) <EOL> else : <EOL> assert_equal ( attr_value , attr_value_cloned ) <EOL> if not isinstance ( attr_value , Hashable ) : <EOL> assert_not_equal ( id ( attr_value ) , id ( attr_value_cloned ) ) <EOL> def test_matern_kernel ( ) : <EOL> """<STR_LIT>""" <EOL> K = Matern ( nu = <NUM_LIT> , length_scale = <NUM_LIT:1.0> ) ( X ) <EOL> assert_array_almost_equal ( np . diag ( K ) , np . ones ( X . shape [ <NUM_LIT:0> ] ) ) <EOL> K_absexp = np . exp ( - euclidean_distances ( X , X , squared = False ) ) <EOL> K = Matern ( nu = <NUM_LIT:0.5> , length_scale = <NUM_LIT:1.0> ) ( X ) <EOL> assert_array_almost_equal ( K , K_absexp ) <EOL> tiny = <NUM_LIT> <EOL> for nu in [ <NUM_LIT:0.5> , <NUM_LIT> , <NUM_LIT> ] : <EOL> K1 = Matern ( nu = nu , length_scale = <NUM_LIT:1.0> ) ( X ) <EOL> K2 = Matern ( nu = nu + tiny , length_scale = <NUM_LIT:1.0> ) ( X ) <EOL> assert_array_almost_equal ( K1 , K2 ) <EOL> def test_kernel_versus_pairwise ( ) : <EOL> """<STR_LIT>""" <EOL> for kernel in kernels : <EOL> if kernel != kernel_white : <EOL> K1 = kernel ( X ) <EOL> K2 = pairwise_kernels ( X , metric = kernel ) <EOL> assert_array_almost_equal ( K1 , K2 ) <EOL> K1 = kernel ( X , Y ) <EOL> K2 = pairwise_kernels ( X , Y , metric = kernel ) <EOL> assert_array_almost_equal ( K1 , K2 ) <EOL> def test_set_get_params ( ) : <EOL> """<STR_LIT>""" <EOL> for kernel in kernels : <EOL> index = <NUM_LIT:0> <EOL> params = kernel . get_params ( ) <EOL> for hyperparameter in kernel . hyperparameters : <EOL> if hyperparameter . bounds is "<STR_LIT>" : <EOL> continue <EOL> size = hyperparameter . n_elements <EOL> if size > <NUM_LIT:1> : <EOL> assert_almost_equal ( np . exp ( kernel . theta [ index : index + size ] ) , <EOL> params [ hyperparameter . name ] ) <EOL> index += size <EOL> else : <EOL> assert_almost_equal ( np . exp ( kernel . theta [ index ] ) , <EOL> params [ hyperparameter . name ] ) <EOL> index += <NUM_LIT:1> <EOL> index = <NUM_LIT:0> <EOL> value = <NUM_LIT:10> <EOL> for hyperparameter in kernel . hyperparameters : <EOL> if hyperparameter . bounds is "<STR_LIT>" : <EOL> continue <EOL> size = hyperparameter . n_elements <EOL> if size > <NUM_LIT:1> : <EOL> kernel . set_params ( ** { hyperparameter . name : [ value ] * size } ) <EOL> assert_almost_equal ( np . exp ( kernel . theta [ index : index + size ] ) , <EOL> [ value ] * size ) <EOL> index += size <EOL> else : <EOL> kernel . set_params ( ** { hyperparameter . name : value } ) <EOL> assert_almost_equal ( np . exp ( kernel . theta [ index ] ) , value ) <EOL> index += <NUM_LIT:1> </s>
