-
-
Notifications
You must be signed in to change notification settings - Fork 25.8k
[MRG] Add Penalty factors for each coefficient in enet ( see #11566) #11671
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
Open
doaa-altarawy
wants to merge
4
commits into
scikit-learn:main
Choose a base branch
from
doaa-altarawy:adaptive_enet
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3e71c4b
Implement penalty.factor of glmnet R package to have different penalt…
doaa-altarawy dbeffd5
Add tests for l1_weights in enet (#11566)
doaa-altarawy 9d4c12e
Fix flake8 and docstring errors
doaa-altarawy 67764c8
Fix flake8, long line
doaa-altarawy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -291,6 +291,44 @@ def test_enet_path(): | |
assert_almost_equal(clf1.alpha_, clf2.alpha_) | ||
|
||
|
||
@pytest.mark.filterwarnings('ignore: You should specify a value') # 0.22 | ||
def test_enet_selective_penalty(): | ||
n_features = 200 | ||
n_informative_features = 20 | ||
# A dataset with small number of samples and large number of features | ||
# with the last n_informative_features as the informative ones | ||
X, y, X_test, y_test = build_dataset( | ||
n_samples=50, n_features=n_features, | ||
n_informative_features=n_informative_features) | ||
|
||
# Default weight is 1 for all features, keep l1 penalty | ||
l1_weights = np.ones(n_features) | ||
|
||
# Add some prior knowledge, when we know some features are important | ||
# So, we will relax l1 penalty on the last n_informative_features. | ||
# Use any small number, or zero if you are 100% sure of the prior | ||
# knowledge | ||
l1_weights[:n_informative_features-1] *= 0.001 | ||
|
||
# Run enet with prior knowledge (l1_weights) | ||
clf_with_prior = ElasticNetCV(alphas=[0.01, 0.05, 0.1, 0.5, 1, 1.5], | ||
eps=2e-3, cv=3, l1_weights=l1_weights) | ||
|
||
ignore_warnings(clf_with_prior.fit)(X, y) | ||
|
||
# This is a model without using any prior knowledge | ||
clf_base = ElasticNetCV(alphas=[0.01, 0.05, 0.1, 0.5, 1, 1.5], | ||
eps=2e-3, cv=3) | ||
|
||
ignore_warnings(clf_base.fit)(X, y) | ||
|
||
# Accuracy of the model with prior knowledge should be higher | ||
# than the model without prior knowledge for a hard data set | ||
# (much less samples than features) | ||
assert_greater(clf_with_prior.score(X_test, y_test), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use plain |
||
clf_base.score(X_test, y_test)) | ||
|
||
|
||
@pytest.mark.filterwarnings('ignore: You should specify a value') # 0.22 | ||
def test_path_parameters(): | ||
X, y, _, _ = build_dataset() | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the
l1_weights
should be inside the L1 norm:||l1_weights*w||__1