Skip to content

ENH: improve validation for SGD models to accept l1_ratio=None when penalty is not elasticnet#30730

Merged
jeremiedbb merged 14 commits into
scikit-learn:mainfrom
MarcBresson:main
Apr 18, 2025
Merged

ENH: improve validation for SGD models to accept l1_ratio=None when penalty is not elasticnet#30730
jeremiedbb merged 14 commits into
scikit-learn:mainfrom
MarcBresson:main

Conversation

@MarcBresson

Copy link
Copy Markdown
Contributor

Reference Issues/PRs

What does this implement/fix? Explain your changes.

Providing l1_ratio only makes sense when user provides penalty=elasticnet. The idea behind this PR is to make the l1_ratio parameter of both SGD models behave the same as l1_ratio of LogisticRegression.

For now, I did something non breaking, but ideally we would set the defaut value for SGDClassifier.l1_ratio to None. I'm waiting on feedback as to what path I should follow considering that this brings breaking API changes.

Here is the l1_ratio definition for logistic regression if you are curious.

Any other comments?

@github-actions

github-actions Bot commented Jan 28, 2025

Copy link
Copy Markdown

✔️ Linting Passed

All linting checks passed. Your pull request is in excellent shape! ☀️

Generated for commit: c2e0b84. Link to the linter CI: here

@adrinjalali

Copy link
Copy Markdown
Member

This also needs a test to check the behavior when None is passed, both with elasticnet and not.

@adrinjalali adrinjalali left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We could also add a changelog here.

Comment thread sklearn/linear_model/_stochastic_gradient.py Outdated
Comment thread sklearn/linear_model/tests/test_sgd.py Outdated
@adrinjalali

Copy link
Copy Markdown
Member

Tests are failing here, and please avoid force pushing to the branch. Makes it harder to review. We squash and merge in the end anyway, so it doesn't matter how many commits you have here.

@MarcBresson

Copy link
Copy Markdown
Contributor Author

Sorry for the wild rebase on the upstream main branch, I tend to forget to specify --no-ff

@adrinjalali adrinjalali left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Otherwise LGTM.

Comment thread doc/whats_new/v1.7.rst

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@adrinjalali adrinjalali added the Waiting for Second Reviewer First reviewer is done, need a second one! label Feb 20, 2025
@adrinjalali

Copy link
Copy Markdown
Member

@OmarManzoor maybe you could have a look?

@OmarManzoor OmarManzoor left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the PR @MarcBresson

Comment thread doc/whats_new/upcoming_changes/sklearn.linear_model/30730.enhancement.rst Outdated
Comment thread doc/whats_new/v1.7.rst
Comment thread sklearn/linear_model/tests/test_sgd.py Outdated
{"penalty": "l1", "l1_ratio": None},
],
)
def test_sgd_passing_validation(klass, kwargs):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
def test_sgd_passing_validation(klass, kwargs):
def test_sgd_passing_penalty_validation(klass, kwargs):
"""Tests that acceptable values for the `penalty` parameter pass the
validation checks"""

Comment thread sklearn/linear_model/tests/test_sgd.py Outdated
),
],
)
def test_sgd_failing_validation(klass, kwargs, err_msg):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
def test_sgd_failing_validation(klass, kwargs, err_msg):
def test_sgd_failing_penalty_validation(klass, kwargs, err_msg):
"""Tests that improper values for the `penalty` parameter raise on
validation"""

Comment thread sklearn/linear_model/tests/test_sgd.py Outdated

@jeremiedbb jeremiedbb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I applied the requested changes and modified the tests to avoid checking what's already covered by the common tests.

LGTM. Thanks @MarcBresson

@jeremiedbb jeremiedbb merged commit 9a6e90a into scikit-learn:main Apr 18, 2025
lucyleeow pushed a commit to EmilyXinyi/scikit-learn that referenced this pull request Apr 23, 2025
…enalty is not `elasticnet` (scikit-learn#30730)

Co-authored-by: Jérémie du Boisberranger <jeremie@probabl.ai>
Co-authored-by: Omar Salman <omar.salman2007@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module:linear_model Waiting for Second Reviewer First reviewer is done, need a second one!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants