Skip to content

Commit

Permalink
Avoid returnin nan values for Precision@K. Fixes #247
Browse files Browse the repository at this point in the history
  • Loading branch information
osma committed Jan 29, 2019
1 parent 3804bf9 commit 03571b0
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions annif/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ def precision_at_k_score(y_true, y_pred, limit):
"""calculate the precision at K, i.e. the number of relevant items
among the top K predicted ones"""
scores = []
origlimit = limit
for true, pred in zip(y_true, y_pred):
order = pred.argsort()[::-1]
limit = min(limit, np.count_nonzero(pred))
order = order[:limit]
orderlimit = min(limit, np.count_nonzero(pred))
order = order[:orderlimit]
gain = true[order]
scores.append(gain.sum() / limit)
if orderlimit > 0:
scores.append(gain.sum() / orderlimit)
else:
scores.append(0.0)
return statistics.mean(scores)


Expand Down

0 comments on commit 03571b0

Please sign in to comment.