KernelRidge#

classsklearn.kernel_ridge.KernelRidge(alpha=1,*,kernel='linear',gamma=None,degree=3,coef0=1,kernel_params=None)[source]#

Kernel ridge regression.

Kernel ridge regression (KRR) combines ridge regression (linear leastsquares with l2-norm regularization) with the kernel trick. It thuslearns a linear function in the space induced by the respective kernel andthe data. For non-linear kernels, this corresponds to a non-linearfunction in the original space.

The form of the model learned by KRR is identical to support vectorregression (SVR). However, different loss functions are used: KRR usessquared error loss while support vector regression uses epsilon-insensitiveloss, both combined with l2 regularization. In contrast to SVR, fitting aKRR model can be done in closed-form and is typically faster formedium-sized datasets. On the other hand, the learned model is non-sparseand thus slower than SVR, which learns a sparse model for epsilon > 0, atprediction-time.

This estimator has built-in support for multi-variate regression(i.e., when y is a 2d-array of shape [n_samples, n_targets]).

Read more in theUser Guide.

Parameters:
alphafloat or array-like of shape (n_targets,), default=1.0

Regularization strength; must be a positive float. Regularizationimproves the conditioning of the problem and reduces the variance ofthe estimates. Larger values specify stronger regularization.Alpha corresponds to1/(2C) in other linear models such asLogisticRegression orLinearSVC. If an array is passed, penalties areassumed to be specific to the targets. Hence they must correspond innumber. SeeRidge regression and classification for formula.

kernelstr or callable, default=”linear”

Kernel mapping used internally. This parameter is directly passed topairwise_kernels.Ifkernel is a string, it must be one of the metricsinpairwise.PAIRWISE_KERNEL_FUNCTIONS or “precomputed”.Ifkernel is “precomputed”, X is assumed to be a kernel matrix.Alternatively, ifkernel is a callable function, it is called oneach pair of instances (rows) and the resulting value recorded. Thecallable should take two rows from X as input and return thecorresponding kernel value as a single number. This means thatcallables fromsklearn.metrics.pairwise are not allowed, asthey operate on matrices, not single samples. Use the stringidentifying the kernel instead.

gammafloat, default=None

Gamma parameter for the RBF, laplacian, polynomial, exponential chi2and sigmoid kernels. Interpretation of the default value is left tothe kernel; see the documentation for sklearn.metrics.pairwise.Ignored by other kernels.

degreefloat, default=3

Degree of the polynomial kernel. Ignored by other kernels.

coef0float, default=1

Zero coefficient for polynomial and sigmoid kernels.Ignored by other kernels.

kernel_paramsdict, default=None

Additional parameters (keyword arguments) for kernel function passedas callable object.

Attributes:
dual_coef_ndarray of shape (n_samples,) or (n_samples, n_targets)

Representation of weight vector(s) in kernel space

X_fit_{ndarray, sparse matrix} of shape (n_samples, n_features)

Training data, which is also required for prediction. Ifkernel == “precomputed” this is instead the precomputedtraining matrix, of shape (n_samples, n_samples).

n_features_in_int

Number of features seen duringfit.

Added in version 0.24.

feature_names_in_ndarray of shape (n_features_in_,)

Names of features seen duringfit. Defined only whenXhas feature names that are all strings.

Added in version 1.0.

See also

sklearn.gaussian_process.GaussianProcessRegressor

Gaussian Process regressor providing automatic kernel hyperparameters tuning and predictions uncertainty.

sklearn.linear_model.Ridge

Linear ridge regression.

sklearn.linear_model.RidgeCV

Ridge regression with built-in cross-validation.

sklearn.svm.SVR

Support Vector Regression accepting a large variety of kernels.

References

  • Kevin P. Murphy“Machine Learning: A Probabilistic Perspective”, The MIT Presschapter 14.4.3, pp. 492-493

Examples

>>>fromsklearn.kernel_ridgeimportKernelRidge>>>importnumpyasnp>>>n_samples,n_features=10,5>>>rng=np.random.RandomState(0)>>>y=rng.randn(n_samples)>>>X=rng.randn(n_samples,n_features)>>>krr=KernelRidge(alpha=1.0)>>>krr.fit(X,y)KernelRidge(alpha=1.0)
fit(X,y,sample_weight=None)[source]#

Fit Kernel Ridge regression model.

