Computing Library › Machine Learning
Machine Learning

Ridge Regression

Ridge regression adds an L2 penalty to least squares, shrinking weights to reduce variance and tame correlated features.

The penalty

Ridge regression minimizes squared error plus a penalty on the squared size of the weights: L(w) = sum (y_i - x_i . w)^2 + alpha * sum w_j^2. The hyperparameter alpha controls the strength. As alpha grows, weights shrink toward zero, trading a little bias for a large drop in variance.

Why shrinkage helps

Kronos motion — lego machine

When features are correlated, ordinary least squares can produce huge, unstable weights that cancel each other. The L2 penalty makes X^T X + alpha*I always invertible and well-conditioned, giving a stable closed form w = (X^T X + alpha*I)^{-1} X^T y. Predictions become far less sensitive to noise in the training data.

python
from sklearn.linear_model import Ridge
model = Ridge(alpha=1.0)
model.fit(X_train, y_train)
# tune alpha by cross-validation, never on the test set

Ridge versus lasso

Choosing alpha

alpha is a hyperparameter, selected by cross-validation over a log-spaced grid. Standardize features first so the penalty treats them on equal footing. Ridge is the default regularizer when you expect many small effects rather than a few dominant ones.

Ridge is a concrete instance of regularization, the general principle of constraining a model to improve generalization.