Skip to content

TST Improve ridge solver consistency tests #19503

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

Merged
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
17 changes: 13 additions & 4 deletions sklearn/linear_model/tests/test_ridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from sklearn.model_selection import cross_val_predict
from sklearn.model_selection import LeaveOneOut

from sklearn.preprocessing import minmax_scale
from sklearn.utils import check_random_state
from sklearn.datasets import make_multilabel_classification

Expand Down Expand Up @@ -415,24 +416,32 @@ def _make_sparse_offset_regression(
@pytest.mark.parametrize(
'n_samples,dtype,proportion_nonzero',
[(20, 'float32', .1), (40, 'float32', 1.), (20, 'float64', .2)])
@pytest.mark.parametrize('normalize', [True, False])
@pytest.mark.parametrize('seed', np.arange(3))
def test_solver_consistency(
solver, proportion_nonzero, n_samples, dtype, sparse_X, seed):
solver, proportion_nonzero, n_samples, dtype, sparse_X, seed,
normalize):
alpha = 1.
noise = 50. if proportion_nonzero > .9 else 500.
X, y = _make_sparse_offset_regression(
bias=10, n_features=30, proportion_nonzero=proportion_nonzero,
noise=noise, random_state=seed, n_samples=n_samples)
if not normalize:
# Manually scale the data to avoid pathological cases. We use
# minmax_scale to deal with the sparse case without breaking
# the sparsity pattern.
X = minmax_scale(X)
svd_ridge = Ridge(
solver='svd', normalize=True, alpha=alpha).fit(X, y)
solver='svd', normalize=normalize, alpha=alpha).fit(X, y)
X = X.astype(dtype, copy=False)
y = y.astype(dtype, copy=False)
if sparse_X:
X = sp.csr_matrix(X)
if solver == 'ridgecv':
ridge = RidgeCV(alphas=[alpha], normalize=True)
ridge = RidgeCV(alphas=[alpha], normalize=normalize)
else:
ridge = Ridge(solver=solver, tol=1e-10, normalize=True, alpha=alpha)
ridge = Ridge(solver=solver, tol=1e-10, normalize=normalize,
alpha=alpha)
ridge.fit(X, y)
assert_allclose(
ridge.coef_, svd_ridge.coef_, atol=1e-3, rtol=1e-3)
Expand Down