'How to calculate precision at k
I have doubts about the use of this metric. I would like to know I use it in the right way by offering this example, which is then very similar to the real scenario. I have a set of 10 processes in an operating system, on which I have conducted analysis. The analysis gives me a ranking of these processes, for example:
process2
process4
process7
process1
process9
process8
process5
process3
process10
process6
Suppose the experimental results, on the other hand, give me this ranking, which therefore constitutes my ground truth.
process2
process4
process1
process7
process8
process5
process9
process3
process6
process10
Suppose we want to compute p@1, p@3 and p@5. From what I understand, to calculate p@1, just compare the first two elements of the two rankings. In my case they coincide, so p@1=1. If they had been different, the value would have been zero. To calculate p@3, you have to count how many items in the top 3 of my ranking appear in the top 3 items of the real ranking? In this case, p@3=2/3? And p@5=3/5? In general, to calculate p@k, should I count how many elements among the first k in my ranking appear among the first k in ground truth and divide by k?
I have also implemented this function in Python, which works as I have described the problem. But I'm not sure that the precision at k works as I understand it.
def precision_at_k(y_pred,y_score,k):
count = 0
for i in range(0,k):
j=0
while(j<k):
if(y_pred[i] == y_score[j]):
count += 1
j += 1
return count/k
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
