'calculate average precision
Params:
rec : numpy.array
cumulated recall
prec : numpy.array
cumulated precision
Returns:
ap as float'

def _average_precision(self, rec, prec):

    mrec = np.concatenate(([0.0], rec, [1.0]))
    mpre = np.concatenate(([0.0], prec, [0.0]))
    for i in range((mpre.size - 1), 0, (-1)):
        mpre[(i - 1)] = np.maximum(mpre[(i - 1)], mpre[i])
    i = np.where((mrec[1:] != mrec[:(-1)]))[0]
    ap = np.sum(((mrec[(i + 1)] - mrec[i]) * mpre[(i + 1)]))
    return ap
