-
-
Notifications
You must be signed in to change notification settings - Fork 26.2k
MNT: Make error message clearer for n_neighbors #23317
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
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.
Thank you for the PR!
I think it would be good to have a test based on #17207 to check the new error message.
Co-authored-by: Thomas J. Fan <thomasjpfan@gmail.com>
I didn't know we could use f-strings @thomasjpfan! In that case, this will be available only in v0.23 or later, right? (That's when support for Python v3.5 and earlier was removed) |
This fix will not be part of 0.23, but in 1.1.X or 1.2, so we can use f-strings. f-strings are currently used throughout the library since Python 3.5 was dropped. |
As noted in #23317 (review), this PR requires a test in |
Co-authored-by: Thomas J. Fan <thomasjpfan@gmail.com>
Co-authored-by: Thomas J. Fan <thomasjpfan@gmail.com>
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.
As noted in #23317 (comment), this PR still requires a test to check the new message.
Hi @Bharat123rox will you be able to address the comments to this pull request? Thanks! |
Co-authored-by: Thomas J. Fan <thomasjpfan@gmail.com>
Hi @Bharat123rox , |
Apologies @Kshitij68 @cmarmo I'm still working on this! I was having setup issues with my system on running |
Thanks for looking into this quickly.
|
Which version of the changelog? 1.1.x or 1.2.x? |
1.2.x: hope it will be out soon.... with your PR included... :) |
Overall the PR looks good to me. Pre-approving it. Note: I am not a core maintainer for scikit-learn, but a contributor. So you might get some additional change requests |
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.
The test is confusing.
- First, we have
n_neighbors=2
but the error message saysn_neighbors=1
. This is becauseLocalOutlierFactor.fit
silently usesself.n_neighbors_ = max(1, min(n_neighbors, n_samples - 1))
. We should be more explicit in the test. - Second,
clf
is not correctly fitted (because the fit raised an error). The fact that it does not raise aNotFittedError
is because the error was caught by pytest, but this is confusing. We should add a proper fit for clarity. - Third, the test makes
kneighbors
raise bothn_neighbors < n_samples_fit
andn_neighbors <= n_samples_fit
with the same number of neighbors. We should test that one is more restrictive than the other.
Proposed test:
def test_lof_error():
X = np.ones((7, 7))
# self.fit only raises if n_samples == 1.
msg = ("Expected n_neighbors < n_samples_fit, but n_neighbors = 1, "
"n_samples_fit = 1, n_samples = 1")
with pytest.raises(ValueError, match=msg):
clf = neighbors.LocalOutlierFactor(n_neighbors=1).fit(X[:1])
clf = neighbors.LocalOutlierFactor(n_neighbors=2).fit(X[:2])
# self.kneighbors(None) raises if n_neighbors == n_samples.
msg = ("Expected n_neighbors < n_samples_fit, but n_neighbors = 2, "
"n_samples_fit = 2, n_samples = 2")
with pytest.raises(ValueError, match=msg):
clf.kneighbors(None, n_neighbors=2)
clf.kneighbors(None, n_neighbors=1)
# self.kneighbors(X) raises if n_neighbors > n_samples.
msg = ("Expected n_neighbors <= n_samples_fit, but n_neighbors = 3, "
"n_samples_fit = 2, n_samples = 7")
with pytest.raises(ValueError, match=msg):
clf.kneighbors(X, n_neighbors=3)
clf.kneighbors(X, n_neighbors=2)
Co-authored-by: Tom Dupré la Tour <tom.duprelatour.10@gmail.com>
Co-authored-by: Tom Dupré la Tour <tom.duprelatour.10@gmail.com>
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 solve the conflicts and just added a couple of assert instead of just running some of code lines.
Activating the auto-merge.
Thanks @bharatr21
Reference Issues/PRs
#17207
What does this implement/fix? Explain your changes.
Fixes #17207 by making error message clearer
Tagging @cmarmo and @joshuacwnewton for review based on the earlier PR