Skip to main content
LearnMLAcademy
Intermediate10 min read

Ridge Regression

Constrain regression coefficients using Ridge L2 regularization penalties, with Python code, architecture diagrams, worked examples, and interview quiz.

What you will master in this lesson
Intuitive core mechanisms & practical real-world intuition
Step-by-step mathematical breakdown & visual diagrams
Runnable Python implementation & practical trade-offs

Ridge Regression

Ridge Regression is Linear Regression with an L2 penalty. It discourages very large coefficients, which can reduce variance and make the model more stable when predictors are strongly correlated.

Ridge Regression in Simple Words

Linear Regression tries to fit the data well. Ridge Regression asks it to do one extra thing: fit the data without relying on unnecessarily large coefficients.

1

Start with Linear Regression

Learn a line or plane from the data.

2

Look at the coefficients

Very large coefficients can make predictions sensitive.

3

Add an L2 penalty

Large coefficients make the cost larger.

4

Shrink the coefficients

The model balances fit and simplicity.

A tiny house-price example

FeatureOrdinary Linear coefficientAfter Ridge
House area12.08.5
Distance from city-9.0-5.8

These numbers are only an illustration. Ridge does not force every coefficient to be small; it finds a balance between prediction error and coefficient size.

What is Ridge Regression?

Ridge Regression is a modified version of Linear Regression that adds a penalty term to the cost function to reduce model complexity. The goal is to prevent overfitting, reduce excessively large coefficients, improve generalization, and handle multicollinearity.

Simple Beginner Explanation

Imagine a student memorizing answers instead of understanding concepts.

  • Memorization = Overfitting: The student scores exactly 100% on the practice test (training data) because they memorized the exact questions. But when given a real exam with slightly different questions (unseen data), they fail completely.
  • Understanding = Ridge Regression: A teacher adds a rule: "You lose points if you just memorize word-for-word." Now the student is forced to understand the general concepts. They might score 90% on the practice test, but they will also score 90% on the real exam.

Ridge Regression can reduce overfitting by penalizing large coefficients. It does not penalize every kind of model complexity; specifically, it adds an L2 penalty to the coefficients.

Why Do We Need Ridge Regression?

1. To Prevent Overfitting

Ordinary Least Squares minimizes squared prediction error. With noisy data, many predictors, or strongly correlated predictors, the fitted coefficients can become unnecessarily sensitive. Ridge can reduce that variance.

2. To Handle Multicollinearity

When predictors are strongly correlated (for example, house area and number of rooms), ordinary least-squares coefficients can become unstable. Ridge improves the conditioning of the problem and often stabilizes those coefficients.

Core Idea of Ridge Regression

In ordinary Linear Regression, we minimize a squared-error data-fit term. Ridge Regression adds an L2 penalty based on the squared coefficients. For intuition, we can write it as follows:

Linear Regression data-fit term:

Data-fit loss = squared prediction error

Ridge Regression objective (simple form):

Cost = Data-fit loss + λ × Σ(w²)

Different books and software may scale the squared-error term slightly differently, but the idea is the same: larger λ gives stronger L2 regularization. The intercept is usually not treated like an ordinary penalized feature coefficient.

The Role of Lambda (λ or Alpha)

λ = 0

The penalty disappears completely. The model turns into standard Linear Regression.

λ = Useful Value

A suitable value balances data fit and coefficient shrinkage. It should be chosen using validation or cross-validation rather than guessed.

λ = Very High

The penalty is too aggressive. The model forces all weights near zero, resulting in a flat line (Underfitting).

Bias-Variance Tradeoff based on Lambda (λ)

Illustrative pattern only: as regularization grows, training error often rises while validation error may first improve and later worsen. Choose λ with validation or cross-validation—not by looking at the final test set.

Visualizing Coefficient Shrinkage

Comparing Learned Weights (Linear vs. Ridge)

Illustrative values only. Ridge tends to shrink coefficients toward zero. The amount of shrinkage depends on the data, feature scaling, and λ; L2 regularization does not intentionally create sparse feature selection.

Mathematical Example (Calculated)

