mojolearn.KernelRidge#

class mojolearn.KernelRidge(alpha=1.0, kernel='linear', gamma=None, degree=3, coef0=1.0)[source]#

Bases: _KernelMethodBase

Kernel ridge regression.

Follows sklearn.kernel_ridge.KernelRidge (and cuML’s). Fitting forms the kernel matrix K of the training rows, adds alpha to its diagonal, factors it with a Cholesky factorization and solves for the dual coefficients; prediction is K(X, X_fit_) @ dual_coef_. The training matrix is kept on the model because prediction needs it.

If K + alpha * I cannot be factored at the given alpha, fit raises an error rather than falling back to a least-squares solve; increase alpha in that case. Inputs and outputs are float32. Supports the 'identical' (default) and 'fast' numeric modes; 'fast' is available on Apple GPUs only.

Parameters:
  • alpha (float, default 1.0) – Regularization strength, added to the kernel matrix diagonal. Must be >= 0; zero is accepted, negative or NaN is refused.

  • kernel (str, default ‘linear’) – One of ‘linear’, ‘poly’, ‘rbf’, ‘sigmoid’, ‘laplacian’, ‘cosine’, ‘chi2’, ‘additive_chi2’ or ‘precomputed’. ‘cosine’, ‘chi2’ and ‘additive_chi2’ are scikit-learn’s pairwise kernels; both chi2 kernels refuse negative values in X with ValueError, as scikit-learn’s do. With ‘precomputed’, X is the n x n kernel matrix at fit and the q x n cross-kernel against the training rows at predict, used as given (symmetry is not checked).

  • gamma (float or None, default None) – Kernel coefficient for ‘poly’, ‘rbf’, ‘sigmoid’, ‘laplacian’ and the chi2 kernels. None means 1 / n_features (1.0 for ‘chi2’, its scikit-learn default). Must be positive where the kernel reads it.

  • degree (int, default 3) – Degree of the ‘poly’ kernel.

  • coef0 (float, default 1.0) – Independent term of the ‘poly’ and ‘sigmoid’ kernels.

  • numeric_mode ({‘identical’, ‘fast’} or None, default None) – Numeric mode for this estimator; None uses the process default.

Variables:
  • dual_coef_ (Array of shape (n_samples,) or (n_samples, n_targets), float32) – Dual coefficients, 1-D when y was 1-D.

  • X_fit_ (Array of shape (n_samples, n_features), float32) – The training data, used by predict.

  • info_ (int) – Status of the factorization; always 0 on a fitted model.

  • n_features_in_ (int) – Number of features seen during fit.

  • n_targets_ (int) – Number of regression targets.

Examples

>>> import numpy as np, mojolearn
>>> rng = np.random.default_rng(0)
>>> X = rng.random((1000, 8), dtype=np.float32)
>>> y = X.sum(axis=1)
>>> model = mojolearn.KernelRidge(alpha=1.0, kernel="rbf").fit(X, y)
>>> pred = model.predict(X[:5])
__init__(alpha=1.0, kernel='linear', gamma=None, degree=3, coef0=1.0)[source]#

Methods

fit(X, y[, sample_weight])

Fit the kernel ridge model.

get_params([deep])

load(path)

Load a model written by save.

numeric_mode_used()

The numeric mode this estimator runs in.

predict(X)

Predict with the fitted model: K(X, X_fit_) @ dual_coef_.

save(path)

Save the fitted model to path as an .npz file.

set_params(**params)

Set constructor parameters.

vendor_used()

The GPU backend this estimator runs on.

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

Fit the kernel ridge model.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Training data, converted to float32. With kernel='precomputed', the square n x n kernel matrix.

  • y (array-like of shape (n_samples,) or (n_samples, n_targets)) – Target values.

  • sample_weight (float or array-like of shape (n_samples,), default None) – Per-sample weights, finite and >= 0, applied as scikit-learn does. None weights every sample equally.

Returns:

self (KernelRidge) – The fitted estimator.

predict(X)[source]#

Predict with the fitted model: K(X, X_fit_) @ dual_coef_.

Parameters:

X (array-like of shape (n_queries, n_features)) – Query data. With kernel='precomputed', the kernel between the queries and the training rows, shape (n_queries, n_samples).

Returns:

Array of shape (n_queries,) or (n_queries, n_targets), float32 – Predictions, 1-D when the fitted y was 1-D.

save(path)[source]#

Save the fitted model to path as an .npz file.

The file holds the training data, the dual coefficients, the kernel parameters as resolved at fit and the numeric mode. Load it with KernelRidge.load, or predict from it on a CPU without a GPU with mojolearn.host_model(path).

Parameters:

path (str or path-like) – Destination file.

Raises:

RuntimeError – If the model has not been fitted.

classmethod load(path)[source]#

Load a model written by save.

Parameters:

path (str or path-like) – File written by KernelRidge.save.

Returns:

KernelRidge – A fitted model, ready to predict, with the saved numeric mode.