Skip to content
DeepForgeby svx
All problems
MediumNLPnlp-410

Average Precision at K

Problem

Average precision at k averages the precision at each rank where a relevant document appears, dividing the sum by the total number of relevant documents.

Given the binary relevance flags in ranked order and the total number of relevant documents, return average precision at k.

Hint

Precision is recomputed at every hit position.

Test cases

case 1
in: [[1,0,1],2,3]
expected: 0.8333333333333333
case 2
in: [[0,0],2,2]
expected: 0
case 3
in: [[1,1],2,2]
expected: 1
Starter codePython
def average_precision_at_k(relevance_flags, total_relevant, k):
    # Your code here
    pass

3 test cases · run your code against them in the editor