Reading section 4 of 8 · Example · 7 minSection 4 of 8 · 7 min
Section 4 of 8 · 7 min read
Elastic Net Regularization › ExampleOne section · Sections in this chapter4 / 8 ↓
Sections in this chapter8
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 work through a detailed mathematical example to understand how Elastic Net works step by step. We'll use a small dataset with correlated features to demonstrate the grouping effect and show the complete calculation process.
Notice that features 1 and 3 are highly correlated (feature 3 ≈ feature 1 + 0.1). This correlation demonstrates how Elastic Net's grouping effect differs from LASSO's behavior.
Step 1: Standardize the FeaturesLink Copied
First, we standardize the features. This is important for regularized regression because it ensures that all features are on the same scale, so the regularization penalty is applied fairly. Without standardization, features with larger scales would be penalized more heavily, distorting our results.
is the sample standard deviation of feature (using Bessel's correction with in the denominator)
is the standardized value of feature for observation
Let's calculate these step by step:
Feature 1:
Mean:
Variance:
Standard deviation:
Feature 2:
Mean:
Variance:
Standard deviation:
Feature 3:
Mean:
Variance:
Standard deviation:
Now we standardize each observation:
Each column now has mean 0 and unit variance (after standardization).
Step 2: Calculate the Covariance MatrixLink Copied
Next, we compute , which captures the relationships between features after standardization.
For standardized features, this matrix is proportional to the correlation matrix and reveals how features are related:
Let's calculate each element:
:
:
:
:
Continuing this process:
Notice that features 1 and 3 are highly correlated (off-diagonal value 4.0, which equals the diagonal), indicating very strong correlation. Feature 2 has negative correlation with both features 1 and 3.
Step 3: Add L2 RegularizationLink Copied
We add the L2 penalty by adding times the identity matrix:
Adding to the diagonal stabilizes the matrix and applies the Ridge penalty
Step 4: Calculate Link Copied
We compute how each feature correlates with the target:
Calculating each element:
Element 1:
Element 2:
Element 3:
Note that elements 1 and 3 are identical due to the very strong correlation between features 1 and 3.
Step 5: Understanding the Elastic Net SolutionLink Copied
The Elastic Net optimization problem is:
Substituting our values and :
This requires iterative optimization due to the non-differentiable L1 penalty. The solution process involves coordinate descent, where we update one coefficient at a time while holding others fixed.
For each coefficient, the update rule involves:
Computing the partial derivative of the smooth part (Ridge + data fit)
Applying the soft thresholding operator for the L1 penalty
Iterating until convergence
The soft thresholding operator is defined as:
where is the intermediate coefficient value and is the threshold determined by .
The final Elastic Net coefficients (after convergence) are approximately:
Note: Approximate Solution
The Elastic Net coefficients shown above are approximate values obtained through iterative optimization. The exact values depend on the convergence criteria and optimization algorithm used. In practice, we would use computational tools like scikit-learn to obtain precise solutions.
Comparison with Other MethodsLink Copied
Let's compare with other regularization approaches:
Grouping Effect: Elastic Net keeps both correlated features 1 and 3 with similar coefficients (1.8 and 1.7), while LASSO arbitrarily selects only feature 1.
Sparsity: All methods correctly identify that feature 2 is not important (coefficient ≈ 0).
Shrinkage: All regularized methods shrink coefficients compared to OLS, with Elastic Net providing a balanced approach.
Stability: Elastic Net's solution is more stable than LASSO when features are correlated, making it more reliable for interpretation.