def label_ranking_average_precision_score(y_true, y_score):
 check_consistent_length(y_true, y_score)
 y_true = check_array(y_true, ensure_2d=False)
 y_score = check_array(y_score, ensure_2d=False)
 if (y_true.shape != y_score.shape):
  raise ValueError('y_true and y_score have different shape')
 y_type = type_of_target(y_true)
 if ((y_type != 'multilabel-indicator') and (not ((y_type == 'binary') and (y_true.ndim == 2)))):
  raise ValueError('{0} format is not supported'.format(y_type))
 y_true = csr_matrix(y_true)
 y_score = (- y_score)
 (n_samples, n_labels) = y_true.shape
 out = 0.0
 for (i, (start, stop)) in enumerate(zip(y_true.indptr, y_true.indptr[1:])):
  relevant = y_true.indices[start:stop]
  if ((relevant.size == 0) or (relevant.size == n_labels)):
   out += 1.0
   continue
  scores_i = y_score[i]
  rank = rankdata(scores_i, 'max')[relevant]
  L = rankdata(scores_i[relevant], 'max')
  out += (L / rank).mean()
 return (out / n_samples)

def label_ranking_average_precision_score ( y_true , y_score ) : check_consistent_length ( y_true , y_score ) y_true = check_array ( y_true , ensure_2d = False ) y_score = check_array ( y_score , ensure_2d = False ) if ( y_true . shape != y_score . shape ) : raise ValueError ( 'y_true and y_score have different shape' ) y_type = type_of_target ( y_true ) if ( ( y_type != 'multilabel-indicator' ) and ( not ( ( y_type == 'binary' ) and ( y_true . ndim == 2 ) ) ) ) : raise ValueError ( '{0} format is not supported' . format ( y_type ) ) y_true = csr_matrix ( y_true ) y_score = ( - y_score ) ( n_samples , n_labels ) = y_true . shape out = 0.0 for ( i , ( start , stop ) ) in enumerate ( zip ( y_true . indptr , y_true . indptr [ 1 : ] ) ) : relevant = y_true . indices [ start : stop ] if ( ( relevant . size == 0 ) or ( relevant . size == n_labels ) ) : out += 1.0 continue scores_i = y_score [ i ] rank = rankdata ( scores_i , 'max' ) [ relevant ] L = rankdata ( scores_i [ relevant ] , 'max' ) out += ( L / rank ) . mean ( ) return ( out / n_samples )
compute ranking-based average precision label ranking average precision is the average over each ground truth label assigned to each sample .