mojolearn.Ridge#
- class mojolearn.Ridge(*, alpha=1.0, solver='auto', fit_intercept=True, normalize=False)[source]#
Bases:
NumericModeMixinLinear least squares with l2 regularization.
Minimizes
||y - X w||^2 + alpha * ||w||^2, the same objective and the same minimizer as scikit-learn’sRidge. The solve is cuML’ssolver='eig': an eigendecomposition ofX.T @ X, which squares the condition number ofX(seeLinearRegression). The intercept is not penalized: withfit_intercept=Truethe data are centered on their means before the solve.Xis read as float32 (a C-contiguous float32 array is used without a copy).ymust be one-dimensional: a single target.sample_weightis not supported, and a design with a single feature andalpha > 0is 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'raiseNotImplementedError; any other value raisesTypeError.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_interceptis False.solver_ (str) – The solver used, always
'eig'.n_features_in_ (int) – The number of features seen in
fit.input_copied_ (bool) – Whether
fithad to copyXto 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)
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
pathas an.npzfile.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
yraisesValueError.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
Xhas 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 constanty(SS_tot == 0) scores 0.0.
- save(path)[source]#
Save the fitted model to
pathas an.npzfile.The file stores what
LinearRegression.savestores plusalpha.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,
alphaand 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.