mojolearn.ElasticNetCV#
- class mojolearn.ElasticNetCV(*, l1_ratio=0.5, eps=0.001, n_alphas='deprecated', alphas=100, fit_intercept=True, precompute='auto', max_iter=1000, tol=0.0001, cv=None, copy_X=True, verbose=0, n_jobs=None, positive=False, random_state=None, selection='cyclic')[source]#
Bases:
_LinearRegressorMixin,NumericModeMixinElastic net with
l1_ratioandalphachosen by K-fold cross-validation.Follows scikit-learn’s ElasticNetCV, solved by coordinate descent: for each
l1_ratioevery fold fits the regularization path, the pair with the lowest mean squared error across folds is chosen, and the model is refit on all rows.cvmust be None or an int (unshuffled K-fold);selection='random'raises ValueError.- Parameters:
l1_ratio (float or sequence of float, default 0.5) – L1 share of the penalty, each in (0, 1]; a sequence is searched.
eps (float, default 1e-3) – Path length:
alpha_min / alpha_maxfor a generated grid.n_alphas (int or ‘deprecated’, default ‘deprecated’) – Grid size, used only when
alphasis None; prefer an intalphas.alphas (int or sequence of float, default 100) – Number of alphas on a generated grid, or the alphas to try.
fit_intercept (bool, default True) – Whether to fit an intercept.
precompute (bool, ‘auto’ or array, default ‘auto’) – Accepted for compatibility; ignored.
max_iter (int, default 1000) – Maximum number of coordinate-descent passes per alpha.
tol (float, default 1e-4) – Coordinate-descent stopping tolerance.
cv (int or None, default None) – Number of folds; None means 5.
copy_X (bool, default True) – Accepted for compatibility;
Xis never modified.verbose (int, default 0) – Accepted for compatibility; ignored.
n_jobs (int or None, default None) – Accepted for compatibility; ignored.
positive (bool, default False) – Restrict the weights to be non-negative.
random_state (int or None, default None) – Accepted for compatibility; ignored.
selection ({‘cyclic’}, default ‘cyclic’) – Only ‘cyclic’ is supported.
numeric_mode (str or None, default None) – Numeric mode for this model (
'identical','deterministic'or'fast', which runs on Apple GPUs only); None uses the process default (seemojolearn.set_numeric_mode()).
- Variables:
alpha_ (float) – Selected alpha.
l1_ratio_ (float) – Selected l1_ratio (one of the values given).
alphas_ (float32 array of shape (n_alphas,) or (n_l1_ratio, n_alphas)) – Alphas tried, in decreasing order; one row per l1_ratio when several.
mse_path_ (float32 array of shape (n_alphas, n_folds) or (n_l1_ratio, n_alphas, n_folds)) – Mean squared error of each alpha on each fold.
coef_ (float32 array of shape (n_features,)) – Weights of the refit model.
intercept_ (float) – Intercept.
n_iter_ (int) – Coordinate-descent passes of the refit.
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 = 3 * X[:, 0] - 2 * X[:, 1] >>> model = mojolearn.ElasticNetCV(l1_ratio=[0.2, 0.5, 0.9]).fit(X, y) >>> model.l1_ratio_, model.alpha_
- __init__(*, l1_ratio=0.5, eps=0.001, n_alphas='deprecated', alphas=100, fit_intercept=True, precompute='auto', max_iter=1000, tol=0.0001, cv=None, copy_X=True, verbose=0, n_jobs=None, positive=False, random_state=None, selection='cyclic')[source]#
Methods
fit(X, y)Fit the model and select
l1_ratioandalpha.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)againsty.set_params(**params)Set constructor parameters.
vendor_used()The GPU backend this estimator runs on.
- fit(X, y)[source]#
Fit the model and select
l1_ratioandalpha.- 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.
- Returns:
self (ElasticNetCV) – The fitted model.
- Raises:
ValueError – If an
l1_ratiois outside (0, 1],selectionis not ‘cyclic’,cvis not None or an int in [2, n_samples], or no alpha is given.