Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Scikit-Learn · Class

ElasticNetCV

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.

How it works

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.

Syntax

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')

Parameters

NameTypeDescription
l1_ratiofloat or list of floatL1/L2 mixing ratio or candidate ratios; default 0.5.
epsfloatLength of the path relative to its maximum regularization value; default 0.001.
alphasint or array-likeNumber of alpha values to test, or explicit alpha values; default 100.
fit_interceptboolWhether to calculate the intercept; default True.
precomputebool or 'auto' or array-likePrecomputation choice for the Gram matrix; default 'auto'.
max_iterintMaximum optimization iterations; default 1000.
tolfloatOptimization tolerance; default 0.0001.
cvint, cross-validation generator or iterableCross-validation splitter or fold count; default None.
copy_XboolWhether to copy X; default True.
verboseintVerbosity level; default 0.
n_jobsintNumber of jobs used for parallel computation; default None.
positiveboolWhether coefficients must be positive; default False.

Returns

An ElasticNetCV estimator fitted by calling fit; fitted attributes include alpha_ and l1_ratio_.

Key points

  • The estimator evaluates models along an iterative path of regularization strengths.
  • l1_ratio controls the mixture of L1 and L2 regularization and accepts values from 0 through 1.
  • cv determines how samples are divided into validation folds.
  • max_iter limits the iterations allowed for each optimization run.
  • Cross-validation selects the model configuration with the best validation result.

Common mistakes

  • Cross-validation is followed by a final fit on all training samples, so the fitted estimator is not merely one fold's model.
  • For large inputs, pass X directly to fit in Fortran-contiguous NumPy layout to reduce memory duplication.

Example: Select Elastic Net hyperparameters

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.

When to use it

Cross-validated regression tuning

Selecting regularization strength for regression.

L1/L2 mixture selection

Balancing sparse and shrinkage behavior.

Regularized correlated-feature modeling

Working with correlated predictors.

Interview questions

QWhat does ElasticNetCV select?

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.

QHow is l1_ratio used?

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.

QWhat forms can cv take?

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.

  • ElasticNet
  • Lasso
  • Ridge
  • MultiTaskElasticNetCV
  • enet_path

Source: ElasticNetCV — scikit-learn 1.9.1 documentation. Checked 2026-10-15.