Skip to content
Back to skills

Top K Accuracy Score

ASecurity

Compute the top_k_accuracy_score metric — provided by scikit-learn. Use when the user has predictions and ground-truth and needs to compute top_k_accuracy_score, or asks how to score with top_k_accuracy_score.

  • 3 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added September 11, 2026
documentationpythonperformance

Security analysis

A100/100

Scanned September 11, 2026

npx -y skills add qhjqhj00/research-skills-pool --skill top-k-accuracy-score --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Top K Accuracy Score?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Top K Accuracy Score
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/qhjqhj00-top-k-accuracy-score/badge)](https://www.skillsdirectory.com/skills/qhjqhj00-top-k-accuracy-score)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: top-k-accuracy-score
description: Compute the top_k_accuracy_score metric — provided by scikit-learn. Use when the user has predictions and ground-truth and needs to compute top_k_accuracy_score, or asks how to score with top_k_accuracy_score.
metadata:
  skill_kind: metric
  source_lib: scikit-learn
  import_path: sklearn.metrics.top_k_accuracy_score
  source: library_introspection
---

# top-k-accuracy-score

> Metric `top_k_accuracy_score` from `scikit-learn` (sklearn.metrics.top_k_accuracy_score)

## When to invoke this skill

The user has predictions + ground truth and asks to evaluate with top_k_accuracy_score, or
mentions `sklearn.metrics.top_k_accuracy_score` directly, or wants the standard scikit-learn implementation.

## Reference signature

```python
from sklearn.metrics import top_k_accuracy_score

# top_k_accuracy_score(y_true, y_score, *, k=2, normalize=True, sample_weight=None, labels=None)
```

## Library docstring

```
Top-k Accuracy classification score.

This metric computes the number of times where the correct label is among
the top `k` labels predicted (ranked by predicted scores). Note that the
multilabel case isn't covered here.

Read more in the :ref:`User Guide <top_k_accuracy_score>`

Parameters
----------
y_true : array-like of shape (n_samples,)
    True labels.

y_score : array-like of shape (n_samples,) or (n_samples, n_classes)
    Target scores. These can be either probability estimates or
    non-thresholded decision values (as returned by
    :term:`decision_function` on some classifiers).
    The binary case expects scores with shape (n_samples,) while the
    multiclass case expects scores with shape (n_samples, n_classes).
    In the multiclass case, the order of the class scores must
    correspond to the order of ``labels``, if provided, or else to
    the numerical or lexicographical order of the labels in ``y_true``.
    If ``y_true`` does not contain all the labels, ``labels`` must be
    provided.

k : int, default=2
    Number of most likely outcomes considered to find the correct label.

normalize : bool, default=True
    If `True`, return the fraction of correctly classified samples.
    Otherwise, return the number of correctly classified samples.

sample_weight : array-like of shape (n_samples,), default=None
    Sample weights. If `None`, all samples are given the same weight.

labels : array-like of shape (n_classes,), default=None
    Multiclass only. List of labels that index the classes in ``y_score``.
    If ``None``, the numerical or lexicographical order of the labels in
    ``y_true`` is used. If ``y_true`` does not contain all the labels,
    ``labels`` must be provided.

Returns
-------
score : float
    The top-k accuracy score. The best performance is 1 with
    `normalize == True` and the number of samples with
    `normalize == False`.

See Also
--------
accuracy_score : Compute the accuracy score. By default, the function will
    return the fraction of correct predictions divided by the total number
    of predictions.

Notes
-----
In cases where two or more labels are assigned equal predicted scores,
the labels with the highest
```

## Quick recipe

```python
import sklearn.metrics as _m
score = _m.top_k_accuracy_score(y_true, y_pred)
```

## Don'ts

- Don't reimplement when the library version handles edge cases (NaN, ties, empty inputs) better than a hand-rolled formula.
- Always check the library version's argument order — sklearn is `(y_true, y_pred)` while torchmetrics is `(preds, target)`.

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.