My goal is to understand Average Precision at K
, and Recall at K
. I have two lists, one is predicted and other is actual (ground truth)
lets call these two lists as predicted and actual. Now I want to do precision@k
and recall@k
.
Using python I implemented Avg precision at K as follows:
def apk(actual, predicted, k=10):
"""
Computes the average precision at k.
This function computes the average precision at k between two lists of items.
Parameters
----------
actual: list
A list of elements that are to be predicted (order doesn't matter)
predicted : list
A list of predicted elements (order does matter)
k: int, optional
Returns
-------
score : double
The average precision at k over the input lists
"""
if len(predicted) > k:
predicted = predicted[:k]
score = 0.0
num_hits = 0.0
for i,p in enumerate(predicted):
if p in actual and p not in predicted[:i]:
num_hits += 1.0
score += num_hits / (i + 1.0)
if not actual:
return 1.0
if min(len(actual), k) == 0:
return 0.0
else:
return score / min(len(actual), k)
lets assume that our predicted has 5 strings in following order:
predicted = ['b','c','a','e','d'] and
actual = ['a','b','e']since we are doing @k would the precision@k is same as
recall@k? If not how would I do
recall@k`
If I want to do f-measure (f-score)
what would be the best route to do for above mention list?