Parameters:
X{array-like, sparse matrix} of shape (n_samples, n_features)

Training data. If kernel == “precomputed” this is insteada precomputed kernel matrix, of shape (n_samples, n_samples).

yarray-like of shape (n_samples,) or (n_samples, n_targets)

Target values.

sample_weightfloat or array-like of shape (n_samples,), default=None

Individual weights for each sample, ignored if None is passed.

Returns:
selfobject

Returns the instance itself.

get_metadata_routing()[source]#

Get metadata routing of this object.

Please checkUser Guide on how the routingmechanism works.

Returns:
routingMetadataRequest

AMetadataRequest encapsulatingrouting information.

get_params(deep=True)[source]#

Get parameters for this estimator.

Parameters:
deepbool, default=True

If True, will return the parameters for this estimator andcontained subobjects that are estimators.

Returns:
paramsdict

Parameter names mapped to their values.

predict(X)[source]#

Predict using the kernel ridge model.

Parameters:
X{array-like, sparse matrix} of shape (n_samples, n_features)

Samples. If kernel == “precomputed” this is instead aprecomputed kernel matrix, shape = [n_samples,n_samples_fitted], where n_samples_fitted is the number ofsamples used in the fitting for this estimator.

Returns:
Cndarray of shape (n_samples,) or (n_samples, n_targets)

Returns predicted values.

score(X,y,sample_weight=None)[source]#

Returncoefficient of determination on test data.

The coefficient of determination,\(R^2\), is defined as\((1 - \frac{u}{v})\), where\(u\) is the residualsum of squares((y_true-y_pred)**2).sum() and\(v\)is the total sum of squares((y_true-y_true.mean())**2).sum().The best possible score is 1.0 and it can be negative (because themodel can be arbitrarily worse). A constant model that always predictsthe expected value ofy, disregarding the input features, would geta\(R^2\) score of 0.0.

Parameters:
Xarray-like of shape (n_samples, n_features)

Test samples. For some estimators this may be a precomputedkernel matrix or a list of generic objects instead with shape(n_samples,n_samples_fitted), wheren_samples_fittedis the number of samples used in the fitting for the estimator.

yarray-like of shape (n_samples,) or (n_samples, n_outputs)

True values forX.

sample_weightarray-like of shape (n_samples,), default=None

Sample weights.

Returns:
scorefloat

\(R^2\) ofself.predict(X) w.r.t.y.

Notes

The\(R^2\) score used when callingscore on a regressor usesmultioutput='uniform_average' from version 0.23 to keep consistentwith default value ofr2_score.This influences thescore method of all the multioutputregressors (except forMultiOutputRegressor).

set_fit_request(*,sample_weight:bool|None|str='$UNCHANGED$')KernelRidge[source]#

Configure whether metadata should be requested to be passed to thefit method.

Note that this method is only relevant when this estimator is used as asub-estimator within ameta-estimator and metadata routing is enabledwithenable_metadata_routing=True (seesklearn.set_config).Please check theUser Guide on how the routingmechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed tofit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it tofit.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains theexisting request. This allows you to change the request for someparameters and not others.

Added in version 1.3.

Parameters:
sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing forsample_weight parameter infit.

Returns:
selfobject

The updated object.

set_params(**params)[source]#

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects(such asPipeline). The latter haveparameters of the form<component>__<parameter> so that it’spossible to update each component of a nested object.

Parameters:
**paramsdict

Estimator parameters.

Returns:
selfestimator instance

Estimator instance.

set_score_request(*,sample_weight:bool|None|str='$UNCHANGED$')KernelRidge[source]#

Configure whether metadata should be requested to be passed to thescore method.

Note that this method is only relevant when this estimator is used as asub-estimator within ameta-estimator and metadata routing is enabledwithenable_metadata_routing=True (seesklearn.set_config).Please check theUser Guide on how the routingmechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed toscore if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it toscore.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains theexisting request. This allows you to change the request for someparameters and not others.

Added in version 1.3.

Parameters:
sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing forsample_weight parameter inscore.

Returns:
selfobject

The updated object.

Gallery examples#

Kernel PCA

Kernel PCA

Comparison of kernel ridge and Gaussian process regression

Comparison of kernel ridge and Gaussian process regression

Comparison of kernel ridge regression and SVR

Comparison of kernel ridge regression and SVR