File size: 702 Bytes
a39be45 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import numpy as np
def getAP(conf, labels):
assert len(conf) == len(labels)
sortind = np.argsort(-conf)
tp = labels[sortind] == 1
fp = labels[sortind] != 1
npos = np.sum(labels)
fp = np.cumsum(fp).astype("float32")
tp = np.cumsum(tp).astype("float32")
rec = tp / npos
prec = tp / (fp + tp)
tmp = (labels[sortind] == 1).astype("float32")
return np.sum(tmp * prec) / npos if npos > 0 else 1
def getClassificationMAP(confidence, labels):
""" confidence and labels are of dimension n_samples x n_label """
AP = []
for i in range(np.shape(labels)[1]):
AP.append(getAP(confidence[:, i], labels[:, i]))
return 100 * sum(AP) / len(AP)
|