mojolearn.GammaRegressor#

class mojolearn.GammaRegressor(*, alpha=1.0, fit_intercept=True, solver='lbfgs', max_iter=100, tol=0.0001, warm_start=False, verbose=0)#

Generalized linear model with a Gamma distribution and a log link.

Follows scikit-learn’s GammaRegressor; targets must be positive. predict returns exp(X @ coef_ + intercept_). Unlike scikit-learn, score returns R^2, not the deviance-based D^2.

Parameters:
  • alpha (float, default 1.0) – L2 regularization strength (>= 0).

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

  • solver ({‘lbfgs’, ‘newton-cholesky’}, default ‘lbfgs’) – Accepted for compatibility; both run a Newton-Cholesky solver.

  • max_iter (int, default 100) – Maximum number of solver iterations.

  • tol (float, default 1e-4) – Stopping tolerance.

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

  • verbose (int, default 0) – Accepted for compatibility; ignored.

  • 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:
  • coef_ (float32 array of shape (n_features,)) – Weights.

  • intercept_ (float) – Intercept.

  • n_iter_ (int) – Number of solver 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 = rng.gamma(2.0, np.exp(X[:, 0])).astype(np.float32)
>>> model = mojolearn.GammaRegressor(alpha=0.01).fit(X, y)

See also

User guide: Linear models

Choosing an estimator

Methods

fit(X, y[, sample_weight])

Fit the generalized linear model.

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 generalized linear model.

Both accepted solver names run the same Newton-Cholesky solver with a line search.

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, inside the valid range of the model’s distribution.

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

Returns:

self (object) – The fitted model.

Raises:

ValueError – If solver is not ‘lbfgs’ or ‘newton-cholesky’, warm_start is True, alpha is negative, or a value of y is outside the valid range of the loss.

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.