-
-
Notifications
You must be signed in to change notification settings - Fork 25.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ridge(normalize=True) fails check_sample_weights_invariance #17444
Comments
#19426 is a fix for the handling of sample_weight when To investigate the cause of the present issue (sample weight semantics), one could however try to use the minimal implementations proposed by @agramfort in the comment thread of #19426: #19426 (comment) In particular, I have extended the This minimal implementation could serve both as a debugging tool and a reference to compare to in the solver consistency test. |
Edit I made a mistake in the original code snippet. I fixed it below. I originally wrongly defined import numpy as np
from sklearn.linear_model import Ridge
from sklearn.preprocessing import scale
rng = np.random.RandomState(0)
n_samples, n_features, n_latents = 100, 300, 50
Z = rng.normal(size=(n_samples, n_latents)) # Low-rank-ish latent variables
X = Z @ rng.normal(size=(n_latents, n_features))
X = scale(X) # to use normalize=False
y = Z @ rng.normal(size=n_latents) + 1e-2 * rng.normal(n_samples)
cutoff = X.shape[0] // 3
sw = np.ones(shape=X.shape[0])
sw[:cutoff] = 0.
X_trimmed, y_trimmed = X[cutoff:, :], y[cutoff:]
params = dict(
alpha=1.,
normalize=False,
fit_intercept=True,
)
ridge_weighted = Ridge(**params).fit(X, y, sample_weight=sw)
ridge_trimmed = Ridge(**params).fit(X_trimmed, y_trimmed)
# Check that no coef is too close to zero to be able to compare
# coef with rtol:
assert np.all(np.abs(ridge_weighted.coef_) > 1e-4)
np.testing.assert_allclose(
ridge_weighted.coef_,
ridge_trimmed.coef_,
) |
For the import numpy as np
from sklearn.linear_model import Ridge
from sklearn.preprocessing import scale
rng = np.random.RandomState(0)
n_samples, n_features, n_latents = 100, 300, 50
Z = rng.normal(size=(n_samples, n_latents)) # Low-rank-ish latent variables
X = Z @ rng.normal(size=(n_latents, n_features))
X = scale(X) # to use normalize=False
y = Z @ rng.normal(size=n_latents) + 1e-2 * rng.normal(n_samples)
cutoff = X.shape[0] // 3
sw = np.ones(shape=X.shape[0])
sw[:cutoff] = 0.
X_trimmed, y_trimmed = X[cutoff:, :], y[cutoff:]
params = dict(
alpha=1.,
normalize=True,
fit_intercept=True,
)
ridge_weighted = Ridge(**params).fit(X, y, sample_weight=sw)
ridge_trimmed = Ridge(**params).fit(X_trimmed, y_trimmed)
# Check that no coef is too close to zero to be able to compare
# coef with rtol:
assert np.all(np.abs(ridge_weighted.coef_) > 1e-4)
np.testing.assert_allclose(
ridge_weighted.coef_,
ridge_trimmed.coef_,
)
|
With |
A possibly related issue to #3020 is that
Ridge(normalize=True)
breaks sample weight invariance properties: that is that removing samples is equivalent to setting sample weights to 0. For instance, running thecheck_sample_weights_invariance(kind=zeros)
estimator check forRidge(normalize=True)
in #17441fails by a significant amount for all solvers,
This makes somewhat sense as for instance
StandardScaler
doesn't account for sample weight andnormalize
likely doesn't also. But then I don't understand why his happens for Ridge but not LinearRegression, Lasso or ElasticNet.The text was updated successfully, but these errors were encountered: