Elastic Net Regularization › Formula
Machine Learning from ScratchAll chapters Elastic Net Regularization › FormulaOne section · Sections in this chapter2 / 8 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Let's build up the Elastic Net objective function step by step, starting from the most intuitive form and explaining each mathematical transformation along the way.
Starting with the Basic Regression Problem
We begin with the standard multiple linear regression problem. Our goal is to find coefficients that minimize the sum of squared errors between our predictions and the actual values:
where:
- is the actual target value for observation (where )
- is the predicted value for observation
- is the number of observations in the dataset
Here, represents our predicted value for observation , which we calculate as:
where:
- is the intercept term (constant offset)
- is the coefficient (weight) for feature (where )
- is the value of feature for observation
- is the number of features (predictors) in the model
Adding Regularization Penalties
Now, instead of just minimizing the sum of squared errors, we want to add penalty terms that will help us control the complexity of our model. Elastic Net adds two types of penalties:
where:
- is the L1 regularization parameter (controls the strength of the L1 penalty)
- is the L2 regularization parameter (controls the strength of the L2 penalty)
- is the absolute value of coefficient
- is the squared value of coefficient
Let's understand why we use these specific penalty forms:
Why the L1 penalty uses absolute values?
The absolute value function has a special property: its derivative is not continuous at zero. This creates a "corner" in the objective function that can drive coefficients exactly to zero, effectively performing automatic feature selection. When we take the derivative of , we get:
- when
- when
- is undefined at
This discontinuity is what allows LASSO to set coefficients exactly to zero.
Why the L2 penalty uses squared terms?
The squared penalty has a smooth, continuous derivative everywhere: . This smoothness helps with correlated features by encouraging similar coefficients for similar features, creating a "grouping effect." The penalty grows quadratically, so larger coefficients are penalized more heavily than smaller ones.
The Complete Elastic Net Objective Function
Combining all three components, our Elastic Net objective function becomes:
where:
- is the vector of coefficients we are optimizing
- is the sum of squared errors (data fit term)
- is the L1 penalty term (encourages sparsity)
- is the L2 penalty term (encourages small coefficients)
Let's break down what each part does:
- Data Fit Term : Ensures our predictions are close to the actual values
- L1 Penalty : Encourages sparsity by driving some coefficients to exactly zero
- L2 Penalty : Encourages small coefficients and handles correlated features well
The parameters and control the strength of each penalty:
- When and : We get pure Ridge regression
- When and : We get pure LASSO regression
- When and : We get Elastic Net
Understanding the Norm Notation
We can write the penalty terms more compactly using norm notation:
where:
- is the L1 norm (Manhattan distance)
- is the L2 norm (Euclidean distance)
- is the squared L2 norm
The L1 norm measures the sum of absolute values, while the L2 norm measures the square root of the sum of squares. In our formulation, we use the squared L2 norm () for mathematical convenience.
Why This Combination Works
Elastic Net's effectiveness comes from how these two penalties complement each other:
- The L1 penalty provides sparsity (feature selection) but can be unstable with correlated features
- The L2 penalty provides stability with correlated features but doesn't perform feature selection
- Together, they provide both sparsity and stability
This is particularly valuable when we have many features, some of which are correlated, and we want both interpretability through feature selection and stable coefficient estimates.
Geometric Interpretation of Regularization
To understand why Elastic Net combines the best of both worlds, let's visualize the constraint regions geometrically. In regularized regression, we can think of the problem as minimizing the sum of squared errors subject to a constraint on the coefficients.

This geometric view reveals several key insights:
-
L1 Constraint (Diamond): The sharp corners at the axes mean that the loss contours are likely to first touch the constraint region at a corner, resulting in sparse solutions where some coefficients are exactly zero.
-
L2 Constraint (Circle): The smooth, round shape means the loss contours will touch the constraint region at a point where all coefficients are typically non-zero but small. This provides stability but no sparsity.
-
Elastic Net Constraint (Rounded Diamond): By combining both penalties, we get a shape that has both corners (for sparsity) and smooth edges (for stability). This allows the model to achieve sparsity when needed while maintaining the grouping effect for correlated features.
The red contours represent levels of the loss function (sum of squared errors). The optimal solution for each method occurs where these contours first touch the respective constraint region. This geometric interpretation makes it clear why Elastic Net can achieve both feature selection and stability.it literally combines the geometric properties of both L1 and L2 constraints.
Matrix Notation
Now let's translate our objective function into matrix notation, which is more compact and reveals the mathematical structure more clearly.
First, let's define our matrices:
- is an vector containing all target values:
- is an design matrix where each row represents one observation and each column represents one feature (including the intercept column of ones)
- is a vector of coefficients:
where:
- is the number of observations
- is the number of features (excluding the intercept)
- The superscript denotes the transpose operation
The predicted values can now be written as:
where is the vector of predicted values.
This is much more compact than writing out each prediction individually. The sum of squared errors becomes:
where represents the L2 norm (Euclidean norm), which for a vector is defined as:
Therefore, is the squared Euclidean distance between the actual values and our predictions.
Similarly, the penalty terms become:
- L1 penalty:
- L2 penalty:
where the summation includes all coefficients from (intercept) to (last feature).
Putting it all together, our Elastic Net objective function in matrix notation is:
where:
- indicates we are finding the coefficient vector that minimizes the objective function
- The objective function is the sum of three terms: data fit (SSE), L1 penalty, and L2 penalty
This notation makes it clear that we're minimizing a function of the coefficient vector , and it's much more convenient for mathematical analysis and computational implementation.
Mathematical Properties
Understanding the mathematical properties of Elastic Net helps us predict how it will behave in different situations:
Sparsity and Grouping Effect: The L1 term encourages sparsity by driving some coefficients exactly to zero, while the L2 term encourages grouping of correlated features. This means that when features are highly correlated, Elastic Net tends to include the entire group rather than arbitrarily selecting one feature from the group (as LASSO might do).
Bias-Variance Tradeoff: As the regularization parameters and increase, the model becomes more biased (systematically wrong) but has lower variance (less sensitive to small changes in the data). This is the fundamental tradeoff in regularization.
Stability: The L2 component helps stabilize the solution when features are correlated, making the coefficient estimates more reliable and less sensitive to small changes in the data.
No Closed-Form Solution: Unlike Ridge regression, which has a closed-form solution, Elastic Net requires iterative optimization due to the non-differentiable L1 penalty. This makes it computationally more expensive but still more tractable than pure LASSO in many cases.
Convexity: The Elastic Net objective function is convex, which means it has a unique global minimum. This guarantees that our optimization algorithm will find the best solution.
Regularization Paths: Visualizing Coefficient Evolution
One of the most insightful ways to understand how Elastic Net behaves is to examine the regularization path.how coefficients change as we vary the regularization strength. This visualization reveals the key difference between LASSO and Elastic Net when dealing with correlated features.


These regularization paths reveal the fundamental difference between LASSO and Elastic Net:
LASSO Path (Left): The red and blue lines (features 0 and 1, which are highly correlated) follow different trajectories. LASSO arbitrarily selects feature 0 and drives feature 1 to zero early in the path. This instability makes it difficult to interpret which feature is more important when features are correlated.
Elastic Net Path (Right): The red and blue lines stay close together throughout the regularization path. Both correlated features enter and exit the model together. This behavior is particularly useful when you have domain knowledge that certain features should be considered together.
The regularization path also shows how to select the optimal regularization strength: you would typically use cross-validation to find the α value that minimizes prediction error on held-out data, then read off the corresponding coefficients from these paths.
Alternative Parameterization
In practice, Elastic Net is often parameterized using a mixing parameter and a total regularization strength . This alternative form makes it easier to understand the relationship between different regularization methods:
where:
- is the mixing parameter (controls the balance between L1 and L2 penalties)
- is the total regularization strength (controls how much regularization we apply overall)
The relationship between the two parameterizations is:
where:
- is the L1 penalty strength from the original formulation
- is the L2 penalty strength from the original formulation
This parameterization makes it much easier to understand the behavior:
- When : We get pure LASSO (L1 only) because the L2 term becomes zero
- When : We get pure Ridge (L2 only) because the L1 term becomes zero
- When : We get Elastic Net with a balance between L1 and L2 penalties
The factor of in the L2 term is included for mathematical convenience - it makes the derivatives cleaner and is a common convention in machine learning literature.
Scikit-learn Implementation
Scikit-learn uses a slightly different parameterization for computational efficiency:
where:
- is the number of samples (observations)
- is the total regularization strength (equivalent to in the alternative parameterization)
- is the
l1_ratioparameter (equivalent to in the alternative parameterization) - The factor normalizes the data fit term by the sample size
In scikit-learn:
alphaparameter controls the overall regularization strength ( in the formula above)l1_ratioparameter controls the mixing between L1 and L2 penalties ( in the formula above)- When
l1_ratio = 1(): Pure LASSO - When
l1_ratio = 0(): Pure Ridge - When
0 < l1_ratio < 1(): Elastic Net
Be careful with notation: scikit-learn's alpha parameter corresponds to in the mathematical literature, and scikit-learn's l1_ratio corresponds to the mixing parameter often denoted in textbooks. Check the documentation for the specific implementation you're using to avoid confusion.
Mathematical Properties
- Sparsity and Grouping: The L1 term encourages sparsity (some coefficients exactly zero), while the L2 term encourages grouping of correlated features
- Bias-Variance Tradeoff: As increases, bias increases but variance decreases
- Stability: The L2 term helps stabilize the solution when features are correlated
- No Closed-Form Solution: Like LASSO, Elastic Net requires iterative optimization due to the L1 penalty