# Top K Accuracy Score

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

- Skill: `qhjqhj00/top-k-accuracy-score` (Agent Skill)
- Install (CLI): `npx skillmds add qhjqhj00/top-k-accuracy-score`
- Raw SKILL.md: https://api.skillmd.com/api/skills/qhjqhj00/top-k-accuracy-score/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: qhjqhj00 (https://skillmd.com/u/qhjqhj00)
- Updated: 2026-09-08
- Page: https://skillmd.com/skills/qhjqhj00/top-k-accuracy-score

---


# 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)`.

