`L irLTdZddlZddlZddlmZmZddlmZddlm Z ddl Z ddl m Z mZmZddlmZmZdd lmZdd lmZdd lmZdd lmZd ZGddeddZGddeZGddZGddZGddZ GddeZ!GddeZ"Gdde"Z#Gd d!e"Z$Gd"d#eZ%Gd$d%ee eZ&Gd&d'ee eZ'Gd(d)eeeZ(Gd*d+e(Z)Gd,d-eeeZ*Gd.d/eeeZ+Gd0d1eZ,d5d2Z-Gd3d4eZ.y)6zRA set of kernels that can be combined by operators and used in Gaussian processes.N)ABCMetaabstractmethod) namedtuple) signature)cdistpdist squareform)gammakv)clone)ConvergenceWarning)pairwise_kernels) _num_samplesc^tj|jt}tj|dkDr t dtj|dk(rI|j d|j dk7r*t d|j d|j dfz|S)Nz2length_scale cannot be of dimension greater than 1rzKAnisotropic kernel must have the same number of dimensions as data (%d!=%d))npsqueezeastypefloatndim ValueErrorshape)X length_scales f/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sklearn/gaussian_process/kernels.py_check_length_scaler(s::l+2259L ww|q MNN ww|!aggajL4F4Fq4I&I *-9-?-?-BAGGAJ,O P   c.eZdZdZdZdfd ZdZxZS)HyperparameteraIA kernel hyperparameter's specification in form of a namedtuple. .. versionadded:: 0.18 Attributes ---------- name : str The name of the hyperparameter. Note that a kernel using a hyperparameter with name "x" must have the attributes self.x and self.x_bounds value_type : str The type of the hyperparameter. Currently, only "numeric" hyperparameters are supported. bounds : pair of floats >= 0 or "fixed" The lower and upper bound on the parameter. If n_elements>1, a pair of 1d array with n_elements each may be given alternatively. If the string "fixed" is passed as bounds, the hyperparameter's value cannot be changed. n_elements : int, default=1 The number of elements of the hyperparameter value. Defaults to 1, which corresponds to a scalar hyperparameter. n_elements > 1 corresponds to a hyperparameter which is vector-valued, such as, e.g., anisotropic length-scales. fixed : bool, default=None Whether the value of this hyperparameter is fixed, i.e., cannot be changed during hyperparameter tuning. If None is passed, the "fixed" is derived based on the given bounds. Examples -------- >>> from sklearn.gaussian_process.kernels import ConstantKernel >>> from sklearn.datasets import make_friedman2 >>> from sklearn.gaussian_process import GaussianProcessRegressor >>> from sklearn.gaussian_process.kernels import Hyperparameter >>> X, y = make_friedman2(n_samples=50, noise=0, random_state=0) >>> kernel = ConstantKernel(constant_value=1.0, ... constant_value_bounds=(0.0, 10.0)) We can access each hyperparameter: >>> for hyperparameter in kernel.hyperparameters: ... print(hyperparameter) Hyperparameter(name='constant_value', value_type='numeric', bounds=array([[ 0., 10.]]), n_elements=1, fixed=False) >>> params = kernel.get_params() >>> for key in sorted(params): print(f"{key} : {params[key]}") constant_value : 1.0 constant_value_bounds : (0.0, 10.0) cpt|tr|dk7rttj|}|dkDrZ|jddk(rtj ||d}n0|jd|k7rt d|||jdfz|t|txr|dk(}t|!||||||S)Nfixedrrz@Bounds on %s should have either 1 or %d dimensions. Given are %d) isinstancestrr atleast_2drrepeatrsuper__new__)clsname value_typebounds n_elementsr# __class__s rr)zHyperparameter.__new__zs&#&&G*;]]6*FA~<<?a'YYvz1=F\\!_ 2$6V\\!_=> =vs+A'0AEwsD*fj%PPrc2|j|jk(xr}|j|jk(xrbtj|j|jk(xr4|j |j k(xr|j |j k(SN)r+r,rallr-r.r#)selfothers r__eq__zHyperparameter.__eq__s| II # *5#3#33 *t{{ell23 *5#3#33 * ekk)  r)rN)__name__ __module__ __qualname____doc__ __slots__r)r5 __classcell__r/s@rr r 4s 5~IQ& rr )r+r,r-r.r#ceZdZdZddZdZdZedZedZ edZ e jdZ ed Z d Z d Zd Zd ZdZdZdZeddZedZedZedZdZy)KernelaBase class for all kernels. .. versionadded:: 0.18 Examples -------- >>> from sklearn.gaussian_process.kernels import Kernel, RBF >>> import numpy as np >>> class CustomKernel(Kernel): ... def __init__(self, length_scale=1.0): ... self.length_scale = length_scale ... def __call__(self, X, Y=None): ... if Y is None: ... Y = X ... return np.inner(X, X if Y is None else Y) ** 2 ... def diag(self, X): ... return np.ones(X.shape[0]) ... def is_stationary(self): ... return True >>> kernel = CustomKernel(length_scale=2.0) >>> X = np.array([[1, 2], [3, 4]]) >>> print(kernel(X)) [[ 25 121] [121 625]] c&t}|j}t|jd|j}t |}gg}}|j j D]z}|j|jk7r*|jdk7r|j|j|j|jk(s`|j|j|t|dk7rtd|d|D]} t|| || <|S)XGet parameters of this kernel. Parameters ---------- deep : bool, default=True If True, will return the parameters for this estimator and contained subobjects that are estimators. Returns ------- params : dict Parameter names mapped to their values. deprecated_originalr3rzmscikit-learn kernels should always specify their parameters in the signature of their __init__ (no varargs). z doesn't follow this convention.)dictr/getattr__init__r parametersvalueskind VAR_KEYWORDr+appendVAR_POSITIONALlen RuntimeError) r3deepparamsr*init init_signargsvarargs parameterargs r get_paramszKernel.get_paramssnns||%:CLLIdO Bg"--446 /I~~!6!669>>V;S INN+~~!9!99y~~.  / w<1 :=?   -C!$,F3K - rc z|s|S|jd}|jD]\}}|jdd}t|dkDr5|\}}||vrt d|d|d||}|j di||i[||vr&t d|d|j jdt||||S) a:Set the parameters of this kernel. The method works on simple kernels as well as on nested kernels. The latter have parameters of the form ``__`` so that it's possible to update each component of a nested object. Returns ------- self T)rM__rzInvalid parameter z for kernel zK. Check the list of available parameters with `kernel.get_params().keys()`.r!) rUitemssplitrKr set_paramsr/r6setattr) r3rN valid_paramskeyvaluerYr+sub_name sub_objects rrZzKernel.set_paramssKD1  ,,. *JCIIdA&E5zA~!&h|+$AEdL *$/ % %%:5(9:l*$ 7 79 c5)- *. rc*t|}||_|S)zReturns a clone of self with given hyperparameters theta. Parameters ---------- theta : ndarray of shape (n_dims,) The hyperparameters )r theta)r3rbcloneds rclone_with_thetazKernel.clone_with_thetast  rc4|jjdS)z>Returns the number of non-fixed hyperparameters of the kernel.r)rbrr3s rn_dimsz Kernel.n_dimsszz""rcvt|Dcgc]}|jdr t||!}}|Scc}w)z4Returns a list of all hyperparameter specifications.hyperparameter_)dir startswithrC)r3attrrs rhyperparameterszKernel.hyperparameterssE D  01 D$     s$6c4g}|j}|jD]-}|jr|j||j/t |dkDr(t jt j|St jgS)Returns the (flattened, log-transformed) non-fixed hyperparameters. Note that theta are typically the log-transformed values of the kernel's hyperparameters as this representation of the search space is more amenable for hyperparameter search, as hyperparameters like length-scales naturally live on a log-scale. Returns ------- theta : ndarray of shape (n_dims,) The non-fixed, log-transformed hyperparameters of the kernel r) rUrnr#rIr+rKrloghstackarray)r3rbrNhyperparameters rrbz Kernel.thetas}""22 :N!'' VN$7$789 : u:>66"))E*+ +88B< rc|j}d}|jD]}|jr|jdkDrBt j ||||jz||j <||jz }at j ||||j <|dz }|t|k7rtd|t|fz|jdi|y)Sets the (flattened, log-transformed) non-fixed hyperparameters. Parameters ---------- theta : ndarray of shape (n_dims,) The non-fixed, log-transformed hyperparameters of the kernel rrzGtheta has not the correct number of entries. Should be %d; given are %dNr!) rUrnr#r.rexpr+rKrrZ)r3rbrNirts rrbz Kernel.theta5s" "22 N##((1,.0ff!a.";";;</~**+^....0ffU1X.>~**+Q  E ?.12CJ@  !&!rc|jDcgc]}|js |j}}t|dkDr(t j t j |St jgScc}wReturns the log-transformed bounds on the theta. Returns ------- bounds : ndarray of shape (n_dims, 2) The log-transformed bounds on the kernel's hyperparameters theta r)rnr#r-rKrrqvstackrs)r3rtr-s rr-z Kernel.boundsTsn#'"6"6 !''  ! !  v;?66"))F+, ,88B<  sA;cdt|tst|t|St||Sr1r$r>SumConstantKernelr3bs r__add__zKernel.__add__gs*!V$t^A./ /4|rcdt|tstt||St||Sr1r~rs r__radd__zKernel.__radd__ls*!V$~a($/ /1d|rcdt|tst|t|St||Sr1r$r>Productrrs r__mul__zKernel.__mul__qs+!V$4!23 3tQrcdt|tstt||St||Sr1rrs r__rmul__zKernel.__rmul__vs+!V$>!,d3 3q$rct||Sr1)Exponentiationrs r__pow__zKernel.__pow__{sdA&&rcjt|t|k7ry|j}|j}tt|j t|j zD]<}t j |j|d|j|dk7s<yy)NFT)typerUsetlistkeysranyget)r3rparams_aparams_br]s rr5z Kernel.__eq__~s :a ??$<<>tHMMO,tHMMO/DDE Cvvhll3-c41HHI rc dj|jjdjt dj|j S)Nz{0}({1}), {0:.3g})formatr/r6joinmaprbrfs r__repr__zKernel.__repr__s>  NN # #TYYs93C3CTZZ/P%Q  rNcy)zEvaluate the kernel.Nr!)r3rY eval_gradients r__call__zKernel.__call__rcy)aReturns the diagonal of the kernel k(X, X). The result of this method is identical to np.diag(self(X)); however, it can be evaluated more efficiently since only the diagonal is evaluated. Parameters ---------- X : array-like of shape (n_samples,) Left argument of the returned kernel k(X, Y) Returns ------- K_diag : ndarray of shape (n_samples_X,) Diagonal of kernel k(X, X) Nr!r3rs rdiagz Kernel.diagrrcy))Returns whether the kernel is stationary.Nr!rfs r is_stationaryzKernel.is_stationaryrrcy)zReturns whether the kernel is defined on fixed-length feature vectors or generic objects. Defaults to True for backward compatibility.Tr!rfs rrequires_vector_inputzKernel.requires_vector_inputs rc &tj|jtj|jj }d}|j D]}|jrt|jD]}||dfr?tjd|d|jd|j|ddtnE||dfr>tjd|d|jd|j|ddt|dz }y ) z?Called after fitting to warn if bounds may have been too tight.rz&The optimal value found for dimension z of parameter z' is close to the specified lower bound zE. Decreasing the bound and calling fit again may find a better value.rz' is close to the specified upper bound zE. Increasing the bound and calling fit again may find a better value.N)riscloser-r&rbTrnr#ranger.warningswarnr+r)r3 list_closeidxhypdims r_check_bounds_paramszKernel._check_bounds_paramssZZ R]]4::-F-H-HI '' CyyS^^, c1f%MM ,/#**S/!:L N + Q'MM ,/#**S/!:L N +q+  rTNF)r6r7r8r9rUrZrdpropertyrgrnrbsetterr-rrrrrr5rrrrrrrr!rrr>r>s4&P&P ##  . \\""<  $    ' ##  $88 rr>) metaclassceZdZdZdZy)NormalizedKernelMixinzSMixin for kernels which are normalized: k(X, X)=1. .. versionadded:: 0.18 cFtj|jdS)Returns the diagonal of the kernel k(X, X). The result of this method is identical to np.diag(self(X)); however, it can be evaluated more efficiently since only the diagonal is evaluated. Parameters ---------- X : ndarray of shape (n_samples_X, n_features) Left argument of the returned kernel k(X, Y) Returns ------- K_diag : ndarray of shape (n_samples_X,) Diagonal of kernel k(X, X) r)ronesrrs rrzNormalizedKernelMixin.diags"wwqwwqz""rN)r6r7r8r9rr!rrrrs  #rrceZdZdZdZy)StationaryKernelMixinzYMixin for kernels which are stationary: k(X, Y)= f(X-Y). .. versionadded:: 0.18 cy)rTr!rfs rrz#StationaryKernelMixin.is_stationarysrN)r6r7r8r9rr!rrrrs  rrc eZdZdZedZy)GenericKernelMixinzMixin for kernels which operate on generic objects such as variable- length sequences, trees, and graphs. .. versionadded:: 0.22 cy)z>Whether the kernel works only on fixed-length feature vectors.Fr!rfs rrz(GenericKernelMixin.requires_vector_inputsrN)r6r7r8r9rrr!rrrrs rrceZdZdZdZd dZedZejdZedZ ddZ d Z d Z ed Z d Zy)CompoundKernelaKernel which is composed of a set of other kernels. .. versionadded:: 0.18 Parameters ---------- kernels : list of Kernels The other kernels Examples -------- >>> from sklearn.gaussian_process.kernels import WhiteKernel >>> from sklearn.gaussian_process.kernels import RBF >>> from sklearn.gaussian_process.kernels import CompoundKernel >>> kernel = CompoundKernel( ... [WhiteKernel(noise_level=3.0), RBF(length_scale=2.0)]) >>> print(kernel.bounds) [[-11.51292546 11.51292546] [-11.51292546 11.51292546]] >>> print(kernel.n_dims) 2 >>> print(kernel.theta) [1.09861229 0.69314718] c||_yr1kernels)r3rs rrDzCompoundKernel.__init__s  rc.t|jS)r@r)rBr)r3rMs rrUzCompoundKernel.get_paramssDLL))rcztj|jDcgc]}|jc}Scc}wrp)rrrrrbr3kernels rrbzCompoundKernel.theta-s)yyT\\B6&,,BCCB8c|jj}t|jD]\}}|||z|dz|z|_y)zSets the (flattened, log-transformed) non-fixed hyperparameters. Parameters ---------- theta : array of shape (n_dims,) The non-fixed, log-transformed hyperparameters of the kernel rN)k1rg enumeraterrb)r3rbk_dimsrxrs rrbzCompoundKernel.theta=sJ"4<<0 @IAv Vq1u.>?FL @rcztj|jDcgc]}|jc}Scc}w)zReturns the log-transformed bounds on the theta. Returns ------- bounds : array of shape (n_dims, 2) The log-transformed bounds on the kernel's hyperparameters theta )rr|rr-rs rr-zCompoundKernel.boundsJs)yydllCF&--CDDCrNc ~|rg}g}|jD]D}||||\}}|j||j|dtjfFtj|tj |dfStj|jDcgc] }||||c}Scc}w)aReturn the kernel k(X, Y) and optionally its gradient. Note that this compound kernel returns the results of all simple kernel stacked along an additional axis. Parameters ---------- X : array-like of shape (n_samples_X, n_features) or list of object, default=None Left argument of the returned kernel k(X, Y) Y : array-like of shape (n_samples_X, n_features) or list of object, default=None Right argument of the returned kernel k(X, Y). If None, k(X, X) is evaluated instead. eval_gradient : bool, default=False Determines whether the gradient with respect to the log of the kernel hyperparameter is computed. Returns ------- K : ndarray of shape (n_samples_X, n_samples_Y, n_kernels) Kernel k(X, Y) K_gradient : ndarray of shape (n_samples_X, n_samples_X, n_dims, n_kernels), optional The gradient of the kernel k(X, X) with respect to the log of the hyperparameter of the kernel. Only returned when `eval_gradient` is True. .)rrIrnewaxisdstack concatenate) r3rrrKK_gradrK_single K_grad_singles rrzCompoundKernel.__call__Us@ AF,, >*0A}*E'-" mCO<= >99Q<!:: :99 UffQ=9UV VUs"B:cJt|t|k7s+t|jt|jk7rytjt t|jDcgc]!}|j||j|k(#c}Scc}wr)rrKrrr2r)r3rrxs rr5zCompoundKernel.__eq__sr :a C $5QYY$Gvv6;C z,KernelOperator.get_params..E36A:s+Ec30K|]\}}d|z|fyw)k2__Nr!rs rrz,KernelOperator.get_params..rr)rBrrrUrXupdater3rMrN deep_itemss rrUzKernelOperator.get_paramsszTWW- ++-335J MME*E E++-335J MME*E E rc |jjDcgc];}td|jz|j|j |j =}}|jjD]J}|jtd|jz|j|j |j L|Scc}w)%Returns a list of all hyperparameter.rr) rrnr r+r,r-r.rrI)r3rtrms rrnzKernelOperator.hyperparameterss#'''"9"9  ,,,))%%))    #gg55 N HH^000"--"))"--   ' sACc~tj|jj|jjSr)rrIrrbrrfs rrbzKernelOperator.thetas%yy 66rc|jj}|d||j_||d|j_yrvN)rrgrbr)r3rbk1_dimss rrbzKernelOperator.thetas3''..hw gh rcd|jjjdk(r|jjS|jjjdk(r|jjSt j |jj|jjfSrz)rr-sizerrr|rfs rr-zKernelOperator.boundssp 77>>  ! #77>> ! 77>>  ! #77>> !yy$''..$''..9::rct|t|k7ry|j|jk(xr|j|jk(xs4|j|jk(xr|j|jk(Sr)rrrrs rr5zKernelOperator.__eq__ s_ :a 1443DGGqttO GGqttO /144 rcn|jjxr|jjSr)rrrrfs rrzKernelOperator.is_stationarys'ww$$&B477+@+@+BBrc^|jjxs|jjSr)rrrrfs rrz$KernelOperator.requires_vector_inputs#ww,,M0M0MMrNr)r6r7r8r9rDrUrrnrbrr-r5rrr!rrrrs .. 7 7 \\ ( ( ; ; CNNrrc$eZdZdZddZdZdZy)ra+The `Sum` kernel takes two kernels :math:`k_1` and :math:`k_2` and combines them via .. math:: k_{sum}(X, Y) = k_1(X, Y) + k_2(X, Y) Note that the `__add__` magic method is overridden, so `Sum(RBF(), RBF())` is equivalent to using the + operator with `RBF() + RBF()`. Read more in the :ref:`User Guide `. .. versionadded:: 0.18 Parameters ---------- k1 : Kernel The first base-kernel of the sum-kernel k2 : Kernel The second base-kernel of the sum-kernel Examples -------- >>> from sklearn.datasets import make_friedman2 >>> from sklearn.gaussian_process import GaussianProcessRegressor >>> from sklearn.gaussian_process.kernels import RBF, Sum, ConstantKernel >>> X, y = make_friedman2(n_samples=500, noise=0, random_state=0) >>> kernel = Sum(ConstantKernel(2), RBF()) >>> gpr = GaussianProcessRegressor(kernel=kernel, ... random_state=0).fit(X, y) >>> gpr.score(X, y) 1.0 >>> kernel 1.41**2 + RBF(length_scale=1) Nc|rJ|j||d\}}|j||d\}}||ztj||ffS|j|||j||zS)aReturn the kernel k(X, Y) and optionally its gradient. Parameters ---------- X : array-like of shape (n_samples_X, n_features) or list of object Left argument of the returned kernel k(X, Y) Y : array-like of shape (n_samples_X, n_features) or list of object, default=None Right argument of the returned kernel k(X, Y). If None, k(X, X) is evaluated instead. eval_gradient : bool, default=False Determines whether the gradient with respect to the log of the kernel hyperparameter is computed. Returns ------- K : ndarray of shape (n_samples_X, n_samples_Y) Kernel k(X, Y) K_gradient : ndarray of shape (n_samples_X, n_samples_X, n_dims), optional The gradient of the kernel k(X, X) with respect to the log of the hyperparameter of the kernel. Only returned when `eval_gradient` is True. Tr)rrrrr3rrrK1 K1_gradientK2 K2_gradients rrz Sum.__call__Ass8 "gga$g?OB "gga$g?OB 7BII{K&@AA A771a=4771a=0 0rcp|jj||jj|zS)aReturns the diagonal of the kernel k(X, X). The result of this method is identical to `np.diag(self(X))`; however, it can be evaluated more efficiently since only the diagonal is evaluated. Parameters ---------- X : array-like of shape (n_samples_X, n_features) or list of object Argument to the kernel. Returns ------- K_diag : ndarray of shape (n_samples_X,) Diagonal of kernel k(X, X) rrrrs rrzSum.diagd'"ww||Aa00rcNdj|j|jS)Nz {0} + {1}rrrrfs rrz Sum.__repr__w!!$''47733rrr6r7r8r9rrrr!rrrrs$L!1F1&4rrc$eZdZdZddZdZdZy)raYThe `Product` kernel takes two kernels :math:`k_1` and :math:`k_2` and combines them via .. math:: k_{prod}(X, Y) = k_1(X, Y) * k_2(X, Y) Note that the `__mul__` magic method is overridden, so `Product(RBF(), RBF())` is equivalent to using the * operator with `RBF() * RBF()`. Read more in the :ref:`User Guide `. .. versionadded:: 0.18 Parameters ---------- k1 : Kernel The first base-kernel of the product-kernel k2 : Kernel The second base-kernel of the product-kernel Examples -------- >>> from sklearn.datasets import make_friedman2 >>> from sklearn.gaussian_process import GaussianProcessRegressor >>> from sklearn.gaussian_process.kernels import (RBF, Product, ... ConstantKernel) >>> X, y = make_friedman2(n_samples=500, noise=0, random_state=0) >>> kernel = Product(ConstantKernel(2), RBF()) >>> gpr = GaussianProcessRegressor(kernel=kernel, ... random_state=0).fit(X, y) >>> gpr.score(X, y) 1.0 >>> kernel 1.41**2 * RBF(length_scale=1) Nc P|r|j||d\}}|j||d\}}||ztj||ddddtjfz||ddddtjfzffS|j|||j||zS)aReturn the kernel k(X, Y) and optionally its gradient. Parameters ---------- X : array-like of shape (n_samples_X, n_features) or list of object Left argument of the returned kernel k(X, Y) Y : array-like of shape (n_samples_Y, n_features) or list of object, default=None Right argument of the returned kernel k(X, Y). If None, k(X, X) is evaluated instead. eval_gradient : bool, default=False Determines whether the gradient with respect to the log of the kernel hyperparameter is computed. Returns ------- K : ndarray of shape (n_samples_X, n_samples_Y) Kernel k(X, Y) K_gradient : ndarray of shape (n_samples_X, n_samples_X, n_dims), optional The gradient of the kernel k(X, X) with respect to the log of the hyperparameter of the kernel. Only returned when `eval_gradient` is True. TrN)rrrrrrs rrzProduct.__call__s8 "gga$g?OB "gga$g?OB 7BIIr!Q "233[2aBJJFVCW5WX 771a=4771a=0 0rcp|jj||jj|zSReturns the diagonal of the kernel k(X, X). The result of this method is identical to np.diag(self(X)); however, it can be evaluated more efficiently since only the diagonal is evaluated. Parameters ---------- X : array-like of shape (n_samples_X, n_features) or list of object Argument to the kernel. Returns ------- K_diag : ndarray of shape (n_samples_X,) Diagonal of kernel k(X, X) rrs rrz Product.diagrrcNdj|j|jS)Nz {0} * {1}rrfs rrzProduct.__repr__rrrrr!rrrr{s%N#1J1&4rrceZdZdZdZddZedZedZejdZedZ dZ dd Z d Z d Zd ZedZy )ra~The Exponentiation kernel takes one base kernel and a scalar parameter :math:`p` and combines them via .. math:: k_{exp}(X, Y) = k(X, Y) ^p Note that the `__pow__` magic method is overridden, so `Exponentiation(RBF(), 2)` is equivalent to using the ** operator with `RBF() ** 2`. Read more in the :ref:`User Guide `. .. versionadded:: 0.18 Parameters ---------- kernel : Kernel The base kernel exponent : float The exponent for the base kernel Examples -------- >>> from sklearn.datasets import make_friedman2 >>> from sklearn.gaussian_process import GaussianProcessRegressor >>> from sklearn.gaussian_process.kernels import (RationalQuadratic, ... Exponentiation) >>> X, y = make_friedman2(n_samples=500, noise=0, random_state=0) >>> kernel = Exponentiation(RationalQuadratic(), exponent=2) >>> gpr = GaussianProcessRegressor(kernel=kernel, alpha=5, ... random_state=0).fit(X, y) >>> gpr.score(X, y) 0.419 >>> gpr.predict(X[:1,:], return_std=True) (array([635.5]), array([0.559])) c ||_||_yr1rexponent)r3rrs rrDzExponentiation.__init__s   rct|j|j}|r@|jjj }|j d|D|S)r@rc30K|]\}}d|z|fyw)kernel__Nr!rs rrz,Exponentiation.get_params..sIFAs:>3/Ir)rBrrrUrXrrs rrUzExponentiation.get_params sMT[[4==A //1779J MMIjI I rc g}|jjD]J}|jtd|jz|j |j |jL|S)rr )rrnrIr r+r,r-r.)r3rmrts rrnzExponentiation.hyperparameters sf "kk99 N HH!4!44"--"))"--   rc.|jjSrrrbrfs rrbzExponentiation.theta/s{{   rc&||j_yrr#)r3rbs rrbzExponentiation.theta?s" rc.|jjS)r{)rr-rfs rr-zExponentiation.boundsJs{{!!!rct|t|k7ry|j|jk(xr|j|jk(Sr)rrrrs rr5zExponentiation.__eq__Us: :a {{ahh&F4==AJJ+FFrNc|rb|j||d\}}||j|ddddtjf|jdz zzz}||jz|fS|j||d}||jzS)aReturn the kernel k(X, Y) and optionally its gradient. Parameters ---------- X : array-like of shape (n_samples_X, n_features) or list of object Left argument of the returned kernel k(X, Y) Y : array-like of shape (n_samples_Y, n_features) or list of object, default=None Right argument of the returned kernel k(X, Y). If None, k(X, X) is evaluated instead. eval_gradient : bool, default=False Determines whether the gradient with respect to the log of the kernel hyperparameter is computed. Returns ------- K : ndarray of shape (n_samples_X, n_samples_Y) Kernel k(X, Y) K_gradient : ndarray of shape (n_samples_X, n_samples_X, n_dims), optional The gradient of the kernel k(X, X) with respect to the log of the hyperparameter of the kernel. Only returned when `eval_gradient` is True. TrNrF)rrrrr3rrrr K_gradients rrzExponentiation.__call__Zs8  KK1DKAMAz $--!Aq"**,<*=$--RSBS*TT TJdmm#Z/ / Aq 6Admm# #rcR|jj||jzSr)rrrrs rrzExponentiation.diag~s""{{"dmm33rcNdj|j|jS)Nz {0} ** {1})rrrrfs rrzExponentiation.__repr__s""4;; >>rc6|jjSr)rrrfs rrzExponentiation.is_stationarys{{((**rc.|jjSr)rrrfs rrz$Exponentiation.requires_vector_inputs{{000rrr)r6r7r8r9rDrUrrnrbrr-r5rrrrrr!rrrrs&P!(   ! ! \\""""G "$H4&?+11rrc<eZdZdZddZedZd dZdZdZ y) raConstant kernel. Can be used as part of a product-kernel where it scales the magnitude of the other factor (kernel) or as part of a sum-kernel, where it modifies the mean of the Gaussian process. .. math:: k(x_1, x_2) = constant\_value \;\forall\; x_1, x_2 Adding a constant kernel is equivalent to adding a constant:: kernel = RBF() + ConstantKernel(constant_value=2) is the same as:: kernel = RBF() + 2 Read more in the :ref:`User Guide `. .. versionadded:: 0.18 Parameters ---------- constant_value : float, default=1.0 The constant value which defines the covariance: k(x_1, x_2) = constant_value constant_value_bounds : pair of floats >= 0 or "fixed", default=(1e-5, 1e5) The lower and upper bound on `constant_value`. If set to "fixed", `constant_value` cannot be changed during hyperparameter tuning. Examples -------- >>> from sklearn.datasets import make_friedman2 >>> from sklearn.gaussian_process import GaussianProcessRegressor >>> from sklearn.gaussian_process.kernels import RBF, ConstantKernel >>> X, y = make_friedman2(n_samples=500, noise=0, random_state=0) >>> kernel = RBF() + ConstantKernel(constant_value=2) >>> gpr = GaussianProcessRegressor(kernel=kernel, alpha=5, ... random_state=0).fit(X, y) >>> gpr.score(X, y) 0.3696 >>> gpr.predict(X[:1,:], return_std=True) (array([606.1]), array([0.248])) c ||_||_yr1)constant_valueconstant_value_bounds)r3r0r1s rrDzConstantKernel.__init__s,%:"rc0tdd|jS)Nr0numeric)r r1rfs rhyperparameter_constant_valuez,ConstantKernel.hyperparameter_constant_values. 4;U;UVVrNc,||}n |r tdtjt|t|f|jtj |jj }|r|jjs`|tjt|t|df|jtj |jj fS|tjt|t|dffS|S)aReturn the kernel k(X, Y) and optionally its gradient. Parameters ---------- X : array-like of shape (n_samples_X, n_features) or list of object Left argument of the returned kernel k(X, Y) Y : array-like of shape (n_samples_X, n_features) or list of object, default=None Right argument of the returned kernel k(X, Y). If None, k(X, X) is evaluated instead. eval_gradient : bool, default=False Determines whether the gradient with respect to the log of the kernel hyperparameter is computed. Only supported when Y is None. Returns ------- K : ndarray of shape (n_samples_X, n_samples_Y) Kernel k(X, Y) K_gradient : ndarray of shape (n_samples_X, n_samples_X, n_dims), optional The gradient of the kernel k(X, X) with respect to the log of the hyperparameter of the kernel. Only returned when eval_gradient is True. .Gradient can only be evaluated when Y is None.dtyperr) rrfullrr0rsr8r4r#emptyr3rrrrs rrzConstantKernel.__call__s: 9A MN N GG !_l1o .   ((4../55  55;;GG%a,q/1=++ hht':':;AA"((LO\!_a#HIIIHrctjt||jtj|jj Srr7)rr9rr0rsr8rs rrzConstantKernel.diag s="ww O   ((4../55  rc^djtj|jS)Nz {0:.3g}**2)rrsqrtr0rfs rrzConstantKernel.__repr__$s"""2774+>+>#?@@r?gh㈵>gj@r) r6r7r8r9rDrr4rrrr!rrrrs4.`;WW4l .Arrc<eZdZdZddZedZd dZdZdZ y) WhiteKernelaSWhite kernel. The main use-case of this kernel is as part of a sum-kernel where it explains the noise of the signal as independently and identically normally-distributed. The parameter noise_level equals the variance of this noise. .. math:: k(x_1, x_2) = noise\_level \text{ if } x_i == x_j \text{ else } 0 Read more in the :ref:`User Guide `. .. versionadded:: 0.18 Parameters ---------- noise_level : float, default=1.0 Parameter controlling the noise level (variance) noise_level_bounds : pair of floats >= 0 or "fixed", default=(1e-5, 1e5) The lower and upper bound on 'noise_level'. If set to "fixed", 'noise_level' cannot be changed during hyperparameter tuning. Examples -------- >>> from sklearn.datasets import make_friedman2 >>> from sklearn.gaussian_process import GaussianProcessRegressor >>> from sklearn.gaussian_process.kernels import DotProduct, WhiteKernel >>> X, y = make_friedman2(n_samples=500, noise=0, random_state=0) >>> kernel = DotProduct() + WhiteKernel(noise_level=0.5) >>> gpr = GaussianProcessRegressor(kernel=kernel, ... random_state=0).fit(X, y) >>> gpr.score(X, y) 0.3680 >>> gpr.predict(X[:2,:], return_std=True) (array([653.0, 592.1 ]), array([316.6, 316.6])) c ||_||_yr1) noise_levelnoise_level_bounds)r3rFrGs rrDzWhiteKernel.__init__Qs&"4rc0tdd|jS)NrFr3)r rGrfs rhyperparameter_noise_levelz&WhiteKernel.hyperparameter_noise_levelUmY8O8OPPrNc| |r td||jtjt |z}|r|j j sE||jtjt |ddddtjfzfS|tjt |t |dffS|Stjt |t |fS)aReturn the kernel k(X, Y) and optionally its gradient. Parameters ---------- X : array-like of shape (n_samples_X, n_features) or list of object Left argument of the returned kernel k(X, Y) Y : array-like of shape (n_samples_X, n_features) or list of object, default=None Right argument of the returned kernel k(X, Y). If None, k(X, X) is evaluated instead. eval_gradient : bool, default=False Determines whether the gradient with respect to the log of the kernel hyperparameter is computed. Only supported when Y is None. Returns ------- K : ndarray of shape (n_samples_X, n_samples_Y) Kernel k(X, Y) K_gradient : ndarray of shape (n_samples_X, n_samples_X, n_dims), optional The gradient of the kernel k(X, X) with respect to the log of the hyperparameter of the kernel. Only returned when eval_gradient is True. Nr6r) rrFreyerrIr#rr:zerosr;s rrzWhiteKernel.__call__Ys: =]MN N 9  266,q/#::A66<<((266,q/+B1aCS+TT bhh Qa!'LMMM88\!_l1o>? ?rctjt||jtj|jj Sr=)rr9rrFrsr8rs rrzWhiteKernel.diags;"ww OT--RXXd>N>N5O5U5U  rcbdj|jj|jS)Nz{0}(noise_level={1:.3g}))rr/r6rFrfs rrzWhiteKernel.__repr__s*)00 NN # #T%5%5  rr@r) r6r7r8r9rDrrIrrrr!rrrDrD(s4&P5QQ-@^ * rrDcFeZdZdZddZedZedZd dZdZ y) RBFa Radial basis function kernel (aka squared-exponential kernel). The RBF kernel is a stationary kernel. It is also known as the "squared exponential" kernel. It is parameterized by a length scale parameter :math:`l>0`, which can either be a scalar (isotropic variant of the kernel) or a vector with the same number of dimensions as the inputs X (anisotropic variant of the kernel). The kernel is given by: .. math:: k(x_i, x_j) = \exp\left(- \frac{d(x_i, x_j)^2}{2l^2} \right) where :math:`l` is the length scale of the kernel and :math:`d(\cdot,\cdot)` is the Euclidean distance. For advice on how to set the length scale parameter, see e.g. [1]_. This kernel is infinitely differentiable, which implies that GPs with this kernel as covariance function have mean square derivatives of all orders, and are thus very smooth. See [2]_, Chapter 4, Section 4.2, for further details of the RBF kernel. Read more in the :ref:`User Guide `. .. versionadded:: 0.18 Parameters ---------- length_scale : float or ndarray of shape (n_features,), default=1.0 The length scale of the kernel. If a float, an isotropic kernel is used. If an array, an anisotropic kernel is used where each dimension of l defines the length-scale of the respective feature dimension. length_scale_bounds : pair of floats >= 0 or "fixed", default=(1e-5, 1e5) The lower and upper bound on 'length_scale'. If set to "fixed", 'length_scale' cannot be changed during hyperparameter tuning. References ---------- .. [1] `David Duvenaud (2014). "The Kernel Cookbook: Advice on Covariance functions". `_ .. [2] `Carl Edward Rasmussen, Christopher K. I. Williams (2006). "Gaussian Processes for Machine Learning". The MIT Press. `_ Examples -------- >>> from sklearn.datasets import load_iris >>> from sklearn.gaussian_process import GaussianProcessClassifier >>> from sklearn.gaussian_process.kernels import RBF >>> X, y = load_iris(return_X_y=True) >>> kernel = 1.0 * RBF(1.0) >>> gpc = GaussianProcessClassifier(kernel=kernel, ... random_state=0).fit(X, y) >>> gpc.score(X, y) 0.9866 >>> gpc.predict_proba(X[:2,:]) array([[0.8354, 0.03228, 0.1322], [0.7906, 0.0652, 0.1441]]) c ||_||_yr1)rlength_scale_bounds)r3rrSs rrDz RBF.__init__s(#6 rcttj|jxrt|jdkDS)Nr)riterablerrKrfs r anisotropiczRBF.anisotropics,{{4,,-L#d6G6G2H12LLrc|jr+tdd|jt|jStdd|jSNrr3)rVr rSrKrrfs rhyperparameter_length_scalezRBF.hyperparameter_length_scalesL   !((D%%&   ni9Q9QRRrNc\tj|}t||j}|Jt ||z d}tj d|z}t |}tj|dn9|r tdt||z ||z d}tj d|z}|r|jjr4|tj|jd|jddffS|jr|jddk(r*|t |zddddtjf}||fS|jrZ|ddtjddf|tjddddfz dz|dzz }||d tjfz}||fSy|S) Return the kernel k(X, Y) and optionally its gradient. Parameters ---------- X : ndarray of shape (n_samples_X, n_features) Left argument of the returned kernel k(X, Y) Y : ndarray of shape (n_samples_Y, n_features), default=None Right argument of the returned kernel k(X, Y). If None, k(X, X) if evaluated instead. eval_gradient : bool, default=False Determines whether the gradient with respect to the log of the kernel hyperparameter is computed. Only supported when Y is None. Returns ------- K : ndarray of shape (n_samples_X, n_samples_Y) Kernel k(X, Y) K_gradient : ndarray of shape (n_samples_X, n_samples_X, n_dims), optional The gradient of the kernel k(X, X) with respect to the log of the hyperparameter of the kernel. Only returned when `eval_gradient` is True. N sqeuclideanmetricgrr6rr .)rr&rrrrwr fill_diagonalrrrYr#r:rrVr)r3rrrrdistsrr)s rrz RBF.__call__s8 MM! *1d.?.?@ 9!l*=AEte|$A1 A   Q " !QRR!l*A ,<]SEte|$A //55"((AGGAJ A#>???%%););A)>!)C*U"33Q2::5EF *}$!!2::q 01Abjj!Q6F4GGAM !O aRZZ00 *}$ "Hrc L|jrSdj|jjdj t dj|j Sdj|jjtj|j dS)Nz{0}(length_scale=[{1}])rrz{0}(length_scale={1:.3g})r) rVrr/r6rrrrravelrfs rrz RBF.__repr__0s   ,33'' #i..0A0ABC  /55''$2C2C)DQ)G rr@r) r6r7r8r9rDrrVrYrrr!rrrQrQsD<|7MMSS9v rrQc2eZdZdZdfd ZddZdZxZS)Maternu Matern kernel. The class of Matern kernels is a generalization of the :class:`RBF`. It has an additional parameter :math:`\nu` which controls the smoothness of the resulting function. The smaller :math:`\nu`, the less smooth the approximated function is. As :math:`\nu\rightarrow\infty`, the kernel becomes equivalent to the :class:`RBF` kernel. When :math:`\nu = 1/2`, the Matérn kernel becomes identical to the absolute exponential kernel. Important intermediate values are :math:`\nu=1.5` (once differentiable functions) and :math:`\nu=2.5` (twice differentiable functions). The kernel is given by: .. math:: k(x_i, x_j) = \frac{1}{\Gamma(\nu)2^{\nu-1}}\Bigg( \frac{\sqrt{2\nu}}{l} d(x_i , x_j ) \Bigg)^\nu K_\nu\Bigg( \frac{\sqrt{2\nu}}{l} d(x_i , x_j )\Bigg) where :math:`d(\cdot,\cdot)` is the Euclidean distance, :math:`K_{\nu}(\cdot)` is a modified Bessel function and :math:`\Gamma(\cdot)` is the gamma function. See [1]_, Chapter 4, Section 4.2, for details regarding the different variants of the Matern kernel. Read more in the :ref:`User Guide `. .. versionadded:: 0.18 Parameters ---------- length_scale : float or ndarray of shape (n_features,), default=1.0 The length scale of the kernel. If a float, an isotropic kernel is used. If an array, an anisotropic kernel is used where each dimension of l defines the length-scale of the respective feature dimension. length_scale_bounds : pair of floats >= 0 or "fixed", default=(1e-5, 1e5) The lower and upper bound on 'length_scale'. If set to "fixed", 'length_scale' cannot be changed during hyperparameter tuning. nu : float, default=1.5 The parameter nu controlling the smoothness of the learned function. The smaller nu, the less smooth the approximated function is. For nu=inf, the kernel becomes equivalent to the RBF kernel and for nu=0.5 to the absolute exponential kernel. Important intermediate values are nu=1.5 (once differentiable functions) and nu=2.5 (twice differentiable functions). Note that values of nu not in [0.5, 1.5, 2.5, inf] incur a considerably higher computational cost (appr. 10 times higher) since they require to evaluate the modified Bessel function. Furthermore, in contrast to l, nu is kept fixed to its initial value and not optimized. References ---------- .. [1] `Carl Edward Rasmussen, Christopher K. I. Williams (2006). "Gaussian Processes for Machine Learning". The MIT Press. `_ Examples -------- >>> from sklearn.datasets import load_iris >>> from sklearn.gaussian_process import GaussianProcessClassifier >>> from sklearn.gaussian_process.kernels import Matern >>> X, y = load_iris(return_X_y=True) >>> kernel = 1.0 * Matern(length_scale=1.0, nu=1.5) >>> gpc = GaussianProcessClassifier(kernel=kernel, ... random_state=0).fit(X, y) >>> gpc.score(X, y) 0.9866 >>> gpc.predict_proba(X[:2,:]) array([[0.8513, 0.0368, 0.1117], [0.8086, 0.0693, 0.1220]]) c4t|||||_yr1)r(rDnu)r3rrSrfr/s rrDzMatern.__init__s ':;rc  tjtj}t |z d}n!|r t dt |z |z d}jdk(rtj| }njdk(r6|tjdz}d|ztj| z}nAjd k(r>|tjd z}d|z|d zd z ztj| z}njtjk(rtj|d z d z }n|}||dk(xxtjtjz cc<tjd jz|z}|jd djz zt!jz ||jzz}|t#j|z}!t%|}tj&|d|rj(j*r6tj,j.dj.ddf}||fSj0r?ddtj2ddftj2ddddfz d z|d zz } n&t%|d zddddtj2f} jdk(rtj| j5d ddddtj2f} tj6| } tj8| | | | dk7|dtj2f| z}n&jdk(rUd| ztjtjd| j5dz dtj2fz}n‰jd k(r]tjd | j5dzdtj2f}d| z|dzztj| z}nVjtjk(r| |dtj2fz}n fd} |t;j<| dfSj0s4||ddddfj5dddddtj2ffS||fS|S)r[N euclideanr]r6g??rrAg@r g@g@rr)axis)outwhere.g?c4j|Sr1)rd)rbrrr3s rfzMatern.__call__..fs740071==r绽|=)rr&rrrrrrfrwmathr?inffinforepsfillr r r r_rYr#r:rrVrsum zeros_likedivide_approx_fprimerb) r3rrrrr`rtmpr)D denominator divide_resultrqs ``` rrzMatern.__call__s8 MM! *1d.?.?@ 9!l*;?E !QRR!l*A ,<[QE 77c>vA WW^ ! $AqBFFA2J&A WW^ ! $Aq1a4#:%3A WW {S()AA a3hK288E?.. .K))AK(1,C FFA#-(E$''N: ; dgg A DGGS! !A 91 A   Q " //55XXqwwqz1771:q&AB *}$q"**a'(1RZZA-=+>>1D VWXuax(Arzz)9:ww#~ ggaeeem4Q2::5EF " a 0  %%*  sBJJ/-? CURVVRWWQr]-C,C%DS"**_%UU Cgga!%%)m,S"**_=&]cAg6E BFF"3 ?!33 >.Q>>>##*QT*..r21a3CDDD*}$Hrc x|jr^dj|jjdj t dj|j |jSdj|jjtj|j d|jS)Nz#{0}(length_scale=[{1}], nu={2:.3g})rrz%{0}(length_scale={1:.3g}, nu={2:.3g})r) rVrr/r6rrrrfrrbrfs rrzMatern.__repr__s   8??'' #i..0A0ABC  ;AA''$2C2C)DQ)G r)rArBrir)r6r7r8r9rDrrr;r<s@rrdrd<sM^eN rrdcNeZdZdZ ddZedZedZd dZdZ y) RationalQuadraticaRational Quadratic kernel. The RationalQuadratic kernel can be seen as a scale mixture (an infinite sum) of RBF kernels with different characteristic length scales. It is parameterized by a length scale parameter :math:`l>0` and a scale mixture parameter :math:`\alpha>0`. Only the isotropic variant where length_scale :math:`l` is a scalar is supported at the moment. The kernel is given by: .. math:: k(x_i, x_j) = \left( 1 + \frac{d(x_i, x_j)^2 }{ 2\alpha l^2}\right)^{-\alpha} where :math:`\alpha` is the scale mixture parameter, :math:`l` is the length scale of the kernel and :math:`d(\cdot,\cdot)` is the Euclidean distance. For advice on how to set the parameters, see e.g. [1]_. Read more in the :ref:`User Guide `. .. versionadded:: 0.18 Parameters ---------- length_scale : float > 0, default=1.0 The length scale of the kernel. alpha : float > 0, default=1.0 Scale mixture parameter length_scale_bounds : pair of floats >= 0 or "fixed", default=(1e-5, 1e5) The lower and upper bound on 'length_scale'. If set to "fixed", 'length_scale' cannot be changed during hyperparameter tuning. alpha_bounds : pair of floats >= 0 or "fixed", default=(1e-5, 1e5) The lower and upper bound on 'alpha'. If set to "fixed", 'alpha' cannot be changed during hyperparameter tuning. References ---------- .. [1] `David Duvenaud (2014). "The Kernel Cookbook: Advice on Covariance functions". `_ Examples -------- >>> from sklearn.datasets import load_iris >>> from sklearn.gaussian_process import GaussianProcessClassifier >>> from sklearn.gaussian_process.kernels import RationalQuadratic >>> X, y = load_iris(return_X_y=True) >>> kernel = RationalQuadratic(length_scale=1.0, alpha=1.5) >>> gpc = GaussianProcessClassifier(kernel=kernel, ... random_state=0).fit(X, y) >>> gpc.score(X, y) 0.9733 >>> gpc.predict_proba(X[:2,:]) array([[0.8881, 0.0566, 0.05518], [0.8678, 0.0707 , 0.0614]]) c<||_||_||_||_yr1)ralpharS alpha_bounds)r3rrrSrs rrDzRationalQuadratic.__init__Cs#) #6 (rc0tdd|jSrXr rSrfs rrYz-RationalQuadratic.hyperparameter_length_scaleOsni9Q9QRRrc0tdd|jS)Nrr3)r rrfs rhyperparameter_alphaz&RationalQuadratic.hyperparameter_alphaSgy$2C2CDDrNcvttj|jdkDr t dtj |}|dt t|d}|d|jz|jdzzz }d|z}||j z}tj|dnN|r tdt||d}d|d|jz|jdzzz z|j z}|r6|jjs3||z|jdzzz }|ddddtjf}n2tj|j d|j ddf}|j"jsZ||j tj$z|d|jdzz|zz zz} | ddddtjf} n2tj|j d|j ddf} |tj&| |ffS|S) aReturn the kernel k(X, Y) and optionally its gradient. Parameters ---------- X : ndarray of shape (n_samples_X, n_features) Left argument of the returned kernel k(X, Y) Y : ndarray of shape (n_samples_Y, n_features), default=None Right argument of the returned kernel k(X, Y). If None, k(X, X) if evaluated instead. eval_gradient : bool, default=False Determines whether the gradient with respect to the log of the kernel hyperparameter is computed. Only supported when Y is None. Returns ------- K : ndarray of shape (n_samples_X, n_samples_Y) Kernel k(X, Y) K_gradient : ndarray of shape (n_samples_X, n_samples_X, n_dims) The gradient of the kernel k(X, X) with respect to the log of the hyperparameter of the kernel. Only returned when eval_gradient is True. rzeRationalQuadratic kernel only supports isotropic version, please use a single scalar for length_scaleNr\r]r r6r)rKr atleast_1drAttributeErrorr&r rrr_rrrYr#rr:rrrqr) r3rrrr`r|baserlength_scale_gradientalpha_gradients rrzRationalQuadratic.__call__Ws6 r}}T../ 01 4 >  MM!  9uQ}=>E1tzz>D,=,=q,@@ACs7Dtzzk!A   Q " !QRR!Q}5EUa$**nt/@/@!/CCDD$**TA 3399(- T5F5F5ID5P(Q%(=aBJJ>N(O%(*!''!*aggaj!1L(M%,,22!"ZZK"&&,.q4#4#4a#77$>?@""01bjj0@!A!#1771:qwwqz1*E!Fbii1F GHH HHrcxdj|jj|j|jS)Nz({0}(alpha={1:.3g}, length_scale={2:.3g}))rr/r6rrrfs rrzRationalQuadratic.__repr__s09@@ NN # #TZZ1B1B  rrArArBrBr) r6r7r8r9rDrrYrrrr!rrrrsS<@' )SSEEAF rrcNeZdZdZ ddZedZedZd dZdZ y) ExpSineSquaredaExp-Sine-Squared kernel (aka periodic kernel). The ExpSineSquared kernel allows one to model functions which repeat themselves exactly. It is parameterized by a length scale parameter :math:`l>0` and a periodicity parameter :math:`p>0`. Only the isotropic variant where :math:`l` is a scalar is supported at the moment. The kernel is given by: .. math:: k(x_i, x_j) = \text{exp}\left(- \frac{ 2\sin^2(\pi d(x_i, x_j)/p) }{ l^ 2} \right) where :math:`l` is the length scale of the kernel, :math:`p` the periodicity of the kernel and :math:`d(\cdot,\cdot)` is the Euclidean distance. Read more in the :ref:`User Guide `. .. versionadded:: 0.18 Parameters ---------- length_scale : float > 0, default=1.0 The length scale of the kernel. periodicity : float > 0, default=1.0 The periodicity of the kernel. length_scale_bounds : pair of floats >= 0 or "fixed", default=(1e-5, 1e5) The lower and upper bound on 'length_scale'. If set to "fixed", 'length_scale' cannot be changed during hyperparameter tuning. periodicity_bounds : pair of floats >= 0 or "fixed", default=(1e-5, 1e5) The lower and upper bound on 'periodicity'. If set to "fixed", 'periodicity' cannot be changed during hyperparameter tuning. Examples -------- >>> from sklearn.datasets import make_friedman2 >>> from sklearn.gaussian_process import GaussianProcessRegressor >>> from sklearn.gaussian_process.kernels import ExpSineSquared >>> X, y = make_friedman2(n_samples=50, noise=0, random_state=0) >>> kernel = ExpSineSquared(length_scale=1, periodicity=1) >>> gpr = GaussianProcessRegressor(kernel=kernel, alpha=5, ... random_state=0).fit(X, y) >>> gpr.score(X, y) 0.0144 >>> gpr.predict(X[:2,:], return_std=True) (array([425.6, 457.5]), array([0.3894, 0.3467])) c<||_||_||_||_yr1)r periodicityrSperiodicity_bounds)r3rrrSrs rrDzExpSineSquared.__init__s$)&#6 "4rc0tdd|jS)zReturns the length scalerr3rrfs rrYz*ExpSineSquared.hyperparameter_length_scalesni9Q9QRRrc0tdd|jS)Nrr3)r rrfs rhyperparameter_periodicityz)ExpSineSquared.hyperparameter_periodicityrJrNcdtj|}|ttt|d}tj|z|j z }tj |}tjd||jz dzz}nt|r tdt||d}tjdtj tj|j z |z|jz dzz}|r-tj}|jjs6d|jdzz dzz|z} | ddddtjf} n2tj|j d|j d df} |j"js9d|z|jdzz |zz|z} | ddddtjf} n2tj|j d|j d df} |tj$| | ffS|S) r[Nrhr]r r6rr)rr&r rpirsinrwrrrcosrYr#rr:rrr) r3rrrr`rT sin_of_argr cos_of_argrperiodicity_gradients rrzExpSineSquared.__call__s8 MM!  9uQ{;ARARRWXXXA J3399()D,=,=q,@(@:q=(PST(T%(=aBJJ>N(O%(*!''!*aggaj!1L(M%2288Gd//22Z?*LqP%(`. .. versionadded:: 0.18 Parameters ---------- sigma_0 : float >= 0, default=1.0 Parameter controlling the inhomogenity of the kernel. If sigma_0=0, the kernel is homogeneous. sigma_0_bounds : pair of floats >= 0 or "fixed", default=(1e-5, 1e5) The lower and upper bound on 'sigma_0'. If set to "fixed", 'sigma_0' cannot be changed during hyperparameter tuning. References ---------- .. [1] `Carl Edward Rasmussen, Christopher K. I. Williams (2006). "Gaussian Processes for Machine Learning". The MIT Press. `_ Examples -------- >>> from sklearn.datasets import make_friedman2 >>> from sklearn.gaussian_process import GaussianProcessRegressor >>> from sklearn.gaussian_process.kernels import DotProduct, WhiteKernel >>> X, y = make_friedman2(n_samples=500, noise=0, random_state=0) >>> kernel = DotProduct() + WhiteKernel() >>> gpr = GaussianProcessRegressor(kernel=kernel, ... random_state=0).fit(X, y) >>> gpr.score(X, y) 0.3680 >>> gpr.predict(X[:2,:], return_std=True) (array([653.0, 592.1]), array([316.6, 316.6])) c ||_||_yr1)sigma_0sigma_0_bounds)r3rrs rrDzDotProduct.__init__js ,rc0tdd|jS)Nrr3)r rrfs rhyperparameter_sigma_0z!DotProduct.hyperparameter_sigma_0nsiD4G4GHHrNctj|}|'tj|||jdzz}n3|r t dtj|||jdzz}|r|j j sKtj|jd|jddf}d|jdzz|d<||fS|tj|jd|jddffS|S)Return the kernel k(X, Y) and optionally its gradient. Parameters ---------- X : ndarray of shape (n_samples_X, n_features) Left argument of the returned kernel k(X, Y) Y : ndarray of shape (n_samples_Y, n_features), default=None Right argument of the returned kernel k(X, Y). If None, k(X, X) if evaluated instead. eval_gradient : bool, default=False Determines whether the gradient with respect to the log of the kernel hyperparameter is computed. Only supported when Y is None. Returns ------- K : ndarray of shape (n_samples_X, n_samples_Y) Kernel k(X, Y) K_gradient : ndarray of shape (n_samples_X, n_samples_X, n_dims), optional The gradient of the kernel k(X, X) with respect to the log of the hyperparameter of the kernel. Only returned when `eval_gradient` is True. r r6rr).r) rr&innerrrrr#r:rr(s rrzDotProduct.__call__rs8 MM!  9Aq0A !QRRAq0A ..44XXqwwqz1771:q&AB %&q%8 6"*}$"((AGGAJ A#>???HrcPtjd|||jdzzS)aReturns the diagonal of the kernel k(X, X). The result of this method is identical to np.diag(self(X)); however, it can be evaluated more efficiently since only the diagonal is evaluated. Parameters ---------- X : ndarray of shape (n_samples_X, n_features) Left argument of the returned kernel k(X, Y). Returns ------- K_diag : ndarray of shape (n_samples_X,) Diagonal of kernel k(X, X). zij,ij->ir )reinsumrrs rrzDotProduct.diags$"yyQ*T\\1_<= 0 or "fixed", default=(1e-5, 1e5) The lower and upper bound on 'gamma'. If set to "fixed", 'gamma' cannot be changed during hyperparameter tuning. metric : {"linear", "additive_chi2", "chi2", "poly", "polynomial", "rbf", "laplacian", "sigmoid", "cosine"} or callable, default="linear" The metric to use when calculating kernel between instances in a feature array. If metric is a string, it must be one of the metrics in pairwise.PAIRWISE_KERNEL_FUNCTIONS. If metric is "precomputed", X is assumed to be a kernel matrix. Alternatively, if metric is a callable function, it is called on each pair of instances (rows) and the resulting value recorded. The callable should take two arrays from X as input and return a value indicating the distance between them. pairwise_kernels_kwargs : dict, default=None All entries of this dict (if any) are passed as keyword arguments to the pairwise kernel function. Examples -------- >>> from sklearn.datasets import load_iris >>> from sklearn.gaussian_process import GaussianProcessClassifier >>> from sklearn.gaussian_process.kernels import PairwiseKernel >>> X, y = load_iris(return_X_y=True) >>> kernel = PairwiseKernel(metric='rbf') >>> gpc = GaussianProcessClassifier(kernel=kernel, ... random_state=0).fit(X, y) >>> gpc.score(X, y) 0.9733 >>> gpc.predict_proba(X[:2,:]) array([[0.8880, 0.05663, 0.05532], [0.8676, 0.07073, 0.06165]]) Nc<||_||_||_||_yr1)r gamma_boundsr^pairwise_kernels_kwargs)r3r rr^rs rrDzPairwiseKernel.__init__ s" ( '>$rc0tdd|jS)Nr r3)r rrfs rhyperparameter_gammaz#PairwiseKernel.hyperparameter_gamma rrcjjitjtfjj dd}|rkj jr4|tjjdjddffSfd}|tj|dfS|S)rTr^r filter_paramsrc`tfjtj|ddS)NTr)rr^rrw)r rrrr3s rrqz"PairwiseKernel.__call__..f? s<+ ${{ ffUm&*  2 rrr) rrr&rr^r rr#r:rr{rb)r3rrrrrqrs``` @rrzPairwiseKernel.__call__ s8#'">">  ' ' /&( # MM!    ;;**  &   ((.."((AGGAJ A#>???.Q>>>HrcLtj|d|jS)rr)rapply_along_axisrbrs rrzPairwiseKernel.diagM s"$""4A.4466rc|jdvS)r)rbfr]rfs rrzPairwiseKernel.is_stationarya s{{g%%rcxdj|jj|j|jS)Nz{0}(gamma={1}, metric={2}))rr/r6r r^rfs rrzPairwiseKernel.__repr__e s.+22 NN # #TZZ  r)rArBlinearNr) r6r7r8r9rDrrrrrrr!rrrrsF6t  $ ?EE:x7(& rr)r!)/r9rsrabcrr collectionsrinspectrnumpyrscipy.spatial.distancerrr scipy.specialr r rr exceptionsrmetrics.pairwiserutils.validationrrr r>rrrrrrrrrrDrQrdrrrr{rr!rrrskX. '";;#+/+ ` Q` Ftwtn ##4  aHVaHHsNVsNl^4.^4Ba4na4H|1V|1~GA*,>GATx ');Vx vV !6VrESEPY -/DfY xN *,A6N bGTGTV ` V` r