Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions sklearn/linear_model/tests/test_ridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -998,16 +998,18 @@ def test_dtype_match_cholesky():

@pytest.mark.parametrize(
'solver', ['svd', 'cholesky', 'lsqr', 'sparse_cg', 'sag', 'saga'])
def test_ridge_regression_dtype_stability(solver):
random_state = np.random.RandomState(0)
@pytest.mark.parametrize('seed', range(1))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This range looks a bit funny

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's meant to make it easy to edit this test to run on 100s of different seeds. We probably need a more generic way to achieve this with a fixture.

def test_ridge_regression_dtype_stability(solver, seed):
random_state = np.random.RandomState(seed)
n_samples, n_features = 6, 5
X = random_state.randn(n_samples, n_features)
coef = random_state.randn(n_features)
y = np.dot(X, coef) + 0.01 * rng.randn(n_samples)
y = np.dot(X, coef) + 0.01 * random_state.randn(n_samples)
alpha = 1.0
rtol = 1e-2 if os.name == 'nt' and _IS_32BIT else 1e-5

results = dict()
# XXX: Sparse CG seems to be far less numerically stable than the
# others, maybe we should not enable float32 for this one.
atol = 1e-3 if solver == "sparse_cg" else 1e-5
for current_dtype in (np.float32, np.float64):
results[current_dtype] = ridge_regression(X.astype(current_dtype),
y.astype(current_dtype),
Expand All @@ -1022,4 +1024,4 @@ def test_ridge_regression_dtype_stability(solver):

assert results[np.float32].dtype == np.float32
assert results[np.float64].dtype == np.float64
assert_allclose(results[np.float32], results[np.float64], rtol=rtol)
assert_allclose(results[np.float32], results[np.float64], atol=atol)