All problems
Top-K Accuracy
Problem
Compute top-k accuracy: the fraction of samples whose true label appears among the k classes with the highest predicted probability.
Ties in probability are ordered by the smallest class index. If k is at least the number of classes, every sample counts as correct. Empty input returns 0.0.
Hint
Sort classes by (-probability, index) and check the first k entries.
Test cases
case 1
in: [[0,1,2],[[0.7,0.2,0.1],[0.1,0.6,0.3],[0.2,0.3,0.5]],1]
expected: 1
case 2
in: [[2,0],[[0.5,0.4,0.1],[0.6,0.3,0.1]],2]
expected: 0.5
case 3
in: [[2,0],[[0.5,0.4,0.1],[0.6,0.3,0.1]],3]
expected: 1
case 4
in: [[1],[[0.5,0.5,0]],1]
expected: 0
Starter codePython
def top_k_accuracy(y_true, y_pred_probs, k):
# Your code here
pass4 test cases · run your code against them in the editor