Stabilize linear regression
When predictors are correlated or numerous relative to observations.
Scikit-Learn · Class
Ridge is a linear regression estimator that minimizes squared error with an L2 penalty on coefficients. Its alpha parameter controls regularization strength, and it supports single- and multi-target regression.
Ridge fits a linear least-squares model while adding an L2 penalty to the coefficient size. The alpha value controls that penalty, and the estimator can fit either one target or multiple targets; after fitting, it predicts through the learned linear relationship.
class sklearn.linear_model.Ridge(alpha=1.0, *, fit_intercept=True, copy_X=True, max_iter=None, tol=0.0001, solver='auto', positive=False, random_state=None)| Name | Type | Description |
|---|---|---|
alpha | float or array-like of shape (n_targets,), default=1.0 | L2 regularization multiplier; a non-negative float or an array-like value shaped (n_targets,), default 1.0. |
fit_intercept | bool, default=True | Whether to calculate the model intercept; default True. |
copy_X | bool, default=True | Whether to copy X before fitting; default True. |
max_iter | int, default=None | Maximum iterations for iterative solvers; default None. |
tol | float, default=0.0001 | Solver convergence tolerance; default 0.0001. |
solver | {'auto', 'svd', 'cholesky', 'lsqr', 'sparse_cg', 'sag', 'saga', 'lbfgs'}, default='auto' | Numerical solver selection; default 'auto'. |
positive | bool, default=False | Whether to constrain coefficients to be positive; default False. |
random_state | int, RandomState instance or None, default=None | Random-state control for solvers that use randomness; default None. |
A fitted Ridge estimator from fit; predict returns an array of predicted target values.
from sklearn.linear_model import Ridge
import numpy as np
X = np.array([[0.0], [1.0], [2.0], [3.0]])
y = np.array([0.0, 1.0, 2.0, 3.0])
model = Ridge(alpha=1.0)
model.fit(X, y)
print(model.predict([[4.0]]))Creates a Ridge regressor, fits it to one-feature data, and predicts a target for a new sample.
When predictors are correlated or numerous relative to observations.
When coefficient magnitudes should be reduced to limit overfitting.
When one feature matrix maps to several continuous outputs.
Ridge minimizes squared prediction error plus an L2 coefficient penalty. The penalty is scaled by alpha, so increasing alpha generally shrinks coefficients more strongly.
Follow-up: What happens when alpha is zero? It removes the regularization term, reducing the objective to ordinary least squares, provided the chosen solver supports that setting.
The default alpha is 1.0. It may also be an array-like value with one entry for each target in a multi-output regression problem.
Follow-up: Why use target-specific alpha values? They allow different regularization strengths when output targets have different scales or noise characteristics.
Ridge supports multi-variate regression when y is a two-dimensional array shaped as (n_samples, n_targets). After fitting, predict accepts feature data and returns predictions from the linear model.
Follow-up: How would you inspect the learned model? Examine coef_ for feature weights and intercept_ for the fitted offset after calling fit.
RidgeCVRidgeClassifierRidgeClassifierCVKernelRidge