Thanks to visit codestin.com
Credit goes to github.com

Skip to content

FEA Add array API support for top_k_accuracy_score#34425

Draft
Fazel94 wants to merge 3 commits into
scikit-learn:mainfrom
Fazel94:feat/array-api-top-k-accuracy-score
Draft

FEA Add array API support for top_k_accuracy_score#34425
Fazel94 wants to merge 3 commits into
scikit-learn:mainfrom
Fazel94:feat/array-api-top-k-accuracy-score

Conversation

@Fazel94

@Fazel94 Fazel94 commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

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:

  • Code generation
  • Research and understanding

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
@Fazel94 Fazel94 force-pushed the feat/array-api-top-k-accuracy-score branch from 49c112b to 0cf11e2 Compare July 2, 2026 22:09
Fazel94 added 2 commits July 3, 2026 12:33
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant