FEA Add array API support for top_k_accuracy_score#34425
Draft
Fazel94 wants to merge 3 commits into
Draft
Conversation
Keep the documented tie-breaking behavior (ties are resolved in favor of the highest index) by flipping an ascending stable argsort instead of using a stable descending sort. Replace np.setdiff1d in the labels validation with _isin, and use _average for the normalize and sample_weight handling, like accuracy_score does. The labels parameter is now normalized with column_or_1d up front and its length measured with size(), because array API arrays are not required to implement __len__. Register the metric in the array API common tests (multiclass, and binary with k=1) and document support. Towards scikit-learn#26024
49c112b to
0cf11e2
Compare
Namespaces whose default floating dtype is float32, like PyTorch, average the boolean hits at float32 precision, so the dispatched score cannot be compared exactly against the float64 NumPy reference. The tolerance is orders of magnitude smaller than the gap between the two possible tie-breaking directions, so the test keeps its purpose.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Reference Issues/PRs
Towards #26024
What does this implement/fix? Explain your changes.
Add array API support for top_k_accuracy_score
Keep the documented tie-breaking behavior (ties are resolved in favor of the highest index) by flipping an ascending stable argsort instead of using a stable descending sort. Replace np.setdiff1d in the labels validation with _isin, and use _average for the normalize and sample_weight handling, like accuracy_score does.
The labels parameter is now normalized with column_or_1d up front and its length measured with size(), because array API arrays are not required to implement len.
Register the metric in the array API common tests (multiclass, and binary with k=1) and document support.
First time contributor introduction
AI usage disclosure
I used AI assistance for: