himalaya.kernel_ridge.KernelRidge¶
- class himalaya.kernel_ridge.KernelRidge(alpha=1, kernel='linear', kernel_params=None, solver='auto', solver_params=None, fit_intercept=False, force_cpu=False, warn=True)[source]¶
Kernel ridge regression.
Solve the kernel ridge regression:
w* = argmin_w ||K @ w - Y||^2 + alpha (w.T @ K @ w)
where K is a kernel computed on the input X. The default kernel is linear (K = X @ X.T).
- Parameters
- alphafloat, or array of shape (n_targets, )
L2 regularization parameter.
- kernelstr or callable, default=”linear”
Kernel mapping. Available kernels are: ‘linear’, ‘polynomial, ‘poly’, ‘rbf’, ‘sigmoid’, ‘cosine’, or ‘precomputed’. Set to ‘precomputed’ in order to pass a precomputed kernel matrix to the estimator methods instead of samples. A callable should accept two arguments and the keyword arguments passed to this object as kernel_params, and should return a floating point number.
- kernel_paramsdict or None
Additional parameters for the kernel function. See more details in the docstring of the function:
KernelRidge.ALL_KERNELS[kernel]
- solverstr
Algorithm used during the fit, “eigenvalues”, “conjugate_gradient”, “gradient_descent”, or “auto”. If “auto”, use “eigenvalues” if
alpha
is a float, and “conjugate_gradient” isalpha
is an array.- solver_paramsdict or None
Additional parameters for the solver. See more details in the docstring of the function:
KernelRidge.ALL_SOLVERS[solver]
- fit_interceptboolean
Whether to fit an intercept. If False, X and Y must be zero-mean over samples.
- force_cpubool
If True, computations will be performed on CPU, ignoring the current backend. If False, use the current backend.
- warnbool
If True, warn if the number of samples is larger than the number of features, and if the kernel is linear.
Examples
>>> from himalaya.kernel_ridge import KernelRidge >>> import numpy as np >>> n_samples, n_features, n_targets = 10, 5, 3 >>> X = np.random.randn(n_samples, n_features) >>> Y = np.random.randn(n_samples, n_targets) >>> model = KernelRidge() >>> model.fit(X, Y) KernelRidge()
- Attributes
- dual_coef_array of shape (n_samples) or (n_samples, n_targets)
Representation of weight vectors in kernel space.
- intercept_float or array of shape (n_targets, )
Intercept. Only present if fit_intercept is True.
- X_fit_array of shape (n_samples, n_features)
Training data. If kernel == “precomputed” this is None.
- n_features_in_int
Number of features (or number of samples if kernel == “precomputed”) used during the fit.
- dtype_str
Dtype of input data.
Methods
fit
(X[, y, sample_weight])Fit the model.
get_params
([deep])Get parameters for this estimator.
get_primal_coef
([X_fit])Returns the primal coefficients, assuming the kernel is linear.
predict
(X)Predict using the model.
score
(X, y)Return the coefficient of determination R^2 of the prediction.
set_params
(**params)Set the parameters of this estimator.