You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Ridge and Lasso return different shaped .coef_ attributes #19693
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)
Thanks for looking into this. Interestingly, `.coef_.shape` remains the
same for me regardless of the shape of `y`. For now I will just reshape
the `.coef_` output when used as input to my functions as required. My use
case here is a function to plot the coefficients across multiple
independent variables extracted from various classes in
`sklearn.linear_model `, and my current function fails when the shape of
`.coef_` is not as expected.
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 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
Activity
NicolasHug commentedon Mar 16, 2021
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])mjcronin commentedon Mar 16, 2021
thomasjpfan commentedon Mar 20, 2021
Intuitively, I would think that
y.ndim == 2would meancontinuous-multioutput. But currently:This would imply that we should treat this 2d
ythe same asy.ravel(). If that is the case, thenRidgeis incorrectly treatingyas acontinuous-multioutputandLassois okay.Only ravel the coefficients when response is flat
skjerns commentedon Nov 19, 2021
This persists, even when the target is multiclass
I think it should be made clear in the documentation that the input
yis expected as a one-hot encoded vector, which is different from the other linear model classifiers and base classes ofsklearnthomasjpfan commentedon Nov 27, 2021
Since
LassoandRidgeare regressors, an integeryinput is considered a regression problem and not a multi-class by the model.As for this issue (which deals with
Lasso+Ridgebeing consistent),LassoandRidgeare consistent whenyis a 1-d int: