8000 [MRG + 1] Friendly error on KNeighbors(n_neighbors=0 or not convertible to int) by xuewei4d · Pull Request #4312 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

[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

Merged

Conversation

xuewei4d
Copy link
Contributor
@xuewei4d xuewei4d commented Mar 1, 2015

This PR added some lines to NeighborsBase._fit for checking n_neighbors. If it is 0 or not convertible to int, it raise ValueError. Fix #4296

@@ -258,6 +258,19 @@ def _fit(self, X):
else:
raise ValueError("algorithm = '%s' not recognized"
% self.algorithm)

if self.n_neighbors is not None:
Copy link
Member

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...

Copy link
Member

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?

Copy link
Contributor

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(
...```

Copy link
Contributor Author

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

Copy link
Member

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.

Copy link
Member

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.

@amueller
Copy link
Member
amueller commented Mar 2, 2015

maybe we want to check isinstance(n_neighors, numbers.Integral)?

@jnothman
Copy link
Member
jnothman commented Mar 2, 2015

I may have missed some of the discussion, but I've not known scikit-learn
to use such strict type checking of parameters. Why are we doing so here?

On 2 March 2015 at 11:01, Andreas Mueller notifications@github.com wrote:

maybe we want to check isinstance(n_neighors, numbers.Integral)?


Reply to this email directly or view it on GitHub
#4312 (comment)
.

@amueller
Copy link
Member
amueller commented Mar 2, 2015

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 n_neighbors > 0 and if that crashes then the user should see their mistake.

@xuewei4d
Copy link
Contributor Author
xuewei4d commented Mar 4, 2015

@jnothman checked RadiusNeighborsClassifier and RadiusNeighborsClassifier. They will call NeighborsBase._init_params to initiate the parameter which includes set n_neighbors to None, although radius neighbors classes don't use n_neighbors.

@amueller the code now just checks ```n_neighbors > 0 ''' if it is not None.

@amueller
Copy link
Member
amueller commented Mar 5, 2015

Looks good to me, thanks.

@amueller amueller changed the title [MRG] Friendly error on KNeighbors(n_neighbors=0 or not convertible to int) [MRG + 1] Friendly error on KNeighbors(n_neighbors=0 or not convertible to int) Mar 5, 2015
@xuewei4d xuewei4d force-pushed the friendly_error_on_kneighbors branch from bb4704e to d2cdcb5 Compare March 22, 2015 23:07
@coveralls
Copy link

Coverage Status

Coverage decreased (-0.01%) to 95.08% when pulling d2cdcb5 on xuewei4d:friendly_error_on_kneighbors into 2badad3 on scikit-learn:master.

@xuewei4d xuewei4d force-pushed the friendly_error_on_kneighbors branch from d2cdcb5 to 2a5eba2 Compare March 23, 2015 20:32
nbrs = cls(n_neighbors=-1)
assert_raises(ValueError,
nbrs.fit,
X, y)
Copy link
Member

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).

@ogrisel
Copy link
Member
ogrisel commented Mar 24, 2015

Other than my style-related comment this looks good to me as well.

@jnothman we could maybe think of a refactoring of the neighbors based class to avoid initializing a n_neighbors attribute on radius related classes but this looks out of the scope of this specific PR (which is to fix #4296).

@xuewei4d xuewei4d force-pushed the friendly_error_on_kneighbors branch from 2a5eba2 to 9986aa5 Compare March 24, 2015 23:11
@xuewei4d
Copy link
Contributor Author

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?

@ogrisel
Copy link
Member
ogrisel commented Mar 24, 2015

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.

@@ -830,6 +830,10 @@ def test_neighbors_badargs():
assert_raises(ValueError,
nbrs.predict,
[])
if isinstance(cls, neighbors.KNeighborsClassifier) or \
isinstance(cls, neighbors.KNeighborsRegressor):
Copy link
Member

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.

Copy link
Member

Choose a reason for hiding this comment

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

sorry for nitpicking :)

@xuewei4d xuewei4d force-pushed the friendly_error_on_kneighbors branch from 9986aa5 to 63812b7 Compare March 24, 2015 23:34
@xuewei4d
Copy link
Contributor Author

Thanks!. I'd better to read PEP 8. When I use pep8 to check style, it didn't report \ problem....

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.01%) to 95.1% when pulling 9986aa5 on xuewei4d:friendly_error_on_kneighbors into fc681f2 on scikit-learn:master.

@amueller
Copy link
Member

Then you have a very old version of the tool.

@ogrisel
Copy link
Member
ogrisel commented Mar 25, 2015

When I use pep8 to check style, it didn't report \ problem

Sure, it's just a recommendation for the consistency with the scikit-learn code base.

if (isinstance(cls, neighbors.KNeighborsClassifier) or
isinstance(cls, neighbors.KNeighborsRegressor)):
nbrs = cls(n_neighbors=-1)
assert_raises(ValueError, nbrs.fit, X, y)
Copy link
Contributor

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

Copy link
Contributor Author

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

@GaelVaroquaux
Copy link
Member

I am merging this, as it is a net improvement over the code, although, as suggested, it could be improved even more.

GaelVaroquaux added a commit that referenced this pull request Aug 30, 2015
[MRG + 1] Friendly error on KNeighbors(n_neighbors=0 or not convertible to int)
@GaelVaroquaux GaelVaroquaux merged commit 242aaca into scikit-learn:master Aug 30, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Friendly error on KNeighbors(n_neighbors=0)
7 participants
0