mojolearn.LinearRegression#

class mojolearn.LinearRegression(*, fit_intercept=True)[source]#

Bases: NumericModeMixin

Ordinary least squares linear regression.

Fits y ~ X @ coef_ + intercept_ by minimizing the sum of squared residuals, the same model as scikit-learn’s LinearRegression.

Tall designs (more rows than features, up to 511 features) are solved by a QR factorization of [X | y] followed by an SVD of its small triangular factor, so the condition number of X is not squared. Other designs, and fits in fast mode on Apple GPUs, solve the normal equations through an eigendecomposition of the (equilibrated) Gram matrix; that squares the condition number, so prefer well conditioned designs there. A design with more features than rows gets the minimum-norm least-squares solution. Rank-deficient designs are handled by a pseudo-inverse whose cutoff does not depend on a column’s units.

X is read as float32 (a C-contiguous float32 array is used without a copy). y must be one-dimensional: a single target. With fit_intercept=True the data are centered on their (weighted) means before the solve, as in scikit-learn and cuML.

Parameters:
  • fit_intercept (bool, default True) – Whether to fit an intercept. When False the data are assumed to be centered and intercept_ is 0.0.

  • 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.

  • 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.LinearRegression().fit(X, y)
>>> pred = model.predict(X)
__init__(*, fit_intercept=True)[source]#

Methods

fit(X, y[, sample_weight])

Fit the linear 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 linear 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 linear 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 (array-like of shape (n_samples,), default None) – Non-negative, finite per-row weights. A weighted fit minimizes sum(w_i * r_i**2), and its intercept uses weighted means. A weight array of the wrong shape or length, or with a negative or non-finite entry, raises ValueError.

Returns:

self (LinearRegression) – The fitted estimator.

predict(X)[source]#

Predict with the fitted linear 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 coef_, the intercept (as float64), the feature count, fit_intercept and the numeric mode. Equal models give byte-identical files. 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 and numeric mode. It carries no training-time means, so it is for prediction, not for continuing a fit.

Parameters:

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

Returns:

model (LinearRegression)

Raises:

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