Skip to content

MNT Postpone conversion of RidgeCV's alphas out of __init__ #21506

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 3 commits into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
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
6 changes: 4 additions & 2 deletions sklearn/linear_model/_ridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -1483,7 +1483,7 @@ def __init__(
is_clf=False,
alpha_per_target=False,
):
self.alphas = np.asarray(alphas)
self.alphas = alphas
self.fit_intercept = fit_intercept
self.normalize = normalize
self.scoring = scoring
Expand Down Expand Up @@ -1842,6 +1842,8 @@ def fit(self, X, y, sample_weight=None):
if sample_weight is not None:
sample_weight = _check_sample_weight(sample_weight, X, dtype=X.dtype)

self.alphas = np.asarray(self.alphas)

if np.any(self.alphas <= 0):
raise ValueError(
"alphas must be strictly positive. Got {} containing some "
Expand Down Expand Up @@ -1977,7 +1979,7 @@ def __init__(
store_cv_values=False,
alpha_per_target=False,
):
self.alphas = np.asarray(alphas)
self.alphas = alphas
self.fit_intercept = fit_intercept
self.normalize = normalize
self.scoring = scoring
Expand Down
7 changes: 5 additions & 2 deletions sklearn/linear_model/tests/test_coordinate_descent.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ def test_lasso_cv_positive_constraint():

def _scale_alpha_inplace(estimator, n_samples):
"""Rescale the parameter alpha from when the estimator is evoked with
normalize set to True to when it is evoked in a Pipeline with normalize set
normalize set to True as if it were evoked in a Pipeline with normalize set
to False and with a StandardScaler.
"""
if ("alpha" not in estimator.get_params()) and (
Expand All @@ -360,7 +360,10 @@ def _scale_alpha_inplace(estimator, n_samples):
return

if isinstance(estimator, (RidgeCV, RidgeClassifierCV)):
alphas = estimator.alphas * n_samples
# alphas is not validated at this point and can be a list.
# We convert it to a np.ndarray to make sure broadcasting
# is used.
alphas = np.asarray(estimator.alphas) * n_samples
return estimator.set_params(alphas=alphas)
if isinstance(estimator, (Lasso, LassoLars, MultiTaskLasso)):
alpha = estimator.alpha * np.sqrt(n_samples)
Expand Down
21 changes: 21 additions & 0 deletions sklearn/linear_model/tests/test_ridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,27 @@ def test_ridge_classifier_cv_store_cv_values(scoring):
assert r.cv_values_.shape == (n_samples, n_targets, n_alphas)


@pytest.mark.parametrize("Estimator", [RidgeCV, RidgeClassifierCV])
def test_ridgecv_alphas_conversion(Estimator):
rng = np.random.RandomState(0)
alphas = (0.1, 1.0, 10.0)

n_samples, n_features = 5, 5
if Estimator is RidgeCV:
y = rng.randn(n_samples)
else:
y = rng.randint(0, 2, n_samples)
X = rng.randn(n_samples, n_features)

ridge_est = Estimator(alphas=alphas)
assert (
ridge_est.alphas is alphas
), f"`alphas` was mutated in `{Estimator.__name__}.__init__`"

ridge_est.fit(X, y)
assert_array_equal(ridge_est.alphas, np.asarray(alphas))


def test_ridgecv_sample_weight():
rng = np.random.RandomState(0)
alphas = (0.1, 1.0, 10.0)
Expand Down