mojolearn.ElasticNet#

class mojolearn.ElasticNet(alpha=1.0, *, l1_ratio=0.5, 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)[source]#

Bases: ParamsMixin

Linear regression with a combined l1 and l2 penalty, fitted by coordinate descent.

Minimizes scikit-learn’s elastic-net objective:

(1 / (2 * n_samples)) * ||y - X w||^2
+ alpha * l1_ratio * ||w||_1
+ 0.5 * alpha * (1 - l1_ratio) * ||w||^2

so alpha and l1_ratio mean the same as in scikit-learn’s and cuML’s ElasticNet. The solver is cuML’s coordinate descent (solver='cd'), and it differs from scikit-learn in ways that can change results:

  • It stops when the largest coefficient, or the largest coefficient change relative to it, falls below tol after an epoch. There is no duality-gap check, so there is no dual_gap_ attribute.

  • tol defaults to 1e-3 (cuML’s); scikit-learn’s is 1e-4. Set it explicitly for a like-for-like comparison.

  • A column whose sum of squares is at most 1e-5 gets a zero coefficient. This threshold is absolute, so very small-valued features can be zeroed where the same data in larger units would not be. scikit-learn zeroes only an all-zero column.

X must be dense and is read as float32; the caller’s array is never modified. y must be one-dimensional. At least two rows are required, in fit and in predict. With fit_intercept=True the data are centered on the GPU. ElasticNet has no numeric_mode parameter.

Parameters:
  • alpha (float, default 1.0) – Overall penalty strength, >= 0. A negative value raises ValueError; a non-finite value is refused by fit.

  • l1_ratio (float, default 0.5) – The l1 share of the penalty, in [0, 1] (ValueError otherwise); 1.0 is the lasso.

  • 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 (see above).

  • solver ({‘auto’, ‘cd’}, default ‘auto’) – Both mean coordinate descent. 'qn' raises NotImplementedError; any other value raises ValueError.

  • selection ({‘cyclic’}, default ‘cyclic’) – Coordinates are updated in order. 'random' raises NotImplementedError; any other value raises ValueError.

  • precompute (False) – Only False is supported; anything else raises NotImplementedError.

  • copy_X (True) – Only True is supported; False raises NotImplementedError.

  • warm_start (False) – Only False is supported (every fit starts from zero); True raises NotImplementedError.

  • positive (False) – Only False is supported; True raises NotImplementedError.

  • random_state (None, int or NumPy random state, default None) – Accepted for compatibility and unused, as in scikit-learn with selection='cyclic'.

Variables:
  • coef_ (array of shape (n_features,), float32) – The fitted coefficients.

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

  • n_iter_ (int) – The number of epochs run. In identical mode it is the same on NVIDIA and AMD GPUs; in other modes it can differ between GPUs.

  • n_features_in_ (int) – The number of features seen in fit.

  • input_copied_ (bool) – Whether fit had to copy X. A float32 array in C or Fortran order is used without a copy.

  • fortran_copied_ (bool) – The same flag as input_copied_, kept for compatibility.

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.ElasticNet(alpha=0.01, l1_ratio=0.7).fit(X, y)
>>> pred = model.predict(X)
__init__(alpha=1.0, *, l1_ratio=0.5, 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)[source]#

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

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.

predict(X)[source]#

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

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.

classmethod load(path)[source]#

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.

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