Scenario: The Linear Regression cost (MSE) is currently 100. The model has two weights: w1 = 3 and w2 = 4. Let us compute the final Ridge Regression cost if Lambda (λ) is 2.

// 1. Identify Base Cost
MSE = 100

// 2. Calculate L2 Penalty (Square the weights & Sum)
Squared Sum = (3 * 3) + (4 * 4)
Squared Sum = 9 + 16 = 25

// 3. Multiply by Lambda
Penalty = λ * Squared Sum
Penalty = 2 * 25 = 50

Final Ridge Cost = 100 + 50 = 150

Ridge Regression Optimization

Ridge Regression can be solved using direct linear-algebra methods or iterative numerical solvers. To understand what the L2 penalty does during optimization, the Gradient Descent view is especially useful:

Gradient Descent Update Formula (with Ridge)

θ = θ - α(∂J/∂θ + 2λθ)

Formula Explanation

The update contains two parts inside the parenthesis:

  • ∂J/∂θ: The normal gradient (direction of steepest error descent).
  • 2λθ: The Ridge penalty adjustment (shrinks the weight proportionally to its current size).

The extra term pushes larger coefficients more strongly toward zero. The exact update depends on how the loss and regularization term are scaled in the chosen formulation.

Worked Example

Suppose we have the following values at the current step:

  • Current Weight (θ): 8
  • Learning Rate (α): 0.1
  • Gradient (∂J/∂θ): 5
  • Lambda (λ): 0.2

// Update Step

θ_new = 8 - 0.1 * (5 + 2 * 0.2 * 8)

θ_new = 8 - 0.1 * (5 + 3.2)

θ_new = 8 - 0.1 * (8.2)

θ_new = 8 - 0.82

θ_new = 7.18

Without the Ridge penalty, the new weight would have been θ_new = 8 - 0.1 * 5 = 7.5. The Ridge update reduces the weight more aggressively (to 7.18) because it heavily penalizes the large initial weight of 8.

Ridge vs. Lasso vs. Linear

FeatureLinearRidge (L2)Lasso (L1)
Penalty TermNoneSum of squared weightsSum of absolute weights
Coefficient ShrinkageNo shrinkageShrinks coefficients; does not promote sparsityCan shrink some coefficients exactly to zero
Feature SelectionNoNot a direct feature-selection methodCan perform sparse feature selection
Best Used ForWhen regularization is unnecessaryCorrelated predictors / unstable coefficientsWhen sparse coefficients are useful

Assumptions of Ridge Regression

Linear Relationship

There must be a linear relationship between features and the target variable.

Feature Scaling

Scaling is strongly recommended when numerical features use very different units, because the L2 penalty acts on coefficient size. It is not a mathematical requirement that every Ridge input be standardized.

Handles Collinearity

Ridge can stabilize coefficient estimates when predictors are strongly correlated, but it does not make every collinearity-related interpretation problem disappear.

Constant Variance

Roughly constant residual variance is useful for classical linear-model assumptions and inference. Prediction quality should still be checked directly with validation data.

Industry Applications Tree

Ridge Regression Domains
Finance
  • Credit Risk Analysis
  • Stock Price Forecasting
  • Algorithmic Trading
Healthcare
  • Genomic Data Analysis
  • Patient Recovery Prediction
  • Epidemic Forecasting
Real Estate
  • Property Value Estimation
  • Rent Fluctuation
  • Location Indexing
E-commerce
  • Customer Lifetime Value
  • Sales Forecasting
  • Dynamic Pricing
NLP & Computations
  • Text Classification Weights
  • High Dimensional Datasets
  • Deep Learning Preprocessing

Implementation in Python using Scikit-Learn

Below is a step-by-step comparison of ordinary Linear Regression and Ridge. We split the data first, then fit StandardScaler only on the training set so that information from the test set does not leak into preprocessing.

import & prepare data
from sklearn.linear_model import Ridge, LinearRegression
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
import numpy as np

