mojolearn.BayesianRidge#

class mojolearn.BayesianRidge(*, max_iter=300, tol=0.001, alpha_1=1e-06, alpha_2=1e-06, lambda_1=1e-06, lambda_2=1e-06, alpha_init=None, lambda_init=None, compute_score=False, fit_intercept=True, copy_X=True, verbose=False)[source]#

Bases: _LinearRegressorMixin, NumericModeMixin

Bayesian ridge regression.

Follows scikit-learn’s BayesianRidge: estimates the noise precision alpha_ and the weight precision lambda_ by maximizing the marginal likelihood (evidence). compute_score=True and predict(return_std=True) raise ValueError.

Parameters:
  • max_iter (int, default 300) – Maximum number of iterations.

  • tol (float, default 1e-3) – Stop when the weights change by less than this.

  • alpha_1, alpha_2 (float, default 1e-6) – Shape and rate of the Gamma prior over alpha.

  • lambda_1, lambda_2 (float, default 1e-6) – Shape and rate of the Gamma prior over lambda.

  • alpha_init (float or None, default None) – Starting value of alpha; None starts from the inverse variance of y.

  • lambda_init (float or None, default None) – Starting value of lambda; None starts from 1.

  • compute_score (bool, default False) – Not supported: True raises ValueError.

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

  • copy_X (bool, default True) – Accepted for compatibility; X is never modified.

  • verbose (bool, default False) – Accepted for compatibility; ignored.

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

Variables:
  • coef_ (float32 array of shape (n_features,)) – Posterior mean of the weights.

  • intercept_ (float) – Intercept.

  • alpha_ (float) – Estimated noise precision.

  • lambda_ (float) – Estimated weight precision.

  • n_iter_ (int) – Number of iterations run.

  • 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.BayesianRidge().fit(X, y)
__init__(*, max_iter=300, tol=0.001, alpha_1=1e-06, alpha_2=1e-06, lambda_1=1e-06, lambda_2=1e-06, alpha_init=None, lambda_init=None, compute_score=False, fit_intercept=True, copy_X=True, verbose=False)[source]#

Methods

fit(X, y[, sample_weight])

Fit the model.

get_params([deep])

numeric_mode_used()

The numeric mode this estimator runs in.

predict(X[, return_std])

Predict targets with the posterior mean of the weights.

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)[source]#

Fit the model.

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.

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

Returns:

self (BayesianRidge) – The fitted model.

Raises:

ValueError – If compute_score is True or the weights are invalid.

predict(X, return_std=False)[source]#

Predict targets with the posterior mean of the weights.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Samples, converted to float32.

  • return_std (bool, default False) – Not supported: True raises ValueError.

Returns:

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