Implementation:Scikit learn Scikit learn Ridge
| Knowledge Sources | |
|---|---|
| Domains | Machine Learning, Regularized Regression |
| Last Updated | 2026-02-08 15:00 GMT |
Overview
Concrete tool for linear least squares regression with L2 regularization (Tikhonov regularization) provided by scikit-learn.
Description
Ridge implements linear least squares with L2 regularization, minimizing ||y - Xw||^2_2 + alpha * ||w||^2_2. Also known as Ridge Regression or Tikhonov regularization, it addresses multicollinearity and overfitting by shrinking coefficient magnitudes. The implementation supports multiple solvers including SVD decomposition, Cholesky factorization, sparse conjugate gradient, LSQR, SAG/SAGA, and L-BFGS. It natively supports multi-variate regression (2d target arrays) and per-target regularization strengths. The module also includes RidgeClassifier for classification tasks.
Usage
Use Ridge when you have correlated features (multicollinearity), when ordinary least squares overfits, or when you want to regularize a linear model without inducing sparsity. It is particularly useful when all features are believed to be relevant but their individual contributions should be dampened. Ridge is often preferred over Lasso when you do not expect a sparse solution.
Code Reference
Source Location
- Repository: scikit-learn
- File: sklearn/linear_model/_ridge.py
Signature
class Ridge(MultiOutputMixin, RegressorMixin, _BaseRidge):
def __init__(
self,
alpha=1.0,
*,
fit_intercept=True,
copy_X=True,
max_iter=None,
tol=1e-4,
solver="auto",
positive=False,
random_state=None,
):
Import
from sklearn.linear_model import Ridge
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| alpha | float or ndarray of shape (n_targets,) | No | L2 regularization strength; must be non-negative (default=1.0) |
| fit_intercept | bool | No | Whether to fit the intercept (default=True) |
| copy_X | bool | No | Whether to copy X (default=True) |
| max_iter | int | No | Maximum iterations for iterative solvers (default=None, solver-dependent) |
| tol | float | No | Precision of the solution for iterative solvers (default=1e-4) |
| solver | str | No | Solver: 'auto', 'svd', 'cholesky', 'lsqr', 'sparse_cg', 'sag', 'saga', 'lbfgs' (default='auto') |
| positive | bool | No | Force coefficients to be positive (default=False) |
| random_state | int or RandomState | No | Random seed for 'sag' and 'saga' solvers |
Outputs
| Name | Type | Description |
|---|---|---|
| coef_ | ndarray of shape (n_features,) or (n_targets, n_features) | Estimated weight vector |
| intercept_ | float or ndarray of shape (n_targets,) | Independent term in the linear model |
| n_iter_ | int or None | Number of iterations for iterative solvers |
Usage Examples
Basic Usage
from sklearn.linear_model import Ridge
from sklearn.datasets import make_regression
X, y = make_regression(n_samples=100, n_features=20, noise=10, random_state=42)
model = Ridge(alpha=1.0)
model.fit(X, y)
print("Score:", model.score(X, y))
print("Intercept:", model.intercept_)