7 · Modelling & Training · Regularisation & Early Stopping

Elastic Net

Combines L1 and L2 to balance sparsity with stability among correlated features. The important practical question is not only how the technique is defined, but what assumptions it introduces, which data are allowed to influence it, and how its effect should be validated on unseen evidence.

Reference lessonPython exampleVisual explanation
Intuition first

What this concept means in practice

Combines L1 and L2 to balance sparsity with stability among correlated features. The important practical question is not only how the technique is defined, but what assumptions it introduces, which data are allowed to influence it, and how its effect should be validated on unseen evidence.

The practical value of Elastic Net comes from understanding both the transformation and the boundary around it: what information is allowed to enter, what assumption is being made, and how you know the result is still valid after the transformation.

A beginner-friendly way to reason about it is to start with a tiny case where the correct result can be checked independently. Once the mechanism is clear, scale the exact same reasoning to larger tables, pipelines or models.

PurposeUse for high-dimensional linear problems with correlated features.
MechanismCombine L1 and L2 penalties to obtain sparsity plus more stable behaviour among correlated features.
EvidenceInspect intermediate and final output; compare with an independent expectation.
Main cautionBoth overall strength and L1/L2 mixture require tuning.
Mechanism

Trace the operation from input to decision

Combine L1 and L2 penalties to obtain sparsity plus more stable behaviour among correlated features.

1Input→
2Apply rule→
3Inspect state→
4Validate→
5Use result
Key rule
λ[α|w|₁+(1-α)|w|²₂]
Visual explanation

Make the structure visible

The interactive view uses a concept-specific plot when the topic maps naturally to one; otherwise it uses a workflow view instead of leaving a broken placeholder.

Loading visual…
Practical example

Where you would use it

Genomic regression with thousands of correlated predictors can use Elastic Net rather than pure Lasso.

Use when
Use for high-dimensional linear problems with correlated features.
Pitfall

What can make the result misleading

Watch out
Both overall strength and L1/L2 mixture require tuning.

A useful diagnostic question is: Could the same code still run successfully if the analytical assumption were wrong? If yes, add an explicit validation check rather than relying on execution success.

Implementation

Miniature Python example

Keep the example small enough that you can inspect each stage manually.

Python
# Step 1 — Import the module so its functions/classes are available to the rest of this example.
import numpy as np
# Step 2 — Import only the named objects needed by the following steps, keeping dependencies explicit.
from sklearn.linear_model import ElasticNet

# Step 3 — Construct `X` as an array so vectorised numerical operations can be applied consistently.
X = np.array([[1,0],[2,1],[3,0],[4,1],[5,0],[6,1]], dtype=float)
# Step 4 — Construct `y` as an array so vectorised numerical operations can be applied consistently.
y = np.array([2.0,4.5,5.7,8.4,9.8,12.3])
# Step 5 — Display the current value explicitly so the result/state can be inspected during execution.
print("STEP 1 · feature shape:", X.shape)
# Step 6 — Fit the model or transformer, learning its parameters from the supplied training data.
model = ElasticNet(alpha=0.12, l1_ratio=0.5, max_iter=10000).fit(X, y)
# Step 7 — Display the current value explicitly so the result/state can be inspected during execution.
print("STEP 2 · coefficients:", model.coef_.round(3).tolist())
# Step 8 — Display the current value explicitly so the result/state can be inspected during execution.
print("STEP 3 · prediction:", round(model.predict([[7,1]])[0], 2))
Expected / illustrative output
STEP 1 · feature shape: (6, 2)
STEP 2 · coefficients: [1.914, 0.332]
STEP 3 · prediction: 13.98
Implementation checklist

Before you move on

  • Can you state what data or object enters the operation?
  • Can you explain what changes and what must remain invariant?
  • Have you checked the result on a tiny case you can verify independently?
  • Have you considered the main failure mode described above?
  • Can the operation be reproduced from code/formulas and documented assumptions?