Skip to content

Instantly share code, notes, and snippets.

@qfgaohao
Last active October 20, 2017 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qfgaohao/f8987b8f895480e61ad030dc30b0a0c2 to your computer and use it in GitHub Desktop.
Save qfgaohao/f8987b8f895480e61ad030dc30b0a0c2 to your computer and use it in GitHub Desktop.
It computes average precision based on the definition of Pascal Competition. It computes the under curve area of precision and recall. Recall follows the normal definition. Precision is a variant. pascal_precision[i] = typical_precision[i:].max()
import numpy as np
def compute_average_precision(test_results, num_true_cases):
"""
It computes average precision based on the definition of Pascal Competition. It computes the under curve area
of precision and recall. Recall follows the normal definition. Precision is a variant.
pascal_precision[i] = typical_precision[i:].max()
Usage:
test_results = np.array([True, False, True, False, True, False, False, False, False, True])
compute_average_precision(test_results, 8) # 0.33
test_results = np.array([1] * 10)
compute_average_precision(test_results, 8) # 1
test_results = np.array([0] * 10)
compute_average_precision(test_results, 8) # 0
Arguments:
test_results (np.ndarray): test results, i.e [True, False, True, False, False] or [1, 0, 1, 0, 0].
num_true_cases (int): the number of total true cases.
Returns:
average_precision score (float)
"""
true_positive = test_results.cumsum()
false_positive = (~test_results).cumsum()
precision = true_positive / (true_positive + false_positive)
recall = true_positive / num_true_cases
# identical but faster version of new_precision[i] = old_precision[i:].max()
precision = np.concatenate([[0.0], precision, [0.0]])
for i in range(len(precision) - 1, 0, -1):
precision[i - 1] = np.maximum(precision[i - 1], precision[i])
# find the index where the value changes
recall = np.concatenate([[0.0], recall, [1.0]])
changing_points = np.where(recall[1:] != recall[:-1])[0]
# compute under curve area
areas = (recall[changing_points + 1] - recall[changing_points]) * precision[changing_points + 1]
return areas.sum()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment