title: Mean average precision - python
body: <p>I am calculating mean average precision at top k retrieve objects. Here is my code. At this stage, I am computing <em>R@K</em>. 
  The code read the two lists from <code>csv</code> files, then take a sample from a list, compute euclidean distance with all samples from other list, sort them and finally take top k objects to see if the object is available in the retrieved samples.  </p>
  
  <pre><code>import csv
  from scipy.spatial import distance
  from sklearn.utils import shuffle
  from numpy import dot
  from numpy.linalg import norm
  from sklearn.preprocessing import StandardScaler  
  import numpy as np
  from numpy import array
  
  
  def parse_features_from_csv(csv_file):
      feat_lst = []
      id_lst = []
      row_lst = []
      with open(csv_file) as fr:
          reader = csv.reader(fr, delimiter=',')
          for row in reader:
              s_feat = row[:-1]
              identifier = row[-1]
              s_feat = [float(i) for i in s_feat]
              feat_lst.append(s_feat)
              id_lst.append(identifier)
              row_lst.append(row[-1])
      return feat_lst, id_lst,row_lst
  
  
  def compute_distances(et_item, feat_lst, id_lst):
      dist_list = []
      for id_img_item, img_item in enumerate(feat_lst):
          dist = distance.euclidean(img_item,et_item)
          #print (dist)
          dist_list.append((id_lst[id_img_item], dist))
      return dist_list
  
  
  def main():
      top_k = 10
      feat_file = "list_1.csv"
      test_file = "list_2.csv"
      et_feat_lst, et_id_list, _ = parse_features_from_csv(test_file)
      feat_list, id_list,row_lst_et = parse_features_from_csv(feat_file)
  
  
  
      print (len(feat_list))
      print (len(et_feat_lst))
  
  
  
  
      correct = 0
      for id_et_item, et_item in enumerate(et_feat_lst):
          distances = compute_distances(et_item, feat_list, row_lst_et)
          sort_dst = sorted(distances, key=lambda x: x[1])
  
          #print("Target: " + et_id_list[id_et_item] + ", Distances: " + str(sort_dst[:top_k]))
  
          eucl_dist = sort_dst[:top_k]
          gt = et_id_list[id_et_item]
          for idx in eucl_dist:
              tar = idx[0]        
              if gt == tar:
                  correct+= 1
                  break
  
          print ("correct", str(correct) + '/' +  str(id_et_item))
  
  
  if __name__ == '__main__':
      main()
  </code></pre>
  
  <p>Can some one tell how I can use <code>sklearn.metrics.average_precision_score</code>
  function to compute Mean average precision for top K retrieved objects.
  I have confusion related to <code>(y_true, y_scores)</code> . I would appreciate if someone can explain these two parametres of the function. </p>