Before you start
Score a model by whether the true class appears anywhere in its top k
predictions.
Given scores of shape (n, num_classes) and labels of shape (n,), a row
counts as correct when labels[i] is among the k highest scoring classes for
that row. Return the fraction of rows that are correct.
k = 1 is ordinary accuracy. Top-5 became the standard ImageNet metric because
with a thousand classes, many of them near-duplicates, "the right answer was in
the model's shortlist" is often the question you actually care about.
np.argsort gives you the ordering; the top k are the last k entries of
an ascending sort. np.argpartition does the same job in O(n) per row when
you do not need the top k themselves ordered.
Ties are broken by whatever the sort produces, which is fine here: with real valued scores exact ties are vanishingly rare, and the tests avoid them.
def top_k_accuracy(scores, labels, k):
Input
scores = np.array([[0.1, 0.5, 0.4], [0.7, 0.2, 0.1]])
labels = np.array([2, 1])
top_k_accuracy(scores, labels, k=2)
Output
1.0
Reasoning
Row 0's top 2 classes are 1 and 2, which include label 2. Row 1's top 2 are 0 and 1, which include label 1. Both rows count, so the result is 1.0.
k = 1 is plain accuracy.Check runs the visible cases against a fresh kernel. Submit runs the hidden ones too.
⇧↵run cell