Repository navigation
FIX KNeighborsClassifier raise when all neighbors of some sample have zero weights - #26410
Conversation
thomasjpfan
left a comment
There was a problem hiding this comment.
Thank you for the PR @Charlie-XIAO !
I have two concerns:
- There is performance and memory overhead of computing
np.all(weights == 0, axis=1).any(). - There could be a downstream library depending on the existing behavior and this PR will break it. For us to merge, we'll need to consider this PR a bug fix and worth breaking user's code.
|
When I first read the subject of the pull request I thought it would be fixing a bug. This is because we try to follow the "if a sample has a weight of zero it should be treated as if it doesn't exist", so it seems odd to get a prediction where all the nearest neighbours have a weight of zero. Shouldn't we ignore the neighbours that have a weight of zero, which would mean that they don't count as neighbours? For the case of fixed weights this might be easier to do than when the weights are the result of a user provided function. This means it might not be worth strictly implementing this behaviour (too much cost for an edge case). What do people think? |
@betatim IMO there could be cases where the weights of neighbors too far away are assigned zero. But since we are already looking at nearest neighbors, all neighbors would have weight zero.
I agree that this is an odd case. In the original issue, @ogrisel mentioned that such an error message is intended to let users change their weight function in order to avoid this odd case in the first place. Not sure if this is worth a change that breaks backward compatibility. Would like to hear what others think. |
|
Ok, let's not worry about the more broader class of problems -> I'm ok with raising an error like Olivier proposed. |
It seems that the cost of making the check will not be significant compare to the timing involved in Do you think this is problematic @thomasjpfan?
I would rather consider it a fix |
KNeighborsClassifier raise when all neighbors of some sample have zero weightsKNeighborsClassifier raise when all neighbors of some sample have zero weights
Runtime is okay with me. I am more concern with the |
|
@thomasjpfan Sorry for the late response. I have created a small Cython function for this (not sure if I did it correctly though, because I'm not very familiar with Cython). Would you like to take a look? By the way I'm currently placing it under utils.arrayfuncs, but perhaps it's better to make it private? I'm not sure what it the correct place for it. |
|
@glemaitre Changes made, seems that your idea is right that we can support more dtypes by using a fused type in Cython function. |
glemaitre
left a comment
There was a problem hiding this comment.
This looks good on my side. If we get a second review before the 1.4.0 release, we can include this fix inside.
@thomasjpfan Do you think that you can have a look.
thomasjpfan
left a comment
There was a problem hiding this comment.
Thank you for the update!
| axis: int, default=0 | ||
| The axis along which to perform the `all` operation. |
There was a problem hiding this comment.
Given we only need axis=1 in KNeighborsClassifier, can we restrict this function to: _all_with_any_reduction_axis_1?
There was a problem hiding this comment.
Sure, I will make this change for now. Indeed I cannot easily come up with a scenario that we want to check "some feature of all samples is equal to a value". But @glemaitre if you have different opinions please let me know.
|
Thanks for your review @thomasjpfan! I've updated according to your suggestions, but maybe we still need to wait to see if we want |
There was a problem hiding this comment.
I see that @glemaitre recommended making it more generic in: #26410 (comment). Although axis=0 is simple, I prefer not to maintain code we are not using. If we need axis=1 in the future, we can add it then.
I am happy with the current state of this PR. @glemaitre Are you okay with the change to _all_with_any_reduction_axis_1?
|
I pushed a commit to fix the merge conflict with the changelog. |
Ok with this change. |
…ve zero weights (scikit-learn#26410) Co-authored-by: Thomas J. Fan <thomasjpfan@gmail.com>
…ve zero weights (#26410) Co-authored-by: Thomas J. Fan <thomasjpfan@gmail.com>
…ve zero weights (scikit-learn#26410) Co-authored-by: Thomas J. Fan <thomasjpfan@gmail.com>
Reference Issues/PRs
Fixes #25854.
What does this implement/fix? Explain your changes.
Raises a
ValueErrorwhen the weights for all neighbors of some sample are zero. Previous behavior:predictpredicts the first class,predict_probareturns all zero probability.