Skip to content

Ridge and Lasso return different shaped .coef_ attributes #19693

Closed

Description

@mjcronin

Describe the bug

Ridge.coef_ returns an array with shape (1, n_features), while Lasso.coef_ returns an array with shape (n_features,). LinearRegression.coef_ resembles Ridge.coef_

Steps/Code to Reproduce

import numpy as np
from sklearn.linear_model import Ridge, Lasso
x = np.array([1,2,3,4,5]).reshape(-1,1)
y = np.array([2,4,6,8,10]).reshape(-1,1)
ridge_reg = Ridge()
lasso_reg = Lasso()
_ = ridge_reg.fit(x,y)
_ = lasso_reg.fit(x,y)
print(ridge_reg.coef_.shape)
print(lasso_reg.coef_.shape)

Expected Results

(1, 1)
(1, 1)

Actual Results

(1, 1)
(1,)

Versions

System:
python: 3.7.6 (default, Feb 14 2020, 16:17:39) [Clang 11.0.0 (clang-1100.0.33.17)]
executable: ./venv-mmm/bin/python
machine: Darwin-19.6.0-x86_64-i386-64bit

Python dependencies:
pip: 21.0.1
setuptools: 41.2.0
sklearn: 0.23.2
numpy: 1.19.1
scipy: 1.5.2
Cython: None
pandas: 1.1.1
matplotlib: 3.3.1
joblib: 0.16.0
threadpoolctl: 2.1.0

Built with OpenMP: True

Activity

  1. NicolasHug commented on Mar 16, 2021

    @NicolasHug
    Member

    Thanks for the report, that's indeed unexpected.

    For a temporary fix, just don't reshape the target: y = np.array([2,4,6,8,10])

  2. mjcronin commented on Mar 16, 2021

    @mjcronin
    Author
  3. thomasjpfan commented on Mar 20, 2021

    @thomasjpfan
    Member

    Intuitively, I would think that y.ndim == 2 would mean continuous-multioutput. But currently:

    from sklearn.utils.multiclass import type_of_target
    y = np.array([2,4,6,8.1,10.1]).reshape(-1,1)
    print(type_of_target(y))
    # 'continuous'

    This would imply that we should treat this 2d y the same as y.ravel(). If that is the case, then Ridge is incorrectly treating y as a continuous-multioutput and Lasso is okay.

  4. skjerns commented on Nov 19, 2021

    @skjerns

    Intuitively, I would think that y.ndim == 2 would mean continuous-multioutput. But currently:

    from sklearn.utils.multiclass import type_of_target
    y = np.array([2,4,6,8.1,10.1]).reshape(-1,1)
    print(type_of_target(y))
    # 'continuous'

    This would imply that we should treat this 2d y the same as y.ravel(). If that is the case, then Ridge is incorrectly treating y as a continuous-multioutput and Lasso is okay.

    This persists, even when the target is multiclass

    import numpy as np
    from sklearn.utils.multiclass import type_of_target
    from sklearn.linear_model import Lasso
    
    X = np.random.rand(500,50)
    y = np.random.randint(0,5,500)
    
    clf = Lasso()
    clf.fit(X,y)
    
    print(clf.coef_.shape, type_of_target(y))
    # (50, ) , multiclass
    

    I think it should be made clear in the documentation that the input y is expected as a one-hot encoded vector, which is different from the other linear model classifiers and base classes of sklearn

  5. thomasjpfan commented on Nov 27, 2021

    @thomasjpfan
    Member

    Since Lasso and Ridge are regressors, an integer y input is considered a regression problem and not a multi-class by the model.

    As for this issue (which deals with Lasso + Ridge being consistent), Lasso and Ridge are consistent when y is a 1-d int:

    import numpy as np
    from sklearn.linear_model import Lasso, Ridge
    
    X = np.random.rand(500,50)
    y = np.random.randint(0,5,500)
    
    lasso = Lasso()
    lasso.fit(X, y)
    print(lasso.coef_.shape)
    # (50, )
    
    ridge = Ridge()
    ridge.fit(X, y)
    print(ridge.coef_.shape)
    # (50, )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions