code: def average_precision(ranking):
      """
      Score is average precision (area under PR curve). Relevance is binary (nonzero is relevant).
  
      :param ranking: Relevance scores (list or numpy) in rank order (first element is the first item)
      :type ranking: list, np.array
  
      :return: Average precision
      :rtype: float
  
      """
  
      ranking = np.asarray(ranking) != 0
      out = [
          precision_at_k(ranking, k + 1)
          for k in range(ranking.size)
          if ranking[k]
      ]
      if not out:
          return 0.0
      return np.mean(out)
  
imports: import numpy
label_desc: Use image node_id 2 for calling a global function with example usage: average_precision(ranking) and returns: np, int