Back to questions
JuniorTheoryCommon
What does this program print?
import numpy as np
from sklearn.linear_model import Ridge
X = np.array([[1.0], [2.0], [3.0], [4.0]])
y = np.array([2.0, 4.0, 6.0, 8.0])
weak = Ridge(alpha=0.01).fit(X, y)
strong = Ridge(alpha=100.0).fit(X, y)
print(round(weak.coef_[0], 2), round(strong.coef_[0], 2))2.0 0.1
- Expecting
2.0 2.0because the points lie exactly on a line: the penalty grows withalphaand pulls the slope toward zero even when the fit is perfect. - Expecting
0.0for the strong model: the L2 penalty ofRidgeshrinks a coefficient but never makes it exactly zero, so it stays at0.1.
- How does the intercept of
strongchange, given thatRidgedoes not penalize it? - Which
alphawould you expect to work best on new data, and how would you choose it?