def _average_precision(y_true, y_score):
 pos_label = np.unique(y_true)[1]
 n_pos = np.sum((y_true == pos_label))
 order = np.argsort(y_score)[::(-1)]
 y_score = y_score[order]
 y_true = y_true[order]
 score = 0
 for i in range(len(y_score)):
  if (y_true[i] == pos_label):
   prec = 0
   for j in range(0, (i + 1)):
    if (y_true[j] == pos_label):
     prec += 1.0
   prec /= (i + 1.0)
   score += prec
 return (score / n_pos)

def _average_precision ( y_true , y_score ) : pos_label = np . unique ( y_true ) [ 1 ] n_pos = np . sum ( ( y_true == pos_label ) ) order = np . argsort ( y_score ) [ : : ( - 1 ) ] y_score = y_score [ order ] y_true = y_true [ order ] score = 0 for i in range ( len ( y_score ) ) : if ( y_true [ i ] == pos_label ) : prec = 0 for j in range ( 0 , ( i + 1 ) ) : if ( y_true [ j ] == pos_label ) : prec += 1.0 prec /= ( i + 1.0 ) score += prec return ( score / n_pos )
alternative implementation to check for correctness of average_precision_score .