Back to questions
JuniorTheoryCommon
What does this program print?
import numpy as np
from sklearn.linear_model import Lasso, Ridge
X = np.array([[1, 5], [2, 1], [3, 4], [4, 2], [5, 3]])
y = np.array([2, 4, 6, 8, 10])
ridge = Ridge(alpha=1.0).fit(X, y)
lasso = Lasso(alpha=1.0).fit(X, y)
print(int((ridge.coef_ == 0).sum()), int((lasso.coef_ == 0).sum()))0 1
- Expecting
0 0: the L1 penalty ofLassosets the coefficient of the second column, which does not help predicty, exactly to zero. - Expecting
1 1: the L2 penalty ofRidgeshrinks every coefficient toward zero but never makes one exactly zero.
- What happens to the first coefficient of
Lassoasalphakeeps growing? - Why should the features be scaled before either penalty is applied?