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?
Just taking a look at the sklearn
docs
Parameters:
y_true : array, shape = [n_samples] or [n_samples, n_classes] True binary labels in binary label indicators.
y_score : array, shape = [n_samples] or [n_samples, n_classes] Target scores, can either be probability estimates of the positive class, confidence values, or non-thresholded measure of decisions (as returned by “decision_function” on some classifiers).
So your first argument has to be an array of binary labels, but you are passing some sort of float array as the first argument. So I believe you need to reverse the order of the arguments you are passing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With