Skip to content

Commit

Permalink
Merge pull request #248 from NatLibFi/issue247-precision-nan
Browse files Browse the repository at this point in the history
Avoid returnin nan values for Precision@K
  • Loading branch information
osma authored Jan 29, 2019
2 parents 3804bf9 + 03571b0 commit d8a4d26
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 d8a4d26

Please sign in to comment.