mojolearn.RidgeCV#

class mojolearn.RidgeCV(alphas=(0.1, 1.0, 10.0), *, fit_intercept=True, scoring=None, cv=None, gcv_mode=None, store_cv_results=False, alpha_per_target=False)#

Ridge regression with built-in selection of alpha.

Follows scikit-learn’s RidgeCV. With cv=None the alpha is chosen by efficient leave-one-out cross-validation; with cv=k (an int) by unshuffled K-fold cross-validation scored by R^2, after which the model is refit on all rows with the best alpha (the first one on a tie). Cross-validation objects for cv, scoring, alpha_per_target and a 2-D y are not supported.

Parameters:
  • alphas (sequence of float, default (0.1, 1.0, 10.0)) – Candidate regularization strengths; all must be > 0.

  • fit_intercept (bool, default True) – Whether to fit an intercept.

  • scoring (None, default None) – Only None is supported; anything else raises ValueError.

  • cv (int or None, default None) – None for leave-one-out, or the number of folds (2 to n_samples).

  • gcv_mode (str or None, default None) – Accepted for compatibility; ignored.

  • store_cv_results (bool, default False) – Store cv_results_; only with cv=None.

  • alpha_per_target (bool, default False) – Only False is supported; True raises ValueError.

  • numeric_mode (str or None, default None) – Numeric mode for this model ('identical' or 'fast', which runs on Apple GPUs only); None uses the process default (see mojolearn.set_numeric_mode()).

Variables:
  • alpha_ (float) – Selected alpha.

  • best_score_ (float) – Score of the selected alpha: minus its mean squared leave-one-out error with cv=None, its mean K-fold R^2 with cv=k.

  • coef_ (float32 array of shape (n_features,)) – Weights.

  • intercept_ (float) – Intercept.

  • cv_results_ (float32 array of shape (n_alphas,)) – Mean squared leave-one-out error of each alpha, when store_cv_results=True (scikit-learn stores the per-sample errors).

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

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)
>>> model = mojolearn.RidgeCV(alphas=(0.01, 0.1, 1.0)).fit(X, y)
>>> model.alpha_

Methods

fit(X, y[, sample_weight])

Fit the model and select alpha.

get_params([deep])

numeric_mode_used()

The numeric mode this estimator runs in.

predict(X)

Predict targets for X.

score(X, y)

Coefficient of determination R^2 of predict(X) against y.

set_params(**params)

Set constructor parameters.

vendor_used()

The GPU backend this estimator runs on.

fit(X, y, sample_weight=None)#

Fit the model and select alpha.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Training data, converted to float32; values must be finite.

  • y (array-like of shape (n_samples,)) – Targets (1-D).

  • sample_weight (array-like of shape (n_samples,) or float, optional) – Non-negative weights with a positive sum; only with cv=None.

Returns:

self (RidgeCV) – The fitted model.

Raises:

ValueError – On an unsupported option, a non-positive alpha, an invalid cv, sample_weight or store_cv_results with cv set, or when X'X + alpha I cannot be factored.

numeric_mode_used()#

The numeric mode this estimator runs in.

Returns:

str – 'identical' or 'fast', read from the compiled binary the estimator uses, not from the argument it was given.

predict(X)#

Predict targets for X.

Returns X @ coef_ + intercept_; models with a log link (Poisson, Gamma and Tweedie with power > 0) return its exponential.

Parameters:

X (array-like of shape (n_samples, n_features)) – Samples, converted to float32. The feature count must match the one seen in fit.

Returns:

y_pred (float32 array of shape (n_samples,)) – Predicted values.

Raises:
  • RuntimeError – If the model is not fitted.

  • ValueError – If X has a different number of features than in fit.

score(X, y)#

Coefficient of determination R^2 of predict(X) against y.

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

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

Returns:

score (float) – R^2; 1.0 is a perfect fit.

set_params(**params)#

Set constructor parameters. The estimator is rebuilt through its constructor, so every constructor check runs again and any fitted state is dropped, as a new setting invalidates it.

vendor_used()#

The GPU backend this estimator runs on.

Returns:

str – 'metal', 'cuda' or 'hip', read from the compiled binary the estimator uses.