All problems
Precision at K
Problem
Compute precision at k for a ranked binary relevance list: relevant items in the first min(k, n) positions divided by the number of positions used.
Return 0.0 if k <= 0 or the list is empty.
Hint
Precision@k measures how many of the top k recommendations are relevant.
Test cases
case 1
in: [[1,0,1,1,0],2]
expected: 0.5
case 2
in: [[1,0,1,1,0],3]
expected: 0.6666666666666666
case 3
in: [[1,0],0]
expected: 0
case 4
in: [[],2]
expected: 0
Starter codePython
def precision_at_k(relevant, k):
# Your code here
pass4 test cases · run your code against them in the editor