# Random dummy data
np.random.seed(42)
X = np.random.rand(100, 3) # 100 houses, 3 features: [Rooms, Age, Location_Score]
y = 10 * X[:, 0] + 5 * X[:, 1] + 20 * X[:, 2] + np.random.randn(100) * 10
IMPORTANT: Split First, Then Learn the Scaling
# Split before learning preprocessing parameters
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)

# Fit the scaler on training data only
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
train models and compare
# Standard Linear Regression
lr = LinearRegression()
lr.fit(X_train_scaled, y_train)

# Ridge Regression (alpha controls L2 regularization strength)
ridge = Ridge(alpha=10.0)
ridge.fit(X_train_scaled, y_train)

print(f"Linear Regression Weights: {np.round(lr.coef_, 2)}")
print(f"Ridge Regression Weights:  {np.round(ridge.coef_, 2)}")

Terminal Output

Linear Regression Weights: [3.13 1.32 7.76] Ridge Regression Weights:  [2.70 1.14 6.85]

In this reproducible example, the Ridge coefficients are smaller in magnitude than the ordinary least-squares coefficients. That is the L2 penalty influencing the fit—not proof that Ridge will always predict better.

Complete Assembled Code
from sklearn.linear_model import Ridge, LinearRegression
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
import numpy as np

# 1. Random dummy data
np.random.seed(42)
X = np.random.rand(100, 3) # 100 houses, 3 features
y = 10 * X[:, 0] + 5 * X[:, 1] + 20 * X[:, 2] + np.random.randn(100) * 10

# 2. Split first
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)

# 3. Learn scaling from training data only
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)

# 4. Standard Linear Regression
lr = LinearRegression()
lr.fit(X_train_scaled, y_train)

# 5. Ridge Regression
ridge = Ridge(alpha=10.0)
ridge.fit(X_train_scaled, y_train)

# 6. Output
print(f"Linear Regression Weights: {np.round(lr.coef_, 2)}")
print(f"Ridge Regression Weights:  {np.round(ridge.coef_, 2)}")

Advantages of Ridge

  • Can reduce variance and overfitting when large coefficients are making the linear model too sensitive.
  • Often stabilizes coefficient estimates when predictors are strongly correlated.
  • Can improve generalization when the chosen regularization strength is appropriate.

Disadvantages of Ridge

  • Alpha is a hyperparameter and should usually be selected with validation or cross-validation.
  • L2 regularization does not promote sparse coefficients, so Ridge is not normally used as a direct feature-selection method.
  • Coefficient penalties are easier to compare when numerical features are on comparable scales, so scaling is often important in practice.

How Do We Choose Alpha (λ)?

Do not choose alpha by checking which value gives the best score on the final test set. Try several values using validation or cross-validation, then evaluate the selected model once on the test set.

AlphaTypical effect
0Same objective as ordinary least squares.
SmallGentle shrinkage.
LargerStronger shrinkage; too much can underfit.

Scikit-learn shortcut

RidgeCV can compare multiple alpha values using cross-validation.

from sklearn.linear_model import RidgeCV

ridge_cv = RidgeCV(alphas=[0.01, 0.1, 1, 10, 100])
ridge_cv.fit(X_train_scaled, y_train)

print(ridge_cv.alpha_)

Common Questions

Does Ridge remove features?

Not in the sparse-feature-selection sense. L2 regularization shrinks coefficients but does not encourage exact zeros the way Lasso does.

Is a larger alpha always better?

No. Too little regularization may leave high variance; too much can underfit. Choose alpha using validation or cross-validation.

Should I scale features?

Usually yes when numerical features have very different units, because Ridge penalizes coefficient magnitudes.


Final Summary

Ridge Regression is a fundamental L2-regularized linear model. By penalizing large coefficients, it can reduce variance, improve numerical stability, and make coefficient estimates less sensitive when predictors are strongly correlated.

Unlike Lasso, Ridge does not encourage a sparse solution with many coefficients exactly equal to zero. Instead, it usually keeps the predictors in the model while shrinking their coefficients by different amounts.

Most Important Insight to Remember:

