I have written a simple function where I am using the average_precision_score from scikit-learn
to compute average precision.
My Code:
def compute_average_precision(predictions, gold):
gold_predictions = np.zeros(predictions.size, dtype=np.int)
for idx in range(gold):
gold_predictions[idx] = 1
return average_precision_score(predictions, gold_predictions)
When the function is executed, it produces the following error.
Traceback (most recent call last):
File "test.py", line 91, in <module>
total_avg_precision += compute_average_precision(np.asarray(probs), len(gold_candidates))
File "test.py", line 29, in compute_average_precision
return average_precision_score(predictions, gold_predictions)
File "/if5/wua4nw/anaconda3/lib/python3.5/site-packages/sklearn/metrics/ranking.py", line 184, in average_precision_score
average, sample_weight=sample_weight)
File "/if5/wua4nw/anaconda3/lib/python3.5/site-packages/sklearn/metrics/base.py", line 81, in _average_binary_score
raise ValueError("{0} format is not supported".format(y_type))
ValueError: continuous format is not supported
If I print the two numpy arrays predictions
and gold_predictions
, say for one example, it looks alright. [One example is provided below.]
[ 0.40865014 0.26047812 0.07588802 0.26604077 0.10586583 0.17118802
0.26797949 0.34618672 0.33659923 0.22075308 0.42288553 0.24908153
0.26506338 0.28224747 0.32942101 0.19986877 0.39831917 0.23635269
0.34715138 0.39831917 0.23635269 0.35822859 0.12110706]
[1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
What I am doing wrong here? What is the meaning of the error?
predictions
represent? Are they outputs of the predict() method of some estimator or do they represent the probability of getting the positive class, or maybe output ofpredict_proba()
? Anyways,y_true
or yourgold_predictions
need to be the first argument andpredictions
second. – Guzel