-
-
Notifications
You must be signed in to change notification settings - Fork 26.4k
[MRG + 1] Friendly error on KNeighbors(n_neighbors=0 or not convertible to int) #4312
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
[MRG + 1] Friendly error on KNeighbors(n_neighbors=0 or not convertible to int) #4312
Conversation
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.
n_neighbors doesn't exist in all instances of NeighborsBase (i.e. where radius neighbors are intended).
I also don't understand why anyone would set n_neighbors to None. Or why we want to handle things like strings...
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.
It is set to None in _init_params, right? Don't you think this is the right fix?
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.
No informative error appears to be raised for n_neighbors == None for now, although the discussion seems to have converged to that. Perhaps:
if self.n_neighbors is None or self.n_neighbors <= 0:
raise ValueError(
...```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.
Some classes like radiusneighbors which inherits neighborbase do not need n_neighbors. n_neighbors could be None, I think. @banilo
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.
Indeed, none is valid in this part of the code.
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.
It would be possible to add a check in "predict", too, in a place where None is not valid, but I'm not sure that is necessary.
|
maybe we want to check |
|
I may have missed some of the discussion, but I've not known scikit-learn On 2 March 2015 at 11:01, Andreas Mueller notifications@github.com wrote:
|
|
hm we don't really need to. But we should check for zero, as that gives a weird error message downstream and can easily happen. Maybe just check |
|
@jnothman checked @amueller the code now just checks ```n_neighbors > 0 ''' if it is not None. |
|
Looks good to me, thanks. |
bb4704e to
d2cdcb5
Compare
d2cdcb5 to
2a5eba2
Compare
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.
style: please use a single line for function calls that fit in 80 columns.
Related: you should run the pep8 linter to identify code style issues (only consider the errors that match the lines that you edit in your branch).
2a5eba2 to
9986aa5
Compare
|
Thanks @ogrisel ! Fixed style problem. BTW, the code above the line I added has the style problem as well, do I need to fix them? |
No let's keep this PR focused and limit possible merge conflicts. |
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.
again style: please use enclosing parens for the if condition instead of the \-escaping of the new line.
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 for nitpicking :)
9986aa5 to
63812b7
Compare
|
Thanks!. I'd better to read PEP 8. When I use |
|
Then you have a very old version of the tool. |
Sure, it's just a recommendation for the consistency with the scikit-learn code base. |
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.
Similarly to the comnent above, not both cases appear to be covered by this regression test:
a) n_neighbors <= 1
b) n_neighbors == None
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.
a) n_neighbors could be 1, right? @banilo
b) like above comment
|
I am merging this, as it is a net improvement over the code, although, as suggested, it could be improved even more. |
[MRG + 1] Friendly error on KNeighbors(n_neighbors=0 or not convertible to int)
This PR added some lines to
NeighborsBase._fitfor checkingn_neighbors. If it is 0 or not convertible to int, it raise ValueError. Fix #4296