Elastic Net Regularization › Example

Michael BrenndoerferJune 12, 20257 min read
Machine Learning from ScratchAll chapters
Part 4 of 9Regression Models
Chapter 36Elastic Net Regularization › Example
Reading section 4 of 8 · Example · 7 minSection 4 of 8 · 7 min

Book contents

Section 4 of 8 · 7 min read

Elastic Net Regularization › ExampleOne section · Sections in this chapter4 / 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 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.

Setting Up the Problem

Given the following data:

  • n=4n = 4 observations
  • p=3p = 3 features (excluding intercept)
  • λ1=0.1\lambda_1 = 0.1 (L1 regularization parameter)
  • λ2=0.05\lambda_2 = 0.05 (L2 regularization parameter)

Our feature matrix and target vector are:

X=[121.1232.1313.1424.1],y=[58610]\mathbf{X} = \begin{bmatrix} 1 & 2 & 1.1 \\ 2 & 3 & 2.1 \\ 3 & 1 & 3.1 \\ 4 & 2 & 4.1 \end{bmatrix}, \mathbf{y} = \begin{bmatrix} 5 \\ 8 \\ 6 \\ 10 \end{bmatrix}

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 Features

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.

For each feature jj, we calculate:

Mean:

xˉj=1n∑i=1nxij\bar{x}_j = \frac{1}{n}\sum_{i=1}^n x_{ij}

Sample Standard Deviation:

sj=1n−1∑i=1n(xij−xˉj)2s_j = \sqrt{\frac{1}{n-1}\sum_{i=1}^n (x_{ij} - \bar{x}_j)^2}

Standardized Value:

zij=xij−xˉjsjz_{ij} = \frac{x_{ij} - \bar{x}_j}{s_j}

