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

Scikit-Learn · Class

ElasticNet

ElasticNet is linear regression with combined L1 and L2 regularization. Use alpha to control overall penalty strength and l1_ratio to control the L1/L2 balance.

How it works

ElasticNet fits a linear regression model while penalizing both the absolute and squared coefficient magnitudes. The alpha value controls overall regularization, and l1_ratio controls the balance between L1 sparsity and L2 shrinkage.

Syntax

ElasticNet(alpha=1.0, l1_ratio=0.5, fit_intercept=True, precompute=False, max_iter=1000, copy_X=True, tol=0.0001, warm_start=False, positive=False, random_state=None, selection='cyclic')

Parameters

NameTypeDescription
alphafloat, default=1.0Constant that multiplies the combined regularization terms.
l1_ratiofloat, default=0.5ElasticNet mixing parameter between 0 and 1.
fit_interceptbool, default=TrueWhether to calculate the intercept for the model.
precomputebool or array-like, default=FalseWhether to use a precomputed Gram matrix.
max_iterint, default=1000Maximum number of iterations.
copy_Xbool, default=TrueWhether to copy X before fitting.
tolfloat, default=0.0001Tolerance for optimization stopping.
warm_startbool, default=FalseWhether to reuse the previous solution as initialization.
positivebool, default=FalseWhether coefficients must be positive.
random_stateint, RandomState instance or None, default=NoneSeed or random-state object used by randomized selection.
selection{'cyclic', 'random'}, default='cyclic'Coordinate update strategy: cyclic or random.

Returns

A fitted ElasticNet estimator; predict returns the model's predictions for supplied samples.

Key points

  • ElasticNet is a linear regression estimator using both L1 and L2 regularization.
  • l1_ratio must lie between 0 and 1 and determines the mixture of the two penalties.
  • Setting l1_ratio to zero produces the Ridge-like L2 penalty.
  • Setting l1_ratio to one produces the Lasso-like L1 penalty.
  • Values strictly between zero and one apply both regularization terms.
  • The constructor exposes controls for regularization, fitting, convergence, coefficient constraints, reproducibility, and coordinate selection.

Example: Fit and predict with ElasticNet

from sklearn.datasets import make_regression
from sklearn.linear_model import ElasticNet

X, y = make_regression(n_features=2, random_state=0)
model = ElasticNet(alpha=0.1, l1_ratio=0.5, random_state=0)
model.fit(X, y)
predictions = model.predict(X[:3])
print(predictions)

This creates a small regression dataset, fits ElasticNet with mixed L1/L2 regularization, and predicts three samples.

When to use it

Sparse regression

Select useful predictors while fitting a regression model.

Correlated features

Handle correlated predictors more stably than a purely L1-regularized model.

Regularized model selection

Tune regularization strength and penalty balance externally, for example with cross-validation.

Interview questions

QHow does ElasticNet combine L1 and L2 regularization?

ElasticNet minimizes squared prediction error plus a weighted combination of L1 and L2 penalties. alpha sets the total regularization strength, while l1_ratio allocates it between L1 and L2.

Follow-up: What happens at the endpoints? With l1_ratio=0 the penalty is L2, and with l1_ratio=1 it is L1; intermediate values combine both.

QWhat are the main fitted attributes and prediction method?

fit_intercept determines whether the estimator calculates an intercept. The fitted coefficients are stored in coef_, and the intercept is stored in intercept_.

Follow-up: How can predictions be generated after fitting? Call predict(X), which returns the model output for the supplied samples.

QWhat does the selection parameter control?

selection='cyclic' updates coefficients in a fixed order, while selection='random' chooses a coefficient randomly at each iteration. Random selection can converge faster when tol is relaxed.

Follow-up: How can reproducibility be controlled for random selection? Set random_state to an integer so the random coordinate choices are repeatable.

  • sklearn.linear_model.ElasticNetCV
  • sklearn.linear_model.Lasso
  • sklearn.linear_model.Ridge

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