-
-
Notifications
You must be signed in to change notification settings - Fork 25.9k
[MRG + 1] ElasticNetCV: raise ValueError if l1_ratio=0 #7591
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
Conversation
please add a test |
Added a test to make sure |
Hmmm I am wondering whether this is the right fix. If you look at the
Maybe you need to understand why you end up with the error Tip: you can add syntax-highlighting to your snippet by adding |
Yes, this is just a quick fix to disallow |
My bad I missed the associated issue somehow, maybe you want to update the docstring accordingly then. |
@@ -1063,6 +1063,8 @@ def fit(self, X, y): | |||
|
|||
if hasattr(self, 'l1_ratio'): | |||
model_str = 'ElasticNet' | |||
if self.l1_ratio == 0: | |||
raise ValueError("l1_ratio = 0 not supported") |
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.
Maybe you could add a mention about L2 penalty and RidgeCV like you did in the docstring?
Just curious what happens if l1_ratio is a list which contains 0?
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.
Sorry, that was sloppy of me. If 0 was contained in the list, the code would have run without an error, but the mse_path_
on the estimator for that value, would all be nan
s. I.e. a user would think the ridge estimate was evaluated, when it was in fact not. I updated the error message.
clf = ElasticNetCV(l1_ratio=0) | ||
clfl = ElasticNetCV(l1_ratio=[0, 0.5]) | ||
clfm = MultiTaskElasticNetCV(l1_ratio=0) | ||
assert_raises(ValueError, clf.fit, X, y) |
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.
Can you use assert_raise_message
to check the error message? Apart from that LGTM.
LGTM |
solver should work but it's auto mode for alpha grid that breaks |
@agramfort To confirm, you consider this the wrong fix? |
I'm confused. |
@@ -712,3 +712,13 @@ def test_enet_float_precision(): | |||
assert_array_almost_equal(intercept[np.float32], | |||
intercept[np.float64], | |||
decimal=4) | |||
|
|||
def test_enet_l1_ratio(): |
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.
too much indented
Estimator would work with l1_ratio=0. if use passes his own grid of alphas. The solver would manage. It's just the automatic alpha grid that breaks. fix is good enough but maybe I would throw the error if alphas are not passed explicitly. clear? |
Sorry for any confusion, I thought I made it clear in #7551 that it is the grid generation in _alpha_grid that fails. Is there anything I else I should do here? |
can you just make the grid generation fail. It should only be called if alphas=None |
Ok, to be sure: revert everything in this commit, and just raise a |
Yes correct. Make sure error message is as explicit as possible On Sat, Oct 15, 2016 at 3:17 PM +0200, "nuffe" notifications@github.com wrote: Ok, to be sure: revert everything in this commit, and just raise a ValueError if _alpha_grid is called with l1_ratio=0? It is currently only called if alphas=None. Should I update the docstring and add a test? — |
assert_raise_message(ValueError, msg, | ||
ElasticNetCV(l1_ratio=0).fit, X, y) | ||
assert_raise_message(ValueError, msg, | ||
MultiTaskElasticNetCV(l1_ratio=0).fit, X, X) |
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.
test that is works with you pass some alphas
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 am sorry for a possible stupid question, but this commit broke an unrelated test (Logistic regression) on AppVeyor:
======================================================================
FAIL: sklearn.linear_model.tests.test_logistic.test_logistic_regression_sample_weights
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\nose\case.py", line 197, in runTest
self.test(*self.arg)
File "C:\Python27\lib\site-packages\sklearn\linear_model\tests\test_logistic.py", line 638, in test_logistic_regression_sample_weights
assert_array_almost_equal(clf_cw.coef_, clf_sw.coef_, decimal=4)
File "C:\Python27\lib\site-packages\numpy\testing\utils.py", line 842, in assert_array_almost_equal
precision=decimal)
File "C:\Python27\lib\site-packages\numpy\testing\utils.py", line 665, in assert_array_compare
raise AssertionError(msg)
AssertionError:
Arrays are not almost equal to 4 decimals
(mismatch 20.0%)
x: array([[ 2.5404, 0. , 0. , -0.3094, -0.4925]])
y: array([[ 2.5405, 0. , 0. , -0.3094, -0.4924]])
sklearn.decomposition.tests.test_dict_learning.test_dict_learning_reconstruction_parallel: 4.1117s
but the same tests pass on my computer and on Travis, how do I proceed here?
please another commit to restart appveyor. Just to see if bug is reproducible |
It is still |
est = MultiTaskElasticNetCV(l1_ratio=0, alphas=alphas) | ||
with ignore_warnings(): | ||
est.fit(X, X) | ||
est_desired.fit(X, X) |
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.
calling fit(X, X) is weird. How about fit(X, y[:, None]) ?
The failure means that we're unclean on fixing random states somewhere, I think. And windows can give numerically different results. |
est.fit(X, y) | ||
assert_array_almost_equal(est.coef_, est_desired.coef_, decimal=5) | ||
|
||
est_desired = MultiTaskElasticNetCV(l1_ratio=0.00001, alphas=alphas) |
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.
set the random state explicitly
Ideally we would explicitly pass random states everywhere in all tests. To fix the test failure, maybe fix the random states in the test that's failing. You can also add it in the test that you added. |
Ah, thank you. Wouldn't it be easier to set the random state in every single estimator to something else than None by default, instead of changing every test? |
That would drastically change user code. We have our estimators be non-deterministic by default, which is a design choice. Changing that would be quite a severe change in API. It also requires us to be very explicit about randomness in our tests, which is A Good Thing (TM) |
thx @Nuffe |
Ah, thank you. Wouldn't it be easier to set the random state in every single
estimator to something else than None by default, instead of changing every
test?
We have so far frowned from this, as it is against user expectations that
a random object behaves in a deterministic way.
|
…7591) Raise ValueError if l1_ratio=0 in ElasticNetCV and alphas=None
…7591) Raise ValueError if l1_ratio=0 in ElasticNetCV and alphas=None
…7591) Raise ValueError if l1_ratio=0 in ElasticNetCV and alphas=None
…7591) Raise ValueError if l1_ratio=0 in ElasticNetCV and alphas=None
…7591) Raise ValueError if l1_ratio=0 in ElasticNetCV and alphas=None
…7591) Raise ValueError if l1_ratio=0 in ElasticNetCV and alphas=None
Reference Issue
Fixes #7551
What does this implement/fix? Explain your changes.
Before,
gives the runtime error
UnboundLocalError: local variable 'best_l1_ratio' referenced before assignment
Now it returns
ValueError: l1_ratio = 0 not supported