Ridge penalty
Quick answer
Ridge regression fits a linear prediction rule while charging a cost for large coefficients. Its L2 penalty squares each weight. This can make predictions less sensitive to small changes in correlated training features, at the cost of biased coefficients. Examples are standalone local Python 3.13.5 snippets; library examples use scikit-learn 1.8.0. ByteMind execution is unverified.
Compute a penalized loss
r = [1, -2]w = [2, -3]alpha = 2print(sum(e*e for e in r) + alpha*sum(v*v for v in w))Expected output:
31The residual cost is 5 and the weight cost is 26.
How it works
Write predictions as b + Xw, where b is an intercept and w is a vector of feature weights. In scikit-learn Ridge, the objective is SSE + alpha * sum(w_j**2), with alpha nonnegative; the intercept is not penalized when fitted separately. For centered X and y, the stationary condition is (X.T @ X + alpha * I)w = X.T @ y. With alpha > 0 the matrix is positive definite even if X has dependent columns. Solve the system rather than explicitly inverting it. With one centered feature x, w = sum(xy)/(sum(xx)+alpha). For x=[-1,1], y=[-2,2], the numerator is 4 and denominator 2+alpha: alpha=2 yields w=1, compared with OLS w=2. Increasing alpha reduces the L2 norm of the fitted weights; individual weights need not decrease monotonically in a correlated design. More regularization can increase bias while reducing estimation variance, but does not guarantee lower test error. Fit scaling on training rows, because coefficients depend on feature units. A regularization path is a sequence of fits at different penalty strengths, not gradient-descent iterations. Select alpha using validation or cross-validation, never a final test set.
Trace a one-feature ridge path
from sklearn.linear_model import RidgeX = [[-1], [1]]y = [-2, 2]for alpha in [2, 6]: m = Ridge(alpha=alpha, fit_intercept=False).fit(X, y) print(round(float(m.coef_[0]), 3))Expected output:
1.00.5The same centered training set produces smaller weights as the penalty increases.
Common mistakes
Do not penalize an intercept by accident by adding a constant column to a penalized design. Do not compare coefficients measured in different feature units as if they represented the same effect. Use a training-fitted scaler and keep the intercept separate.
Practice and next steps
For the centered example, calculate the slope at alpha=0 and alpha=10. Explain why the training fit worsens even though future predictions may become more stable. Complete the knowledge check and coding exercise. Next: Lasso and sparsity.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Ridge penalty — knowledge check
Apply ridge penalty to new examples.
Which quantity is the ridge penalty?
For centered x=[-1,1], y=[-2,2], alpha=6, what is the ridge weight?
What does a ridge path vary?
Why fit a scaler on training rows only?
What is guaranteed by increasing alpha in the standard ridge objective?