induwara.lk
induwara.lkMachine Learning · Metrics

Top-k Accuracy Calculator

Paste a table of ranked predictions — or raw class scores — and read off top-1, top-5, and everything in between. Reports the exact hit count, a full top-1…top-k ladder, and the samples missed at k. Mirrors scikit-learn's top_k_accuracy_score. No signup, no upload.

By Induwara AshinsanaUpdated Jul 13, 2026
Top-k accuracy calculator

First value is the true label; the rest are the model's guesses, best-scoring first. Comma, tab, or space separated. Up to 50,000 rows.

k can't exceed 3 (the widest prediction list).

Examples
Top-3 accuracy
80.0%
4 / 5 hits
Top-1 accuracy
20.0%
1 / 5 · = ordinary accuracy
Hits at k=3
4
of 5 samples
Missed at k=3
1
true label outside top-k
Top-3 accuracy is 80.0% — the true label is inside the top 3 predictions for 4 of 5 samples.
  • 1 row(s) never list their true label among the predictions — those samples are missed at every k.

Top-1 … top-3 ladder

kAccuracyHitsMarginal gain
1= accuracy20.0%1 / 5
260.0%3 / 5+40.0%
3chosen80.0%4 / 5+20.0%

Missed at k = 3: 1 sample(s)

Row 4

These samples' true label falls outside the top 3 predictions — a good starting point for error analysis.

Computed entirely in your browser — nothing is uploaded. Definition per scikit-learn top_k_accuracy_score; membership cross-check passed; last verified 2026-07-13.

How it works

Top-k accuracyanswers one question about a classifier: how often is the correct label among the model's k highest-scoring guesses? A single sample counts as a hit at cutoff k exactly when its true label appears in the top k predictions. This is the definition scikit-learn implements in top_k_accuracy_score, and top-1 accuracy is identical to ordinary accuracy_score.

The metric became standard through the ImageNet Large Scale Visual Recognition Challenge (Russakovsky et al., 2015), where classifiers pick among 1,000 visually similar classes. Judging only the single top guess is harsh when a Siberian husky is labelled an Eskimo dog, so the challenge reported top-5 accuracyalongside top-1 — the number you still see quoted as “top-1 / top-5” on model cards today.

The calculation this tool runs is:

  1. Rank each sample. In ranked-labels mode the row is already in score order. In score-matrix mode the (class, score)pairs are sorted by score descending — stably, so tied scores keep header order, matching scikit-learn's argsort selection.
  2. Find the rank of the true label — the 1-based position where it first appears, or infinity if it never does.
  3. Score at k. The sample is a hit at k iff its rank ≤ k:top-k accuracy = (samples with rank ≤ k) / (total samples)The tool also exposes the raw hit count, which is what scikit-learn returns under normalize=False.
  4. Build the ladder.Because each sample's rank is computed once, accuracy at every cutoff from 1 to your chosen k is a single pass: accuracy@k′ is the fraction of samples with rank ≤ k′. The ladder is always monotonic non-decreasing — a wider top-k can only capture more true labels — so a drop would signal a parsing error and is flagged.

Two independent derivations back the result. The ladder counts hits from each sample's precomputed rank; a separate check re-counts them with a direct membership scan over the top-k slice. The “scikit-learn verified” badge lights only when the two agree on every rung. All of this runs client-side — your predictions never leave the browser.

Worked examples

Ranked-labels mode — 5 samples, k = 3

  1. cat, cat, dog, fox → true 'cat' at rank 1
  2. dog, cat, dog, wolf → 'dog' at rank 2
  3. fox, dog, cat, fox → 'fox' at rank 3
  4. wolf, cat, dog, fox → 'wolf' absent → rank ∞ (miss)
  5. cat, dog, cat, wolf → 'cat' at rank 2
  6. ranks = [1, 2, 3, ∞, 2]
  7. Top-1 = |rank ≤ 1| / 5 = 1 / 5 = 20.0%
  8. Top-2 = |rank ≤ 2| / 5 = 3 / 5 = 60.0%
  9. Top-3 = |rank ≤ 3| / 5 = 4 / 5 = 80.0% ← headline
  10. Missed at k=3: row 4. Ladder 20 → 60 → 80 is monotonic.

Score-matrix mode — classes [A, B, C, D], k = 2

  1. A, 0.5, 0.2, 0.2, 0.1 → ranked A,B,C,D → 'A' rank 1 (B,C tie 0.2)
  2. C, 0.4, 0.35, 0.15, 0.1 → ranked A,B,C,D → 'C' rank 3 → miss at k=2
  3. B, 0.1, 0.6, 0.2, 0.1 → ranked B,C,A,D → 'B' rank 1
  4. D, 0.25, 0.25, 0.2, 0.3 → ranked D,A,B,C → 'D' rank 1 (A,B tie 0.25)
  5. ranks = [1, 3, 1, 1]
  6. Top-1 = 3 / 4 = 75.0%
  7. Top-2 = 3 / 4 = 75.0% ← headline (row 2 needs k=3)
  8. Top-3 = 4 / 4 = 100.0%
  9. Row 1 has a score tie exactly at the k=2 boundary — surfaced, but harmless since 'A' is already rank 1.

Edge case — true label never predicted (k = 5)

  1. cat, dog, fox, wolf → true 'cat' absent → rank ∞
  2. A row whose true label is missing is a miss at every k, k=1 through k=5.
  3. It lowers the denominator's worth of accuracy and appears in the missed-at-k list.
  4. Rows shorter than k are still scored — a hit only if the true label is among the predictions present.

Frequently asked questions

Sources & references

The definition on this page was last cross-checked against the scikit-learn documentation on 2026-07-13. This is a fixed mathematical definition that does not change over time; the result is verified by computing hits two independent ways and confirming they agree.

Related tools

Rate this tool
Be the first to rate

Comments & feedback

Spotted a bug or want an improvement? Tell us — our team reviews every comment, and good ideas get built. Comments are public and anonymous.

Found a bug, edge case, or want to suggest an improvement?

Email me at [email protected] — most fixes ship within 24 hours.