Skip to content
StackDecode
AI & ML Atlas
Machine LearningalgorithmInteractive simulation

Ridge Regression

Linear regression with an L2 penalty that shrinks the weights.

The idea

Adding λ·Σw² to the loss discourages large weights, which makes the model less sensitive to noise and to correlated features. As λ grows, all weights shrink smoothly toward zero but never reach it exactly.

w = (XᵀX + λI)⁻¹Xᵀy

Watch it run

This is a real computation, not a drawing. Pause, step, change the settings, or try new random data.

x1x2x3x4x5x6
  • positive weight
  • negative weight
  • true weight (dashed)

With no penalty the model fits the noise freely: even useless features get a small weight.

penalty:
0
zero weights:
0
1/15

Bars are the learned weights; dashed lines are the weights that generated the data. Features 3, 4 and 6 are pure noise.

Step by step

  1. 1Standardise the features.
  2. 2Choose the penalty strength λ.
  3. 3Minimise squared error + λ‖w‖².
  4. 4Tune λ with cross-validation.

Where it is used

Many correlated features, small datasets, stabilising a regression.

Watch out for

Does not select features; every weight stays non-zero.

Related