mojolearn.Ridge#

class mojolearn.Ridge(*, alpha=1.0, solver='auto', fit_intercept=True, normalize=False)[source]#

Bases: NumericModeMixin

Linear least squares with l2 regularization.

Minimizes ||y - X w||^2 + alpha * ||w||^2, the same objective and the same minimizer as scikit-learn’s Ridge. The solve is cuML’s solver='eig': an eigendecomposition of X.T @ X, which squares the condition number of X (see LinearRegression). The intercept is not penalized: with fit_intercept=True the data are centered on their means before the solve.

X is read as float32 (a C-contiguous float32 array is used without a copy). y must be one-dimensional: a single target. sample_weight is not supported, and a design with a single feature and alpha > 0 is refused with an error.

Parameters:
  • alpha (float, default 1.0) – Regularization strength, non-negative. 0.0 gives ordinary least squares. A negative value raises ValueError.

  • solver ({‘auto’, ‘eig’}, default ‘auto’) – 'auto' means 'eig', the one solver implemented. 'svd' and 'cd' raise NotImplementedError; any other value raises TypeError.

  • fit_intercept (bool, default True) – Whether to fit an intercept. When False intercept_ is 0.0.

  • normalize (bool, default False) – Only False is supported; True raises NotImplementedError.

  • numeric_mode ({‘identical’, ‘fast’} or None, default None) – The numeric mode for this estimator; None uses the process default (see mojolearn.set_numeric_mode). 'fast' is for Apple GPUs.

Variables:
  • coef_ (array of shape (n_features,), float32) – The fitted coefficients.

  • intercept_ (float) – The fitted intercept; 0.0 when fit_intercept is False.

  • solver_ (str) – The solver used, always 'eig'.

  • n_features_in_ (int) – The number of features seen in fit.

  • input_copied_ (bool) – Whether fit had to copy X to float32 C order.

Examples

>>> import numpy as np, mojolearn
>>> rng = np.random.default_rng(0)
>>> X = rng.random((1000, 8), dtype=np.float32)
>>> y = X @ np.arange(8, dtype=np.float32) + 1.0
>>> model = mojolearn.Ridge(alpha=0.5).fit(X, y)
>>> pred = model.predict(X)
__init__(*, alpha=1.0, solver='auto', fit_intercept=True, normalize=False)[source]#

Methods

fit(X, y[, sample_weight])

Fit the 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 ridge model.

save(path)

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

score(X, y)

Return the coefficient of determination R^2 of the prediction.

set_params(**params)

Set constructor parameters.

vendor_used()

The GPU backend this estimator runs on.

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

Fit the ridge model.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Training data, read as float32.

  • y (array-like of shape (n_samples,)) – Target values. A 2-D y raises ValueError.

  • sample_weight (None) – Must be None; weights raise NotImplementedError.

Returns:

self (Ridge) – The fitted estimator.

predict(X)[source]#

Predict with the fitted ridge model.

Parameters:

X (array-like of shape (n_samples, n_features)) – Samples, read as float32.

Returns:

y_pred (array of shape (n_samples,), float32) – X @ coef_ + intercept_.

Raises:

ValueError – If the model is not fitted, or X has a different number of features than the training data.

score(X, y)[source]#

Return the coefficient of determination R^2 of the prediction.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Test samples.

  • y (array-like of shape (n_samples,)) – True target values.

Returns:

score (float) – 1 - SS_res / SS_tot, computed in float64. A constant y (SS_tot == 0) scores 0.0.

save(path)[source]#

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

The file stores what LinearRegression.save stores plus alpha. mojolearn.host_model(path) can predict from the file on a machine without a GPU.

Parameters:

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

Raises:

RuntimeError – If the model is not fitted.

classmethod load(path)[source]#

Load a model written by save.

The loaded model predicts with the saved coefficients, intercept, alpha and numeric mode; solver_ is 'eig'.

Parameters:

path (str or path-like) – A file written by Ridge.save.

Returns:

model (Ridge)

Raises:

ValueError – If the file was saved by a different estimator or its fields do not match.