chrischoy/SpatioTemporalSegmentation

average_precision for binary classification

amineremache opened this issue · 0 comments

Hello,

The function test.py/average_precision uses label_binarize from sklearn.preprocessing, and if you read the documentation you can see that it explicitly says :
Shape will be [n_samples, 1] for binary problems.

So you get this error:

image

Because since the output of this function will be used to calculate the average precision, the target and predictions must have the same shape ( Nx2 ).

Here is a "quick" solution that I don't like a lot (basically creating a dummy class), so if anyone could give a better one that would be good:

def average_precision(prob_np, target_np):
num_class = prob_np.shape[1]
if num_class == 2:
num_class += 1
label = label_binarize(target_np, classes=list(range(num_class)))
with np.errstate(divide='ignore', invalid='ignore'):
return average_precision_score(label, prob_np, None)

And also, if you think that I am mistaken, please let me know, feedback is always welcomed.