Skip to content

FIX KNeighborsClassifier raise when all neighbors of some sample have zero weights - #26410

Merged
thomasjpfan merged 24 commits into
scikit-learn:mainfrom
Charlie-XIAO:knc-zero-weights
Jan 15, 2024
Merged

thomasjpfan merged 24 commits into
scikit-learn:mainfrom
Charlie-XIAO:knc-zero-weights

Conversation

@Charlie-XIAO

Copy link
Copy Markdown
Contributor

Reference Issues/PRs

Fixes #25854.

What does this implement/fix? Explain your changes.

Raises a ValueError when the weights for all neighbors of some sample are zero. Previous behavior: predict predicts the first class, predict_proba returns all zero probability.

@thomasjpfan thomasjpfan left a comment

Copy link
Copy Markdown
Member

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 @Charlie-XIAO !

I have two concerns:

  1. There is performance and memory overhead of computing np.all(weights == 0, axis=1).any().
  2. 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.

Comment thread sklearn/neighbors/_classification.py Resolved
Comment thread doc/whats_new/v1.3.rst Resolved
@betatim

betatim commented May 22, 2023

Copy link
Copy Markdown
Member

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?

@Charlie-XIAO

Copy link
Copy Markdown
Contributor Author

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?

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

it seems odd to get a prediction where all the nearest neighbours have a weight of 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.

@betatim

betatim commented May 24, 2023

Copy link
Copy Markdown
Member

Ok, let's not worry about the more broader class of problems -> I'm ok with raising an error like Olivier proposed.

@github-actions

github-actions Bot commented Sep 15, 2023 •

Copy link
Copy Markdown

✔️ Linting Passed

All linting checks passed. Your pull request is in excellent shape! ☀️

Generated for commit: 2fee151. Link to the linter CI: here

@glemaitre
glemaitre self-requested a review November 15, 2023 16:40
@glemaitre

Copy link
Copy Markdown
Member

There is performance and memory overhead of computing np.all(weights == 0, axis=1).any()

It seems that the cost of making the check will not be significant compare to the timing involved in predict. The check is ~6.6 ms for a weight array of 1 millions samples by 10 neighbors while a couple of minutes when it comes to the prediction.

Do you think this is problematic @thomasjpfan?

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.

I would rather consider it a fix

@Charlie-XIAO Charlie-XIAO changed the title ENH KNeighborsClassifier raise when all neighbors of some sample have zero weights FIX KNeighborsClassifier raise when all neighbors of some sample have zero weights Nov 15, 2023
@thomasjpfan

thomasjpfan commented Nov 25, 2023 •

Copy link
Copy Markdown
Member

It seems that the cost of making the check will not be significant compare to the timing involved in predict

Runtime is okay with me. I am more concern with the n_samples * n_neighbors memory increase from weights == 0 for error checking. I think the only way to work around this is to have a small Cython function.

@Charlie-XIAO

Charlie-XIAO commented Dec 13, 2023 •

Copy link
Copy Markdown
Contributor Author

@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 glemaitre left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just a couple of thoughts.

Comment thread doc/whats_new/v1.4.rst Resolved
Comment thread sklearn/utils/arrayfuncs.pyx Resolved
Comment thread sklearn/neighbors/_classification.py Resolved
@Charlie-XIAO

Copy link
Copy Markdown
Contributor Author

@glemaitre Changes made, seems that your idea is right that we can support more dtypes by using a fused type in Cython function.

@glemaitre
glemaitre self-requested a review January 9, 2024 15:22

@glemaitre glemaitre left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Comment thread doc/whats_new/v1.4.rst Resolved
Comment thread doc/whats_new/v1.4.rst Resolved
@glemaitre glemaitre added the Waiting for Second Reviewer First reviewer is done, need a second one! label Jan 11, 2024

@thomasjpfan thomasjpfan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you for the update!

Comment thread sklearn/utils/arrayfuncs.pyx Resolved
Comment thread sklearn/utils/arrayfuncs.pyx Outdated
Comment on lines +47 to +48
axis: int, default=0
The axis along which to perform the `all` operation.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Given we only need axis=1 in KNeighborsClassifier, can we restrict this function to: _all_with_any_reduction_axis_1?

@Charlie-XIAO Charlie-XIAO Jan 14, 2024 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Comment thread sklearn/utils/arrayfuncs.pyx Resolved
@Charlie-XIAO

Copy link
Copy Markdown
Contributor Author

Thanks for your review @thomasjpfan! I've updated according to your suggestions, but maybe we still need to wait to see if we want _all_with_any_reduction_axis_1 or the more general _all_with_any_reduction.

@thomasjpfan thomasjpfan left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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?

@thomasjpfan

Copy link
Copy Markdown
Member

I pushed a commit to fix the merge conflict with the changelog.

@glemaitre

Copy link
Copy Markdown
Member

I am happy with the current state of this PR. @glemaitre Are you okay with the change to _all_with_any_reduction_axis_1?

Ok with this change.

@thomasjpfan
thomasjpfan merged commit 6f9d565 into scikit-learn:main Jan 15, 2024
@thomasjpfan thomasjpfan added the To backport PR merged in master that need a backport to a release branch defined based on the milestone. label Jan 15, 2024
@Charlie-XIAO
Charlie-XIAO deleted the knc-zero-weights branch January 15, 2024 04:34
Charlie-XIAO added a commit to Charlie-XIAO/scikit-learn that referenced this pull request Jan 15, 2024
glemaitre pushed a commit that referenced this pull request Jan 15, 2024
@jeremiedbb jeremiedbb added this to the 1.4 milestone Jan 17, 2024
jeremiedbb pushed a commit to jeremiedbb/scikit-learn that referenced this pull request Jan 17, 2024
…ve zero weights (scikit-learn#26410)

Co-authored-by: Thomas J. Fan <thomasjpfan@gmail.com>
jeremiedbb pushed a commit to jeremiedbb/scikit-learn that referenced this pull request Jan 17, 2024
jeremiedbb pushed a commit that referenced this pull request Jan 17, 2024
…ve zero weights (#26410)

Co-authored-by: Thomas J. Fan <thomasjpfan@gmail.com>
jeremiedbb pushed a commit that referenced this pull request Jan 17, 2024
glemaitre pushed a commit to glemaitre/scikit-learn that referenced this pull request Feb 10, 2024
…ve zero weights (scikit-learn#26410)

Co-authored-by: Thomas J. Fan <thomasjpfan@gmail.com>
glemaitre pushed a commit to glemaitre/scikit-learn that referenced this pull request Feb 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module:neighbors To backport PR merged in master that need a backport to a release branch defined based on the milestone. Waiting for Second Reviewer First reviewer is done, need a second one!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

KNeighborsClassifier with Default Label

5 participants