diff --git a/Python/ml_metrics/average_precision.py b/Python/ml_metrics/average_precision.py index e18297d..ee77e03 100644 --- a/Python/ml_metrics/average_precision.py +++ b/Python/ml_metrics/average_precision.py @@ -1,10 +1,11 @@ import numpy as np + def apk(actual, predicted, k=10): """ Computes the average precision at k. - This function computes the average prescision at k between two lists of + This function computes the average precision at k between two lists of items. Parameters @@ -22,33 +23,34 @@ def apk(actual, predicted, k=10): The average precision at k over the input lists """ - if len(predicted)>k: + if len(predicted) > k: predicted = predicted[:k] score = 0.0 num_hits = 0.0 - for i,p in enumerate(predicted): + 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) + score += num_hits / (i + 1.0) if not actual: return 0.0 return score / min(len(actual), k) + def mapk(actual, predicted, k=10): """ Computes the mean average precision at k. - This function computes the mean average prescision at k between two lists + This function computes the mean average precision at k between two lists of lists of items. Parameters ---------- actual : list - A list of lists of elements that are to be predicted + A list of lists of elements that are to be predicted (order doesn't matter in the lists) predicted : list A list of lists of predicted elements @@ -62,4 +64,4 @@ def mapk(actual, predicted, k=10): The mean average precision at k over the input lists """ - return np.mean([apk(a,p,k) for a,p in zip(actual, predicted)]) + return np.mean([apk(a, p, k) for a, p in zip(actual, predicted)])