mojolearn.Lasso#

class mojolearn.Lasso(alpha=1.0, *, fit_intercept=True, max_iter=1000, tol=0.001, solver='auto', selection='cyclic', precompute=False, copy_X=True, warm_start=False, positive=False, random_state=None)#

Linear regression with an l1 penalty, fitted by coordinate descent.

Lasso(alpha) is ElasticNet(alpha, l1_ratio=1.0): it minimizes (1 / (2 * n_samples)) * ||y - X w||^2 + alpha * ||w||_1, the same objective as scikit-learn’s Lasso. Everything in ElasticNet’s description applies, including the stopping rule, the absolute zero-column threshold and the refused options.

tol defaults to 1e-3 (cuML’s); scikit-learn’s is 1e-4.

Parameters:
  • alpha (float, default 1.0) – Penalty strength, finite and >= 0.

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

  • max_iter (int, default 1000) – Maximum number of coordinate-descent epochs.

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

  • solver, selection, precompute, copy_X, warm_start, positive, random_state – As on ElasticNet.

Variables:
  • coef_ (array of shape (n_features,), float32) – The fitted coefficients; the l1 penalty sets some exactly to zero.

  • intercept_ (float) – The fitted intercept; exactly 0.0 when fit_intercept is False.

  • n_iter_ (int) – The number of epochs run.

  • n_features_in_ (int) – The 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) + 1.0
>>> model = mojolearn.Lasso(alpha=0.01).fit(X, y)
>>> pred = model.predict(X)

Methods

fit(X, y[, sample_weight])

Fit the model by coordinate descent.

get_params([deep])

load(path)

Load a model written by save.

predict(X)

Predict with the fitted 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.

fit(X, y, sample_weight=None)#

Fit the model by coordinate descent.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Dense training data, read as float32. Sparse input raises NotImplementedError.

  • y (array-like of shape (n_samples,)) – Target values. A 2-D y or a length mismatch raises ValueError.

  • sample_weight (None) – Must be None; weights raise NotImplementedError.

Returns:

self (ElasticNet) – The fitted estimator.

classmethod load(path)#

Load a model written by save.

The loaded model predicts with the saved coefficients and intercept and carries the saved hyperparameters and n_iter_.

Parameters:

path (str or path-like) – A file written by save of the same class.

Returns:

model (ElasticNet or Lasso)

Raises:

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

predict(X)#

Predict with the fitted model.

Parameters:

X (array-like of shape (n_samples, n_features)) – Samples, read as float32; at least two rows.

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.

save(path)#

Save the fitted model to path as an .npz file.

The file stores coef_, the intercept, alpha, l1_ratio, tol, max_iter, fit_intercept, n_iter_ and the numeric mode. 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.

score(X, y)#

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 scores 0.0.

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.