Cross-validated regression tuning
Selecting regularization strength for regression.
Scikit-Learn · Class
ElasticNetCV fits Elastic Net models along a regularization path and uses cross-validation to select regularization settings. It then refits the chosen model on the full training set.
ElasticNetCV fits Elastic Net models over a regularization path and uses cross-validation to select alpha and, when supplied as a list, l1_ratio. After selection, it refits the chosen model on the complete training set.
class sklearn.linear_model.ElasticNetCV(*, l1_ratio=0.5, eps=0.001, 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')| Name | Type | Description |
|---|---|---|
l1_ratio | float or list of float | L1/L2 mixing ratio or candidate ratios; default 0.5. |
eps | float | Length of the path relative to its maximum regularization value; default 0.001. |
alphas | int or array-like | Number of alpha values to test, or explicit alpha values; default 100. |
fit_intercept | bool | Whether to calculate the intercept; default True. |
precompute | bool or 'auto' or array-like | Precomputation choice for the Gram matrix; default 'auto'. |
max_iter | int | Maximum optimization iterations; default 1000. |
tol | float | Optimization tolerance; default 0.0001. |
cv | int, cross-validation generator or iterable | Cross-validation splitter or fold count; default None. |
copy_X | bool | Whether to copy X; default True. |
verbose | int | Verbosity level; default 0. |
n_jobs | int | Number of jobs used for parallel computation; default None. |
positive | bool | Whether coefficients must be positive; default False. |
An ElasticNetCV estimator fitted by calling fit; fitted attributes include alpha_ and l1_ratio_.
from sklearn.datasets import make_regression
from sklearn.linear_model import ElasticNetCV
X, y = make_regression(n_features=2, random_state=0)
model = ElasticNetCV(cv=5, random_state=0)
model.fit(X, y)
print(model.alpha_, model.l1_ratio_)Fits ElasticNetCV, then prints the regularization settings selected by cross-validation.
Selecting regularization strength for regression.
Balancing sparse and shrinkage behavior.
Working with correlated predictors.
ElasticNetCV searches a regularization path and uses cross-validation to choose alpha and, if multiple candidates are provided, l1_ratio. It then refits the selected model on the full training set.
Follow-up: What does l1_ratio control? It sets the balance between L1 and L2 penalties: 1 gives L1, 0 gives L2, and intermediate values combine them.
The l1_ratio parameter may be a float or a list of floats. A list causes the candidate values to be evaluated by cross-validation, while a single float fixes the mixing ratio.
Follow-up: Why might a list be useful with correlated features? It lets validation choose the sparsity-versus-grouping trade-off rather than imposing one ratio.
The cv parameter controls the cross-validation splitting strategy. None uses the default strategy, an integer specifies the number of folds, and an iterable can provide explicit train/test index splits.
Follow-up: What splitter is used for integer or None inputs? KFold is used.
ElasticNetLassoRidgeMultiTaskElasticNetCVenet_path