"Ridge adds an L2 penalty to discourage large coefficients. Larger alpha means stronger shrinkage; choose alpha with validation or cross-validation rather than assuming one value is always best."

Apply this concept in a real hands-on project

You have studied the idea. Now use it in working Python code, see the calculation and output, and explain what changed. Each project includes step-by-step instructions, full source and tests.

Practice challenge: predict an outcome first, run the code, and explain the difference.

Track Your Progress0% curriculum done

Finished "Ridge Regression"?

Mark this lesson as completed to update your progress bar and track your milestones across the full curriculum.

Overall Progress0 / 167 lessons

Was this tutorial helpful?

Your feedback helps us continuously refine and improve our open-access tutorials.

Summary / Key Takeaways

  • Ridge adds an L2 penalty based on squared coefficient magnitudes.
  • Shrinkage can reduce variance for correlated or weak features.
  • The intercept is normally unpenalized and features should be scaled.
  • A larger penalty increases shrinkage but can underfit.
  • Ridge usually keeps every coefficient nonzero.
  • Tune regularization inside a cross-validated pipeline.

Knowledge Check

Progress: 0/4

Test your active recall. Choose one answer for each question, then check it to reveal the step-by-step reasoning.

Question 1 of 4
What penalty does Ridge Regression add to the MSE loss?
Question 2 of 4
Why does Ridge specifically help when features are highly correlated?
Question 3 of 4
You try λ values [0.001, 0.01, 0.1, 1, 10, 100] using 5-fold CV. Best validation RMSE is at λ=10, but training RMSE is higher there than at λ=0.001. Is this a problem?
Question 4 of 4
Ridge shrinks coefficients toward zero but never to exactly zero. When is this a disadvantage?

Knowledge Check Review & Explanations

Review the questions, verified correct answers, and conceptual explanations for this lesson.

1What penalty does Ridge Regression add to the MSE loss?▼

Correct Answer: λ × sum of squared weights (L2): Loss = MSE + λΣwᵢ²

The L2 penalty squares weights before summing. Large weights are penalised disproportionately more than small weights, creating strong pressure to keep all weights small and stable.

2Why does Ridge specifically help when features are highly correlated?▼

Correct Answer: Standard OLS becomes numerically unstable when features are collinear (X'X is nearly singular). Ridge adds λI to X'X before inverting, making the matrix well-conditioned and producing stable coefficient estimates under multicollinearity

OLS solves (X'X)⁻¹X'y. With multicollinearity, X'X is nearly singular — small data changes cause huge coefficient changes. Ridge solves (X'X + λI)⁻¹X'y, which is always invertible and stable.

3You try λ values [0.001, 0.01, 0.1, 1, 10, 100] using 5-fold CV. Best validation RMSE is at λ=10, but training RMSE is higher there than at λ=0.001. Is this a problem?▼

Correct Answer: No — this is correct behaviour. Higher λ means more regularisation, which intentionally increases training error slightly (adds bias) to reduce overfitting (reduce variance). Best validation RMSE at λ=10 means this tradeoff favours generalisation

Regularisation deliberately accepts higher training error to achieve better generalisation. The validation RMSE is the only metric you should use to select λ.

4Ridge shrinks coefficients toward zero but never to exactly zero. When is this a disadvantage?▼

Correct Answer: If the true model is sparse (only 10 of 500 features are relevant), Ridge keeps all 500 with small nonzero weights — hard to interpret and requiring computation of all 500 features at prediction time. Lasso (L1) drives irrelevant coefficients to exactly zero, giving a cleaner sparse model

Ridge produces dense models — all features get weights. For interpretability or when you want to eliminate irrelevant features entirely, Lasso's sparsity property is more appropriate.

Keep moving through the path

Continue Learning

Free Download: ML Interview Cheatsheet

100 essential Machine Learning interview questions and answers covering foundations, algorithms, evaluation, deep learning, Python, system design and interview strategy. Enter your email to download it instantly.

Optional Learning Resources

These resources may complement the lesson. They are optional and are not required to use ML Academy.

As an Amazon Associate I earn from qualifying purchases. Other links are identified separately when they are not affiliate links.

Next