Top-K accuracy
Before you start
You need nothing else first. This unit starts from scratch.
The idea in one paragraph
Some systems do not give one answer. They give a ranked list of candidates, best first. For each case, find the right answer's position in the list, its rank. Top-K accuracy is the share of cases where that rank is K or better, that is, where the right answer appears among the first K entries. Top-1 accuracy is ordinary accuracy. A larger K is easier to meet, so top-K accuracy can only stay the same or rise as K grows.
A case counts as a hit when the right answer is in the first K
Picture an app that names a bird from a photo. It shows its six best guesses, most likely first. If the right bird is first, the app was right outright. If the right bird is third, a birdwatcher who reads the top three still finds it. Top-K accuracy measures how often a reader who checks the first K guesses finds the right one.
To compute it, take a set of test cases where you know the right answer. For each case:
- Find the rank of the right answer in the list: 1 if it is first, 2 if it is second, and so on.
- Call the case a hit if the rank is K or less.
Top-K accuracy is the number of hits divided by the number of cases.
Predict first. In the five cases below, the right answer has ranks 1, 3, 2, 1 and 6. Guess the top-1 and top-3 accuracy before you read on.
The ranks of the right bird are 1, 3, 2, 1 and 6.
- Top-1: ranks 1 and 1 are hits. 2 of 5 = 40%.
- Top-3: ranks 1, 3, 2 and 1 are hits. 4 of 5 = 80%.
- Top-6: every rank is 6 or less. 5 of 5 = 100%.
| K | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|
| Top-K accuracy | 40% | 60% | 80% | 80% | 80% | 100% |
Going from K = 3 to K = 5 gains nothing here, because no right answer sits at rank 4 or 5.
Try it. Find the smallest K that gives 100%. Then find two values of K that give the same accuracy, and say why.
Pick K to match how many guesses someone will check
Top-K accuracy is useful when a person, or a later step in a program, looks at a short list rather than one answer. Then the honest question is "how often is the right answer on the list they will see?" A bigger K finds more right answers, but someone must check more candidates per case.
Compare against chance. If a system put N candidates in random order, the right answer would land in the first K with probability K / N. With 100 candidates, random order gives a top-10 accuracy of about 10%. A useful system beats that by a wide margin.
Ties. If two candidates have the same score, their order is a matter of choice, and the right answer's rank depends on it. The code below breaks ties in the right answer's favor; other code may not.
A missing answer. If the right answer is not among the candidates at all, the case is a miss for every K. Then top-K accuracy stays below 100% even when K is the whole list.
Several right answers. Some tasks have more than one right answer per case. Then you must say what counts as a hit, such as "at least one right answer in the first K".
Readers who know a little Python (see Reading Python) can compute ranks from a system's scores. The rank of the right answer is 1 plus the number of candidates that score higher.
topk.py
import numpy as np
# Each row holds one case's scores for 6 candidates.
scores = np.array([[0.9, 0.1, 0.3, 0.2, 0.0, 0.4],
[0.2, 0.8, 0.1, 0.7, 0.3, 0.6],
[0.1, 0.3, 0.2, 0.9, 0.4, 0.0]])
right = np.array([0, 5, 4]) # the index of the right candidate
# Rank = 1 + how many candidates score higher than the right one.
# np.arange(len(right)) gives the row numbers [0, 1, 2]. Paired with right,
# they pick scores[0, 0], scores[1, 5] and scores[2, 4]: each case's right score.
right_score = scores[np.arange(len(right)), right]
# right_score[:, None] stands the 3 scores in a column, so each row is compared
# with its own right score. sum(axis=1) counts the True results in each row.
rank = 1 + (scores > right_score[:, None]).sum(axis=1)
print(rank)
for k in (1, 2, 3):
# rank <= k means "rank at most k". The mean counts True as 1 and False as 0.
print(k, round((rank <= k).mean(), 2))
[1 3 2] 1 0.33 2 0.67 3 1.0
Check yourself
-
Four cases have the right answer at ranks 2, 1, 4 and 1. What are the top-1, top-2 and top-4 accuracy?
Answer
Top-1: 2 of 4 = 50%. Top-2: 3 of 4 = 75%. Top-4: 4 of 4 = 100%.
-
A report says a system has 70% top-1 accuracy and 60% top-3 accuracy on the same cases. What is wrong?
Answer
It cannot happen. Every top-1 hit is also a top-3 hit, so top-3 accuracy is at least the top-1 accuracy.
-
A system ranks 50 candidates per case. Its top-5 accuracy is 12%. Is it doing better than random order?
Answer
Only a little. Random order gives about 5 / 50 = 10%.
Go further
Optional background, for readers who want more than this page.
-
The formula and a ready-made function, with a short example; useful when you compute it in Python.
Where this shows up
No unit or explanation builds on this one yet.