(y_true, y_score, sample_weight=None)
return out
Compute ranking-based average precision

    Parameters
    ----------
    y_true : array or sparse matrix, shape = [n_samples, n_labels]
        True binary labels in binary indicator format.

    y_score : array, shape = [n_samples, n_labels]
        Target scores, can either be probability estimates of the positive
        class, confidence values, or non-thresholded measure of decisions
        (as returned by "decision_function" on some classifiers).

    sample_weight : array-like of shape = [n_samples], optional
        Sample weights.

    Returns
    -------
    score : float
Compute ranking-based average precision
Compute ranking - based average precision
def label_ranking_average_precision_score(y_true, y_score, sample_weight=None):
    """Compute ranking-based average precision

    Parameters
    ----------
    y_true : array or sparse matrix, shape = [n_samples, n_labels]
        True binary labels in binary indicator format.

    y_score : array, shape = [n_samples, n_labels]
        Target scores, can either be probability estimates of the positive
        class, confidence values, or non-thresholded measure of decisions
        (as returned by "decision_function" on some classifiers).

    sample_weight : array-like of shape = [n_samples], optional
        Sample weights.

    Returns
    -------
    score : float

    """
    check_consistent_length(y_true, y_score, sample_weight)

    if y_true.shape != y_score.shape:
        raise ValueError("y_true and y_score have different shape")

    # Handle badly formatted array and the degenerate case with one label
    y_type = type_of_target(y_true)
    if (y_type != "multilabel" 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.
    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):
            # If all labels are relevant or unrelevant, the score is also
            # equal to 1. The label ranking has no meaning.
            out += 1.
            continue

        scores_i = y_score[i]
        rank = rankdata(scores_i, 'max')[relevant]
        L = rankdata(scores_i[relevant], 'max')
        aux = (L / rank).mean()
        if sample_weight is not None:
            aux = aux * sample_weight[i]
        out += aux

    if sample_weight is None:
        out /= n_samples
    else:
        out /= np.sum(sample_weight)

    return out
def label_ranking_average_precision_score ( y_true , y_score , sample_weight = None ) : check_consistent_length ( y_true , y_score , sample_weight ) if y_true . shape != y_score . shape : raise ValueError ( "y_true and y_score have different shape" ) # Handle badly formatted array and the degenerate case with one label y_type = type_of_target ( y_true ) if ( y_type != "multilabel" 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. 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 ) : # If all labels are relevant or unrelevant, the score is also # equal to 1. The label ranking has no meaning. out += 1. continue scores_i = y_score [ i ] rank = rankdata ( scores_i , 'max' ) [ relevant ] L = rankdata ( scores_i [ relevant ] , 'max' ) aux = ( L / rank ) . mean ( ) if sample_weight is not None : aux = aux * sample_weight [ i ] out += aux if sample_weight is None : out /= n_samples else : out /= np . sum ( sample_weight ) return out