Bring the mental model from Linear Regression & Least Squares; this page will reuse it instead of restarting from zero.
Machine Learning
Regularization: Ridge, Lasso, and Elastic Net
Ridge, lasso, and elastic net add shape to coefficient space so models trade training fit for shrinkage, sparsity, and more stable validation behavior.
Concept Structure
Regularization: Ridge, Lasso, and Elastic Net
Start with the picture, metaphor, or geometric mechanism.
Make the objects explicit and connect them with notation.
Mirror the equations with runnable implementation details.
Manipulate the mechanism and watch the idea respond.
Learning map
Regularization: Ridge, Lasso, and Elastic NetConceptual Bridge
What should feel connected as you move through this page.
Ridge, lasso, and elastic net add shape to coefficient space so models trade training fit for shrinkage, sparsity, and more stable validation behavior.
The next edge should feel earned: use the demo prediction here before following Model Selection and Hyperparameter Search.
01
Intuition
Build the mental picture first so the rest of the page has something to attach to.
You are here because a model with many features can explain the training data in too many ways. Regularization asks each explanation to pay rent: if a coefficient is large, the model must justify why that size is worth the extra complexity.
Before this, know least squares, norms, and why train/dev/test separation matters. By the end, you should be able to predict how ridge, lasso, and elastic net change coefficients, why feature scaling matters, and why the penalty strength belongs to validation or cross-validation, not the final test set.
Start with least squares. It only asks, "Did the predictions match the training targets?" If two features are correlated, many coefficient pairs can make similar predictions. One fit might put most of the weight on feature 1, another on feature 2, and both can look equally good on the training set.
Regularization adds a second question:
How expensive is this coefficient vector?
Ridge regression uses an cost. It dislikes large squared coefficients, so it pulls weights smoothly toward zero. It usually keeps correlated features sharing the load rather than choosing one winner.
Lasso uses an cost. Its geometry has sharp corners on the coordinate axes. Those corners make exact zeros natural, so lasso can perform feature selection: some coefficients are not merely small; they disappear.
Elastic net mixes the two. It keeps the lasso's ability to create zeros, but adds enough ridge-like smoothing that groups of correlated features behave less erratically.
Three caveats matter:
- Scale matters. Penalizing a coefficient is unfair if one feature is measured in dollars and another in thousands of dollars. Standardize features before comparing penalties, but fit that scaler on the training fold only and apply the same transformation to dev or test rows.
- Sparsity is not truth. A zero lasso coefficient means "not chosen under this data, scaling, penalty, and correlation structure," not "causally irrelevant."
- Test data stays sealed. Choose and the ridge/lasso mix using dev data or cross-validation, then evaluate the selected pipeline once on test.
02
Math
Translate the story into symbols, assumptions, and a derivation you can inspect.
Let be a standardized design matrix, let be centered targets, and let be the coefficients. We handle the intercept separately and do not penalize it. The unregularized least-squares loss is
Ridge regression adds a squared penalty:
The gradient is
If is invertible, the ridge solution is
The constants differ across books depending on whether the squared error is summed or averaged, but the invariant is the same: adding a positive multiple of the identity makes large coefficient directions more expensive and stabilizes nearly singular directions.
Lasso replaces squared length with length:
The norm is not differentiable at . Its subgradient is
That interval at zero is the algebraic reason exact zeros can be optimal. In the special case where the empirical Gram matrix satisfies , define . The lasso solution is soft-thresholding:
Small coordinates vanish. Large coordinates are shifted toward zero by a fixed amount.
Elastic net blends the two penalties with a mixing parameter :
When , this is ridge. When , this is lasso. Between them, elastic net can keep sparsity while reducing some of the instability lasso has when predictors are strongly correlated. Exact zero coefficients come from the part, so the mix needs for lasso-style zeros.
The penalty strength is a hyperparameter. A clean workflow fits candidate pipelines on training folds, chooses by development loss or cross-validation, optionally refits the selected pipeline by a pre-declared rule, and tests once.
03
Code
Keep the implementation aligned with the notation so the algorithm is legible.
import numpy as np
rng = np.random.default_rng(7)
n = 120
x1 = rng.normal(size=n)
x2 = 0.92 * x1 + np.sqrt(1 - 0.92**2) * rng.normal(size=n)
x3 = rng.normal(size=n) # noise feature
X_raw = np.column_stack([x1, x2, x3])
y_raw = 2.0 * x1 + rng.normal(scale=0.7, size=n)
train, dev = np.arange(80), np.arange(80, n)
x_mean = X_raw[train].mean(axis=0)
x_std = X_raw[train].std(axis=0)
X = (X_raw - x_mean) / x_std
y_mean = y_raw[train].mean()
y = y_raw - y_mean
def soft_threshold(z, amount):
return np.sign(z) * max(abs(z) - amount, 0.0)
def fit_elastic_net(alpha, lam, steps=700):
Xtr, ytr = X[train], y[train]
w = np.zeros(X.shape[1])
for _ in range(steps):
for j in range(X.shape[1]):
residual = ytr - Xtr @ w + Xtr[:, j] * w[j]
rho = np.mean(Xtr[:, j] * residual)
denom = np.mean(Xtr[:, j] ** 2) + lam * (1 - alpha)
w[j] = soft_threshold(rho, lam * alpha) / denom
return w
for name, alpha in [("ridge", 0.0), ("elastic", 0.5), ("lasso", 1.0)]:
w = fit_elastic_net(alpha=alpha, lam=0.35)
mse = np.mean((X[dev] @ w - y[dev]) ** 2)
print(name, "coef=", np.round(w, 3), "dev MSE=", round(mse, 3))
The code mirrors the math. alpha=0 uses a ridge-style denominator, alpha=1 uses pure soft-thresholding, and alpha=0.5 blends them. Because x1 and x2 are highly correlated, lasso may choose one correlated twin while ridge tends to spread weight across both.
The scaler and target centering are fit on training rows only; dev rows are transformed with those same statistics.
04
Interactive Demo
Use direct manipulation to connect the explanation to a moving system.
Choose the penalty strength, the mix, and how correlated the two signal features are. Before revealing the coefficients, predict the dominant visible behavior: will the model spread weight across the correlated pair, drop one correlated twin, or mostly shrink coefficient magnitudes?
The bars show fitted coefficients for two correlated signal features and one noise feature. The correlation slider controls a population construction target, so the finite sample correlation will be close but not exact. The dev-set readout is there to keep the workflow honest: and are modeling choices, so they belong to validation or cross-validation rather than the final test set.
Live Concept Demo
Explore Regularization: Ridge, Lasso, and Elastic Net
The stage is code-native and interactive. Use it to test the explanation against the mechanism.
Manipulate one control and predict the visible change.
Commit to what Regularization: Ridge, Lasso, and Elastic Net should make visible before reading the result.
After The First Pass
Turn the concept into an inspected object.
Once the invariant is visible in the intuition, math, code, and demo, use these panels to inspect the mechanism visually, check source support, practice the idea, and attach a grounded research question.
Mechanism Storyboard
See the idea move before the page explains it
Ridge, lasso, and elastic net add shape to coefficient space so models trade training fit for shrinkage, sparsity, and more stable validation behavior.
Start with the picture, metaphor, or geometric mechanism.
Before reading further, choose the kind of change Regularization: Ridge, Lasso, and Elastic Net should make visible.
Visual Inquiry
Make the image answer a mathematical question
Ridge, lasso, and elastic net add shape to coefficient space so models trade training fit for shrinkage, sparsity, and more stable validation behavior.
Which visible object should carry the first intuition?
Pick the cue that should make Regularization: Ridge, Lasso, and Elastic Net easier to reason about before the page gives the answer.
Source Grounding
Canonical references for the mechanism on this page.
Source for ridge/lasso objectives, shrinkage, sparsity, scaling caveats, and cross-validated lambda selection.
Open sourceSource for regularization as a model-selection/generalization tool and cross-validation of regularized choices.
Open sourceSource for parameter-norm penalties, L2 weight decay, L1 sparsity, and bias-variance regularization framing.
Open sourceSource for the elastic-net objective, sparsity plus shrinkage framing, and correlated-predictor grouping behavior.
Open sourceClaim Review
Ridge, lasso, and elastic net add shape to coefficient space so models trade training fit for shrinkage, sparsity, and more stable validation behavior.
Claims without a substantive review badge still need exact source-support review.
islr-ridge-lasso, cs229-regularization-model-selection, goodfellow-regularization, zou-hastie-elastic-net
Use equation, code, and demo objects to check whether the source support is operational.
ISLR directly supports ridge/lasso objectives, scaling caveats, L1 sparsity, and CV lambda selection; CS229 supports cross-validation/model-selection framing; Goodfellow supports parameter-norm penalties and L1/L2 regularization behavior; Zou and Hastie support the elastic-net objective and grouping behavior under correlated predictors.
Sources: An Introduction to Statistical Learning, CS229 Notes: Regularization and Model Selection, Deep Learning: Regularization for Deep Learning, Regularization and Variable Selection via the Elastic NetThis claim covers linear-model penalty geometry and toy coordinate-descent witnesses; it does not claim lasso always recovers true causal features, that ridge/lasso universally dominate each other, or that neural weight decay is identical to AdamW.A bounded review summary is present; still check caveats and exact source scope.Substantively reviewed after train-only preprocessing, demo classifier, correlation-generation, and elastic-net source fixes. Sources support ridge/lasso/elastic-net objectives, L1 sparsity, scaling-within-training-fold caveat, and validation/CV lambda selection. Caveats: toy linear-model witness only; no causal feature-selection guarantee; no claim of coordinate-wise monotone ridge shrinkage or AdamW equivalence.
Reviewer: gpt-pro; reviewed 2026-06-28Source support candidates
book 2023An Introduction to Statistical LearningSource for ridge/lasso objectives, shrinkage, sparsity, scaling caveats, and cross-validated lambda selection.
course-notes 2019CS229 Notes: Regularization and Model SelectionSource for regularization as a model-selection/generalization tool and cross-validation of regularized choices.
book 2016Deep Learning: Regularization for Deep LearningSource for parameter-norm penalties, L2 weight decay, L1 sparsity, and bias-variance regularization framing.
paper 2005Regularization and Variable Selection via the Elastic NetSource for the elastic-net objective, sparsity plus shrinkage framing, and correlated-predictor grouping behavior.
Practice Loop
Try the idea before it explains itself
Ridge, lasso, and elastic net add shape to coefficient space so models trade training fit for shrinkage, sparsity, and more stable validation behavior.
Before touching the demo, predict one visible change that should happen in Regularization: Ridge, Lasso, and Elastic Net.
Reveal when your model needs a nudge.
Reveal when your model needs a nudge.
Reveal when your model needs a nudge.
A concrete answer is on the canvas.
The answer names why the claim should hold.
It touches the page context or a neighboring idea.
Research Room
Attach the question to an exact object
Pick the concept, equation, source, code witness, claim, misconception, or demo state before asking for help. The handoff stays grounded to that object.Open the draft below to save one note and next action in this browser.
Regularization: Ridge, Lasso, and Elastic Net
What is the smallest example that makes Regularization: Ridge, Lasso, and Elastic Net click without losing the math?
Local action draftNo local draft saved yetExpand only when ready to capture one local next action
This draft stays locally in this browser for concept:machine-learning/regularization-ridge-lasso-elastic-net.
- Source ids to inspect: islr-ridge-lasso, cs229-regularization-model-selection, goodfellow-regularization, zou-hastie-elastic-net
- Definition, prerequisite, and contrast concept links
- The equation or code witness that makes the concept operational
- One demo state that shows the invariant instead of a slogan
- The learner can state the mechanism in their own words
- The learner can name the prerequisite that would repair confusion
- The learner can predict how the mechanism changes under one perturbation
I am working in Continuous Function's research reading room. Object: concept - Regularization: Ridge, Lasso, and Elastic Net Object key: concept:machine-learning/regularization-ridge-lasso-elastic-net Context: Machine Learning Anchor id: concept/concept-notebook/machine-learning/regularization-ridge-lasso-elastic-net Open question: What is the smallest example that makes Regularization: Ridge, Lasso, and Elastic Net click without losing the math? Evidence to inspect: - Source ids to inspect: islr-ridge-lasso, cs229-regularization-model-selection, goodfellow-regularization, zou-hastie-elastic-net - Definition, prerequisite, and contrast concept links - The equation or code witness that makes the concept operational - One demo state that shows the invariant instead of a slogan What would resolve this: - The learner can state the mechanism in their own words - The learner can name the prerequisite that would repair confusion - The learner can predict how the mechanism changes under one perturbation Answer as a careful research tutor: stay source-grounded, separate verified evidence from assumptions, name the relevant math objects, and end with one next action.
concept/concept-notebook/machine-learning/regularization-ridge-lasso-elastic-net
concept:machine-learning/regularization-ridge-lasso-elastic-net