Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Scikit-Learn · Class

Ridge

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.

How it works

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.

Syntax

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)

Parameters

NameTypeDescription
alphafloat or array-like of shape (n_targets,), default=1.0L2 regularization multiplier; a non-negative float or an array-like value shaped (n_targets,), default 1.0.
fit_interceptbool, default=TrueWhether to calculate the model intercept; default True.
copy_Xbool, default=TrueWhether to copy X before fitting; default True.
max_iterint, default=NoneMaximum iterations for iterative solvers; default None.
tolfloat, default=0.0001Solver convergence tolerance; default 0.0001.
solver{'auto', 'svd', 'cholesky', 'lsqr', 'sparse_cg', 'sag', 'saga', 'lbfgs'}, default='auto'Numerical solver selection; default 'auto'.
positivebool, default=FalseWhether to constrain coefficients to be positive; default False.
random_stateint, RandomState instance or None, default=NoneRandom-state control for solvers that use randomness; default None.

Returns

A fitted Ridge estimator from fit; predict returns an array of predicted target values.

Key points

  • Ridge combines a linear least-squares loss with L2 regularization.
  • alpha controls the strength of coefficient shrinkage: larger values apply a stronger penalty.
  • The estimator is also called ridge regression or Tikhonov regularization.
  • The class supports multi-output regression without requiring a separate model per target.
  • Call fit(X, y) to learn the coefficients from training data.
  • Call predict(X) after fitting to generate regression predictions.

Common mistakes

  • alpha cannot be negative; use zero only when an unregularized least-squares solution is intended.
  • For multiple targets, alpha may be supplied as one value per target.

Example: Fit and predict with Ridge

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 to use it

Stabilize linear regression

When predictors are correlated or numerous relative to observations.

Regularize a regression model

When coefficient magnitudes should be reduced to limit overfitting.

Fit multi-target regression

When one feature matrix maps to several continuous outputs.

Interview questions

QWhat objective does Ridge optimize?

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.

QWhat forms can alpha take?

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.

QHow does Ridge handle multiple regression targets?

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.

  • RidgeCV
  • RidgeClassifier
  • RidgeClassifierCV
  • KernelRidge

Source: sklearn.linear_model.Ridge — scikit-learn 1.9.1 documentation. Checked 2026-10-15.