Last active
August 29, 2015 14:02
-
-
Save eickenberg/79d360540a7c1c0cc953 to your computer and use it in GitHub Desktop.
Scaling comparison Ridge vs SGDRegressor(penalty='l2')
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from sklearn.linear_model import Ridge, SGDRegressor | |
rng = np.random.RandomState(42) | |
n_samples, n_features = 2000, 50 | |
X = rng.randn(n_samples, n_features) | |
w = rng.randn(n_features) | |
y_clean = X.dot(w) | |
noise_level = 1 | |
y_noisy = y_clean + y_clean.std() * rng.randn(n_samples) * noise_level | |
alpha = 10. | |
ridge = Ridge(alpha=alpha) | |
ridge_rescaled = Ridge(alpha=alpha * n_samples) | |
sgd = SGDRegressor(alpha=alpha, penalty='l2') | |
ridge.fit(X, y_noisy) | |
ridge_rescaled.fit(X, y_noisy) | |
sgd.fit(X, y_noisy) | |
coef_diff1 = ((ridge.coef_ - sgd.coef_) ** 2).sum() | |
coef_diff2 = ((ridge_rescaled.coef_ - sgd.coef_) ** 2).sum() | |
print coef_diff1, coef_diff2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment