mojolearn.LinearRegression#
- class mojolearn.LinearRegression(*, fit_intercept=True)[source]#
Bases:
NumericModeMixinOrdinary least squares linear regression.
Fits
y ~ X @ coef_ + intercept_by minimizing the sum of squared residuals, the same model as scikit-learn’sLinearRegression.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 ofXis not squared. Other designs, and fits infastmode 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.Xis read as float32 (a C-contiguous float32 array is used without a copy).ymust be one-dimensional: a single target. Withfit_intercept=Truethe 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:
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)
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
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 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
yraisesValueError.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, raisesValueError.
- 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
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
coef_, the intercept (as float64), the feature count,fit_interceptand 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.