Top-K Accuracy Check
You are given a list of prediction scores for each class and need to determine if the true class is among the top-k highest-scoring predictions.
Top-k accuracy is a relaxed metric commonly used in image classification benchmarks like ImageNet, where top-5 accuracy is reported alongside top-1.
The algorithm:
- Rank all classes by their prediction scores (descending)
- Take the top k classes with highest scores
- Check if the true class is in this top-k set
This is more forgiving than top-1 accuracy, especially for fine-grained classification where similar classes may be confused.
Example:
scores = [0.1, 0.5, 0.3, 0.1] true_class = 2 k = 2
True
- Sort classes by score (descending):
- Class 1: 0.5 (rank 1)
- Class 2: 0.3 (rank 2)
- Class 0: 0.1 (rank 3)
- Class 3: 0.1 (rank 4)
- Top-2 classes are: [1, 2]
- true_class = 2 is in the top-2 set
- Return True
Constraints:
- scores is a list of prediction scores for each class (higher = more likely)
- true_class is the index of the correct class
- k is a positive integer (1 <= k <= len(scores))
- Return True if true_class is in top-k predictions, False otherwise
You want to check whether the true class index is within the top-k highest scores in a prediction vector.
1. Background Knowledge
In a multi-class image classification task, a model outputs a score (often a logit or probability) for each possible class. For an input image, you might get a vector like:
scores=[s0,s1,…,sC−1]where C is the number of classes, and si is how confident the model is that the image belongs to class i.
Top-1 accuracy checks whether the single highest-scoring class matches the true class label. This can be strict: if the correct class is the second highest score, it’s counted as wrong. Top-k accuracy relaxes this: it is counted as correct if the true class is among the k highest-scoring classes. This is especially useful in fine-grained or large-label-space tasks (like ImageNet) where several classes are visually similar, so “the right answer is in the top few guesses” is still informative.
2. Algorithm / General Approach
The pattern for a single example:
- Rank classes by their prediction scores in descending order.
- Select the top k class indices.
- Check membership: return true/1 if the true class index is in this set; otherwise false/0.
For a batch, you repeat this per example and average the results to get top-k accuracy.
3. Step-by-Step Strategy
Assume:
- scores is a list/array of length n_classes.
- true_idx is the integer index of the true class.
- k is the integer for “top-k”.
Step-by-step:
-
Pair scores with indices Create pairs (score, index) so you remember which class each score belongs to.
-
Sort by score descending Sort the pairs by score from largest to smallest.
-
Take top-k indices
- Slice the first k elements after sorting.
- Extract their class indices into something like top_indices.
- Check if true class is included
- Check true_idx in top_indices.
- If yes, the prediction is top-k correct; otherwise it’s incorrect.
In code form (Python-like, conceptual):
def is_top_k_correct(scores, true_idx, k):
# 1. pair scores with indices
indexed = list(enumerate(scores)) # [(0, s0), (1, s1),...]
# 2. sort by score descending
indexed.sort(key=lambda x: x, reverse=True)
# 3. take top-k indices
top_k_indices = [idx for idx, _ in indexed[:k]]
# 4. membership check
return true_idx in top_k_indices
4. Common Pitfalls
- Using ascending sort by mistake: always sort descending so highest scores come first.
- Off-by-one with k:
- Ensure k >= 1 and typically k <= number_of_classes.
- If k is larger than the number of classes, you should handle or clamp it.
- Confusing class labels vs indices:
- Make sure true_idx corresponds to the position of the class in the scores array, not some arbitrary label id unless you have mapped it.
- Floating-point ties:
- Rarely critical for simple problems; just rely on the sort’s tie-breaking.
- Inefficient full sort for large n:
- For very large numbers of classes, you can use a partial selection (like a max-heap or nth_element / nlargest) to get top-k without fully sorting. For an “Easy” problem, a full sort is fine.
5. Time & Space Complexity
Let C be the number of classes.
-
Full sort approach:
-
Time: O(ClogC) (sorting all class scores).
-
Space: O(C) for storing the paired (index, score) or a copy.
-
Using selection/top-k algorithms (optional optimization):
-
Time: O(Clogk) using a size-k heap, or average O(C) with specialized selection.
-
Space: O(C) or O(k), depending on implementation.
For typical small/medium C in an Easy-level problem, O(ClogC) with a simple sort is completely acceptable and easiest to implement.