Skip to content

Lasso(alpha) = SGDRegressor(alpha, penalty="l1"), but Ridge(alpha) = SGDRegressor(alpha / len(train_set)) #21891

Closed

Description

@ageron

Describe the bug

Ridge and SGDRegressor are inconsistent in the way they handle the regularization hyperparameter alpha:

  • Ridge(alpha=alpha) is equivalent to SGDRegressor(alpha / m, penalty="l2", ...), where m is the training set size.
  • But Lasso(alpha=alpha) is equivalent to SGDRegressor(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

Activity

  1. kaush26 commented on Dec 6, 2021

    @kaush26

    The alpha of SGDRegressor is different form that of Ridge, but same for Lasso is may be because the cost function of Ridge is slightly differ than that of Lasso and SGDRegressor.

    i.e.

    • Loss of Ridge:

      ||y - Xw||_2^2 + α_r * ||w||_2^2

    • Loss of Lasso :

      1 / m ||y - Xw||_2^2 + α_l * ||w||_1

    • Loss of SGDRegressor :

      1 / m ||y - Xw||_2^2 + α_s * ||w||_k^k

    where k=1, 2 for L1, L2 norms respectively

    comparing the above results :
    α_s ≡ α_l
    α_s ≡ α_r / m

  2. glemaitre commented on Dec 6, 2021

    @glemaitre
    Member

    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_weight and document it clearly.

  3. TomDLT commented on Dec 11, 2021

    @TomDLT
    Member

    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.

  4. added
    Needs Decision
    Breaking Change
    and removed on Mar 14, 2024
  5. lorentzenchr commented on Sep 26, 2026

    @lorentzenchr
    Member

    @ageron I am a bit late 😄, still you might find replacing C with 1/alpha in LogisticRegression interesting. There was a similar discussion as to the scaling with or without 1/n, see #32042 (comment).

  6. lorentzenchr commented on Sep 30, 2026

    @lorentzenchr
    Member

    It is true that we are not consistent in the way the penalties are implemented in linear models. Basically, we have 2 of them:

    • A) 1/n * sum(loss) + alpha * penalty
      Examples are Lasso, PoissonRegressor
    • B) 1/n * sum(loss) + alpha/n * penalty
      Examples are Ridge and (soon also with alpha) LogisticRegression

    Considerations:

    • Different estimators have different penalties, like L2 in Ridge but L1 in Lasso. Different penalties might perform (a little bit) better with different scalings.
    • A) might be easier to reason about, the penalty is w.r.t. the mean loss.
    • B) can be seen as Bayesian Gaussian prior for the coefficients, the more data (larger n) the less the impact of the prior. Also theory for the statistical risk indicates that the optimal penalty should decay with the sample size.

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    APIBreaking ChangeNeeds Decisionmodule:linear_model

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions