8000 [MRG] LogReg: Check if l1_ratio is None when using elasticnet by joseortiz3 · Pull Request #25925 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

[MRG] LogReg: Check if l1_ratio is None when using elasticnet #25925

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 6 commits into from
Mar 22, 2023
Merged
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions sklearn/linear_model/_logistic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,9 @@ def fit(self, X, y, sample_weight=None):
"(penalty={})".format(self.penalty)
)

if self.penalty == "elasticnet" and self.l1_ratio is None:
raise ValueError("l1_ratio must be specified when penalty is elasticnet.")

# TODO(1.4): Remove "none" option
if self.penalty == "none":
warnings.warn(
Expand Down
9 changes: 9 additions & 0 deletions sklearn/linear_model/tests/test_logistic.py
739A
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,15 @@ def test_check_solver_option(LR):
lr.fit(X, y)


@pytest.mark.parametrize("LR", [LogisticRegression, LogisticRegressionCV])
def test_elasticnet_l1_ratio_err_helpful(LR):
# Check that an informative error message is raised when penalty="elasticnet"
# but l1_ratio is not specified.
model = LR(penalty="elasticnet", solver="saga")
with pytest.raises(ValueError, match=r".*l1_ratio.*"):
model.fit(np.array([[1, 2], [3, 4]]), np.array([0, 1]))


@pytest.mark.parametrize("solver", ["lbfgs", "newton-cg", "sag", "saga"])
def test_multinomial_binary(solver):
# Test multinomial LR on a binary problem.
Expand Down
0