From b678036ba1f221c3fa2fe74244f260924e978de1 Mon Sep 17 00:00:00 2001 From: pengju Date: Sun, 3 Jun 2018 19:16:46 +0800 Subject: [PATCH] when actual list zero lists, the apk should alwasy return 1.0; The current versio of code will always return 0.0 which is different from the code of function using pip install. Besides, put the code at first to skip unncessary operation. --- Python/ml_metrics/average_precision.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Python/ml_metrics/average_precision.py b/Python/ml_metrics/average_precision.py index e18297d..1f71e9e 100644 --- a/Python/ml_metrics/average_precision.py +++ b/Python/ml_metrics/average_precision.py @@ -22,6 +22,9 @@ def apk(actual, predicted, k=10): The average precision at k over the input lists """ + if not actual: + return 1.0 + if len(predicted)>k: predicted = predicted[:k] @@ -32,10 +35,6 @@ def apk(actual, predicted, k=10): if p in actual and p not in predicted[:i]: num_hits += 1.0 score += num_hits / (i+1.0) - - if not actual: - return 0.0 - return score / min(len(actual), k) def mapk(actual, predicted, k=10):