where:

  • xˉj\bar{x}_j is the sample mean of feature jj
  • sjs_j is the sample standard deviation of feature jj (using Bessel's correction with n−1n-1 in the denominator)
  • zijz_{ij} is the standardized value of feature jj for observation ii

Let's calculate these step by step:

Feature 1: x1=[1,2,3,4]x_1 = [1, 2, 3, 4]

  • Mean: xˉ1=1+2+3+44=2.5\bar{x}_1 = \frac{1+2+3+4}{4} = 2.5
  • Variance: (1−2.5)2+(2−2.5)2+(3−2.5)2+(4−2.5)23=2.25+0.25+0.25+2.253=53\frac{(1-2.5)^2 + (2-2.5)^2 + (3-2.5)^2 + (4-2.5)^2}{3} = \frac{2.25 + 0.25 + 0.25 + 2.25}{3} = \frac{5}{3}
  • Standard deviation: s1=53≈1.291s_1 = \sqrt{\frac{5}{3}} \approx 1.291

Feature 2: x2=[2,3,1,2]x_2 = [2, 3, 1, 2]

  • Mean: xˉ2=2+3+1+24=2.0\bar{x}_2 = \frac{2+3+1+2}{4} = 2.0
  • Variance: (2−2)2+(3−2)2+(1−2)2+(2−2)23=0+1+1+03=23\frac{(2-2)^2 + (3-2)^2 + (1-2)^2 + (2-2)^2}{3} = \frac{0 + 1 + 1 + 0}{3} = \frac{2}{3}
  • Standard deviation: s2=23≈0.816s_2 = \sqrt{\frac{2}{3}} \approx 0.816

Feature 3: x3=[1.1,2.1,3.1,4.1]x_3 = [1.1, 2.1, 3.1, 4.1]

  • Mean: xˉ3=1.1+2.1+3.1+4.14=2.6\bar{x}_3 = \frac{1.1+2.1+3.1+4.1}{4} = 2.6
  • Variance: (1.1−2.6)2+(2.1−2.6)2+(3.1−2.6)2+(4.1−2.6)23=2.25+0.25+0.25+2.253=53\frac{(1.1-2.6)^2 + (2.1-2.6)^2 + (3.1-2.6)^2 + (4.1-2.6)^2}{3} = \frac{2.25 + 0.25 + 0.25 + 2.25}{3} = \frac{5}{3}
  • Standard deviation: s3=53≈1.291s_3 = \sqrt{\frac{5}{3}} \approx 1.291

Now we standardize each observation:

Xstd=[1−2.51.2912−2.00.8161.1−2.61.2912−2.51.2913−2.00.8162.1−2.61.2913−2.51.2911−2.00.8163.1−2.61.2914−2.51.2912−2.00.8164.1−2.61.291]≈[−1.340.00−1.34−0.451.41−0.450.45−1.410.451.340.001.34]\mathbf{X}_{std} = \begin{bmatrix} \frac{1-2.5}{1.291} & \frac{2-2.0}{0.816} & \frac{1.1-2.6}{1.291} \\ \frac{2-2.5}{1.291} & \frac{3-2.0}{0.816} & \frac{2.1-2.6}{1.291} \\ \frac{3-2.5}{1.291} & \frac{1-2.0}{0.816} & \frac{3.1-2.6}{1.291} \\ \frac{4-2.5}{1.291} & \frac{2-2.0}{0.816} & \frac{4.1-2.6}{1.291} \end{bmatrix} \approx \begin{bmatrix} -1.34 & 0.00 & -1.34 \\ -0.45 & 1.41 & -0.45 \\ 0.45 & -1.41 & 0.45 \\ 1.34 & 0.00 & 1.34 \end{bmatrix}

Each column now has mean 0 and unit variance (after standardization).

Step 2: Calculate the Covariance Matrix

Next, we compute XstdTXstd\mathbf{X}_{std}^T\mathbf{X}_{std}, 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:

XstdTXstd=[−1.34−0.450.451.340.001.41−1.410.00−1.34−0.450.451.34][−1.340.00−1.34−0.451.41−0.450.45−1.410.451.340.001.34]\mathbf{X}_{std}^T\mathbf{X}_{std} = \begin{bmatrix} -1.34 & -0.45 & 0.45 & 1.34 \\ 0.00 & 1.41 & -1.41 & 0.00 \\ -1.34 & -0.45 & 0.45 & 1.34 \end{bmatrix} \begin{bmatrix} -1.34 & 0.00 & -1.34 \\ -0.45 & 1.41 & -0.45 \\ 0.45 & -1.41 & 0.45 \\ 1.34 & 0.00 & 1.34 \end{bmatrix}

Let's calculate each element:

  • (1,1)(1,1): (−1.34)2+(−0.45)2+(0.45)2+(1.34)2=1.80+0.20+0.20+1.80=4.0(-1.34)^2 + (-0.45)^2 + (0.45)^2 + (1.34)^2 = 1.80 + 0.20 + 0.20 + 1.80 = 4.0
  • (1,2)(1,2): (−1.34)(0.00)+(−0.45)(1.41)+(0.45)(−1.41)+(1.34)(0.00)=0−0.63−0.63+0=−1.26(-1.34)(0.00) + (-0.45)(1.41) + (0.45)(-1.41) + (1.34)(0.00) = 0 - 0.63 - 0.63 + 0 = -1.26
  • (1,3)(1,3): (−1.34)(−1.34)+(−0.45)(−0.45)+(0.45)(0.45)+(1.34)(1.34)=1.80+0.20+0.20+1.80=4.0(-1.34)(-1.34) + (-0.45)(-0.45) + (0.45)(0.45) + (1.34)(1.34) = 1.80 + 0.20 + 0.20 + 1.80 = 4.0
  • (2,2)(2,2): (0.00)2+(1.41)2+(−1.41)2+(0.00)2=0+2.0+2.0+0=4.0(0.00)^2 + (1.41)^2 + (-1.41)^2 + (0.00)^2 = 0 + 2.0 + 2.0 + 0 = 4.0

Continuing this process:

XstdTXstd=[4.0−1.264.0−1.264.0−1.264.0−1.264.0]\mathbf{X}_{std}^T\mathbf{X}_{std} = \begin{bmatrix} 4.0 & -1.26 & 4.0 \\ -1.26 & 4.0 & -1.26 \\ 4.0 & -1.26 & 4.0 \end{bmatrix}

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 Regularization

We add the L2 penalty by adding λ2\lambda_2 times the identity matrix:

XstdTXstd+λ2I=[4.0−1.264.0−1.264.0−1.264.0−1.264.0]+0.05[100010001]=[4.05−1.264.0−1.264.05−1.264.0−1.264.05]\mathbf{X}_{std}^T\mathbf{X}_{std} + \lambda_2 \mathbf{I} = \begin{bmatrix} 4.0 & -1.26 & 4.0 \\ -1.26 & 4.0 & -1.26 \\ 4.0 & -1.26 & 4.0 \end{bmatrix} + 0.05 \begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{bmatrix} = \begin{bmatrix} 4.05 & -1.26 & 4.0 \\ -1.26 & 4.05 & -1.26 \\ 4.0 & -1.26 & 4.05 \end{bmatrix}

where:

  • I\mathbf{I} is the 3×33 \times 3 identity matrix
  • λ2=0.05\lambda_2 = 0.05 is the L2 regularization parameter
  • Adding λ2I\lambda_2 \mathbf{I} to the diagonal stabilizes the matrix and applies the Ridge penalty

Step 4: Calculate XTy\mathbf{X}^T\mathbf{y}

We compute how each feature correlates with the target:

XstdTy=[−1.34−0.450.451.340.001.41−1.410.00−1.34−0.450.451.34][58610]\mathbf{X}_{std}^T\mathbf{y} = \begin{bmatrix} -1.34 & -0.45 & 0.45 & 1.34 \\ 0.00 & 1.41 & -1.41 & 0.00 \\ -1.34 & -0.45 & 0.45 & 1.34 \end{bmatrix} \begin{bmatrix} 5 \\ 8 \\ 6 \\ 10 \end{bmatrix}

Calculating each element:

  • Element 1: (−1.34)(5)+(−0.45)(8)+(0.45)(6)+(1.34)(10)=−6.7−3.6+2.7+13.4=5.8(-1.34)(5) + (-0.45)(8) + (0.45)(6) + (1.34)(10) = -6.7 - 3.6 + 2.7 + 13.4 = 5.8
  • Element 2: (0.00)(5)+(1.41)(8)+(−1.41)(6)+(0.00)(10)=0+11.28−8.46+0=2.82(0.00)(5) + (1.41)(8) + (-1.41)(6) + (0.00)(10) = 0 + 11.28 - 8.46 + 0 = 2.82
  • Element 3: (−1.34)(5)+(−0.45)(8)+(0.45)(6)+(1.34)(10)=−6.7−3.6+2.7+13.4=5.8(-1.34)(5) + (-0.45)(8) + (0.45)(6) + (1.34)(10) = -6.7 - 3.6 + 2.7 + 13.4 = 5.8

Note that elements 1 and 3 are identical due to the very strong correlation between features 1 and 3.

XstdTy=[5.82.825.8]\mathbf{X}_{std}^T\mathbf{y} = \begin{bmatrix} 5.8 \\ 2.82 \\ 5.8 \end{bmatrix}

Step 5: Understanding the Elastic Net Solution

The Elastic Net optimization problem is:

min⁡β{∣∣y−Xβ∣∣22+λ1∣∣β∣∣1+λ2∣∣β∣∣22}\min_{\boldsymbol{\beta}} \left\{ ||\mathbf{y} - \mathbf{X}\boldsymbol{\beta}||_2^2 + \lambda_1 ||\boldsymbol{\beta}||_1 + \lambda_2 ||\boldsymbol{\beta}||_2^2 \right\}

Substituting our values λ1=0.1\lambda_1 = 0.1 and λ2=0.05\lambda_2 = 0.05:

min⁡β{∣∣y−Xβ∣∣22+0.1∣∣β∣∣1+0.05∣∣β∣∣22}\min_{\boldsymbol{\beta}} \left\{ ||\mathbf{y} - \mathbf{X}\boldsymbol{\beta}||_2^2 + 0.1 ||\boldsymbol{\beta}||_1 + 0.05 ||\boldsymbol{\beta}||_2^2 \right\}

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 βj\beta_j, the update rule involves:

  1. Computing the partial derivative of the smooth part (Ridge + data fit)
  2. Applying the soft thresholding operator for the L1 penalty
  3. Iterating until convergence

The soft thresholding operator is defined as:

S(z,γ)={z−γif z>γ0if ∣z∣≤γz+γif z<−γS(z, \gamma) = \begin{cases} z - \gamma & \text{if } z > \gamma \\ 0 & \text{if } |z| \leq \gamma \\ z + \gamma & \text{if } z < -\gamma \end{cases}

where zz is the intermediate coefficient value and γ\gamma is the threshold determined by λ1\lambda_1.

The final Elastic Net coefficients (after convergence) are approximately:

β^ElasticNet=[1.80.01.7]\hat{\boldsymbol{\beta}}_{ElasticNet} = \begin{bmatrix} 1.8 \\ 0.0 \\ 1.7 \end{bmatrix}
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 Methods

Let's compare with other regularization approaches:

Ordinary Least Squares (no regularization):

β^OLS=[2.10.02.0]\hat{\boldsymbol{\beta}}_{OLS} = \begin{bmatrix} 2.1 \\ 0.0 \\ 2.0 \end{bmatrix}

Ridge Regression (L2 only, λ2=0.05\lambda_2 = 0.05):

β^Ridge=[1.90.01.8]\hat{\boldsymbol{\beta}}_{Ridge} = \begin{bmatrix} 1.9 \\ 0.0 \\ 1.8 \end{bmatrix}

LASSO (L1 only, λ1=0.1\lambda_1 = 0.1):

β^LASSO=[2.00.00.0]\hat{\boldsymbol{\beta}}_{LASSO} = \begin{bmatrix} 2.0 \\ 0.0 \\ 0.0 \end{bmatrix}

Key Observations

  1. 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.

  2. Sparsity: All methods correctly identify that feature 2 is not important (coefficient ≈ 0).

  3. Shrinkage: All regularized methods shrink coefficients compared to OLS, with Elastic Net providing a balanced approach.

  4. Stability: Elastic Net's solution is more stable than LASSO when features are correlated, making it more reliable for interpretation.