Repository navigation
Closed
Labels
APIBreaking ChangeIssue resolution would not be easily handled by the usual deprecation cycle.Needs DecisionRequires decisionmodule:linear_model
Description
Describe the bug
Ridge and SGDRegressor are inconsistent in the way they handle the regularization hyperparameter alpha:
Ridge(alpha=alpha)is equivalent toSGDRegressor(alpha / m, penalty="l2", ...), wheremis the training set size.- But
Lasso(alpha=alpha)is equivalent toSGDRegressor(alpha, penalty="l1", ...), which is great.
Looking at the source code of SGDRegressor, it looks like l1 and l2 are consistent, so it must be a problem with Ridge. Perhaps it should be modified to divide alpha by m?
Is it just historical, or is there a rationale behind this?
Steps/Code to Reproduce
import numpy as np
from sklearn.linear_model import Lasso, Ridge, SGDRegressor
alpha = 0.1
m = 100
np.random.seed(42)
X = 2 * np.random.rand(m, 1)
y = 4 + 3 * X + np.random.randn(m, 1)
ridge = Ridge(alpha=alpha)
ridge.fit(X, y)
print("Ridge:", ridge.intercept_, ridge.coef_)
sgd_reg = SGDRegressor(
loss="squared_error", penalty="l2", alpha=alpha / m,
max_iter=10_000, tol=None, eta0=0.0001,
learning_rate="constant", random_state=42)
sgd_reg.fit(X, y.ravel())
print("SGDRegressor with l2:", sgd_reg.intercept_, sgd_reg.coef_)
lasso = Lasso(alpha=alpha)
lasso.fit(X, y)
print("Lasso:", lasso.intercept_, lasso.coef_)
sgd_reg = SGDRegressor(loss="squared_error", penalty="l1", alpha=alpha,
max_iter=10_000, tol=None, eta0=0.0001,
learning_rate="constant", random_state=42)
sgd_reg.fit(X, y.ravel())
print("SGDRegressor with l1:", sgd_reg.intercept_, sgd_reg.coef_)Expected Results
I would expect the Ridge model to be different from the SGDRegressor model, since it does not use the same alpha value. Instead, they are equal.
Actual Results
But here is the output of the previous code:
Ridge: [4.22250784] [[2.76223165]]
SGDRegressor with l2: [4.22261191] [2.76235098]
Lasso: [4.48341837] [2.48477396]
SGDRegressor with l1: [4.4834874] [2.48492104]
As you can see, the Ridge model is equivalent to SGDRegressor(alpha=alpha / m, ...) instead of SGDRegressor(alpha=alpha, ...). But Lasso(alpha) is equivalent to SGDRegressor(alpha=alpha, penalty="l1", ...).
Versions
Running on Colab:
System:
python: 3.7.12 (default, Sep 10 2021, 00:21:48) [GCC 7.5.0]
executable: /usr/bin/python3
machine: Linux-5.4.104+-x86_64-with-Ubuntu-18.04-bionic
Python dependencies:
pip: 21.1.3
setuptools: 57.4.0
sklearn: 1.0.1
numpy: 1.19.5
scipy: 1.4.1
Cython: 0.29.24
pandas: 1.1.5
matplotlib: 3.2.2
joblib: 1.1.0
threadpoolctl: 3.0.0
Built with OpenMP: True
Metadata
Metadata
Assignees
Labels
APIBreaking ChangeIssue resolution would not be easily handled by the usual deprecation cycle.Needs DecisionRequires decisionmodule:linear_model
Activity
kaush26 commentedon Dec 6, 2021
The
alphaofSGDRegressoris different form that ofRidge, but same forLassois may be because the cost function ofRidgeis slightly differ than that ofLassoandSGDRegressor.i.e.
Loss of Ridge:
||y - Xw||_2^2 + α_r * ||w||_2^2Loss of Lasso :
1 / m ||y - Xw||_2^2 + α_l * ||w||_1Loss of SGDRegressor :
1 / m ||y - Xw||_2^2 + α_s * ||w||_k^kwhere
k=1, 2forL1, L2norms respectivelycomparing the above results :
α_s ≡ α_lα_s ≡ α_r / mglemaitre commentedon Dec 6, 2021
Yep this look at what we are discussing here #15657
and that recently pointed out here #21238 (reply in thread)
We should settle on the definition of the loss, how to handle
sample_weightand document it clearly.TomDLT commentedon Dec 11, 2021
See also this example. It is about linear SVC, but the same argument should work with least-square regression. The conclusion is that it makes sense to do a different scaling by n_samples for L1 compared to L2 regularization.
lorentzenchr commentedon Sep 26, 2026
@ageron I am a bit late 😄, still you might find replacing C with 1/alpha in
LogisticRegressioninteresting. There was a similar discussion as to the scaling with or without 1/n, see #32042 (comment).lorentzenchr commentedon Sep 30, 2026
It is true that we are not consistent in the way the penalties are implemented in linear models. Basically, we have 2 of them:
1/n * sum(loss) + alpha * penaltyExamples are
Lasso,PoissonRegressor1/n * sum(loss) + alpha/n * penaltyExamples are
Ridgeand (soon also with alpha)LogisticRegressionConsiderations:
Conclusion: If we were to design the estimators from scratch, we might value consistency of the parametrization more. If one cv-searches for optimal penalty alpha, the parametrization does not matter (much).
If many users would prefer such a consistency, we might consider it for a new major version (like 2.0).
I therefore dare to close this issue. @scikit-learn/core-devs friendly ping in case you are interested.