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
Instability in test_ridge.py::test_ridge_sample_weights #11200
This means that this test is brittle and depends on the RNG state. Increasing numerical tolerance might be a solution or possibly increasing the number of samples?
Just a stand-alone snippet reproducing the problem, which is a small variation around test_ridge_sample_weights to estimate how sensitive it is on its random state:
fromitertoolsimportproductimportnumpyasnpfromsklearn.utils.testingimport (assert_array_almost_equal,
assert_almost_equal)
fromsklearn.linear_modelimportRidgefromscipyimportlinalgdeftest_ridge_sample_weights(rng):
param_grid=product((1.0, 1e-2), (True, False),
('svd', 'cholesky', 'lsqr', 'sparse_cg'))
forn_samples, n_featuresin ((6, 5), (5, 10)):
y=rng.randn(n_samples)
X=rng.randn(n_samples, n_features)
sample_weight=1.0+rng.rand(n_samples)
for (alpha, intercept, solver) inparam_grid:
# Ridge with explicit sample_weightest=Ridge(alpha=alpha, fit_intercept=intercept, solver=solver)
est.fit(X, y, sample_weight=sample_weight)
coefs=est.coef_inter=est.intercept_# Closed form of the weighted regularized least square# theta = (X^T W X + alpha I)^(-1) * X^T W yW=np.diag(sample_weight)
ifinterceptisFalse:
X_aug=XI=np.eye(n_features)
else:
dummy_column=np.ones(shape=(n_samples, 1))
X_aug=np.concatenate((dummy_column, X), axis=1)
I=np.eye(n_features+1)
I[0, 0] =0cf_coefs=linalg.solve(X_aug.T.dot(W).dot(X_aug) +alpha*I,
X_aug.T.dot(W).dot(y))
ifinterceptisFalse:
assert_array_almost_equal(coefs, cf_coefs)
else:
assert_array_almost_equal(coefs, cf_coefs[1:])
assert_almost_equal(inter, cf_coefs[0])
rng=np.random.RandomState(0)
foriinrange(100):
try:
test_ridge_sample_weights(rng)
exceptAssertionError:
print('failed')
So the problem was the default tolerance in Ridge. If we want to use the default tol in assert_array_almost_equal which is 1e-6, we need to make sure Ridge has the same tolerance. When I changed the tolerance in Ridge to 1e-6 (default was 1e-3), I did not get an errors any more. Here is the code I ran https://gist.github.com/sergulaydore/313bbcfa17d287fd97d492d0f62cea59. Of course, the test runs a little slower for a lower tolerance. Here is the comparison for timing:
Took 1.089055061340332 seconds with tol=1e-3 and Failed tests = 26 Took 1.3238649368286133 seconds with tol=1e-6 Failed tests = 0
Activity
lesteve commentedon Jun 6, 2018
Just a stand-alone snippet reproducing the problem, which is a small variation around
test_ridge_sample_weightsto estimate how sensitive it is on its random state:On my machine I get 26 failures out of 100 runs.
jnothman commentedon Jun 6, 2018
sergulaydore commentedon Jul 17, 2018
I generated a histogram of mismatch percentages for
decimal=4using the script https://gist.github.com/sergulaydore/6767aa908d051cb5d11417600f6161a1. The default test usesdecimal=6but it was hard to see the differences with this value. The issue only happens whensolverislsqrorsparse_cg. I used only one set ofparam_grid.sergulaydore commentedon Jul 17, 2018
So the problem was the default tolerance in
Ridge. If we want to use the defaulttolinassert_array_almost_equalwhich is1e-6, we need to make sureRidgehas the same tolerance. When I changed the tolerance inRidgeto1e-6(default was1e-3), I did not get an errors any more. Here is the code I ran https://gist.github.com/sergulaydore/313bbcfa17d287fd97d492d0f62cea59. Of course, the test runs a little slower for a lower tolerance. Here is the comparison for timing:Took 1.089055061340332 seconds with tol=1e-3 and Failed tests = 26Took 1.3238649368286133 seconds with tol=1e-6 Failed tests = 0I am creating a PR to fix this in the test.