(y_true, y_score, sample_weight=None)
return np.average(loss, weights=sample_weight)
Compute Ranking loss measure.
    
    Compute the average number of label pairs that are incorrectly ordered
    given y_score weighted by the size of the label set and the number of
    labels not in the label set.

    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
    -------
    loss : float

    References
    ----------
    .. [1] Tsoumakas, G., Katakis, I., & Vlahavas, I. (2010).
           Mining multi-label data. In Data mining and knowledge discovery
           handbook (pp. 667-685). Springer US.
Compute Ranking loss measure.
    
    Compute the average number of label pairs that are incorrectly ordered
    given y_score weighted by the size of the label set and the number of
    labels not in the label set.
Compute Ranking loss measure . Compute the average number of label pairs that are incorrectly ordered given y_score weighted by the size of the label set and the number of labels not in the label set .
def label_ranking_loss(y_true, y_score, sample_weight=None):
    """Compute Ranking loss measure.
    
    Compute the average number of label pairs that are incorrectly ordered
    given y_score weighted by the size of the label set and the number of
    labels not in the label set.

    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
    -------
    loss : float

    References
    ----------
    .. [1] Tsoumakas, G., Katakis, I., & Vlahavas, I. (2010).
           Mining multi-label data. In Data mining and knowledge discovery
           handbook (pp. 667-685). Springer US.
    """
    check_consistent_length(y_true, y_score, sample_weight)

    y_type = type_of_target(y_true)
    if y_type not in ("multilabel",):
        raise ValueError("{0} format is not supported".format(y_type))

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

    n_samples, n_labels = y_true.shape

    y_true = csr_matrix(y_true)

    loss = np.zeros(n_samples)
    for i, (start, stop) in enumerate(zip(y_true.indptr, y_true.indptr[1:])):
        # Sort and bin the label scores
        unique_scores, unique_inverse = np.unique(y_score[i],
                                                  return_inverse=True)
        true_at_reversed_rank = np.bincount(
            unique_inverse[y_true.indices[start:stop]],
            minlength=len(unique_scores))
        all_at_reversed_rank = np.bincount(unique_inverse,
                                        minlength=len(unique_scores))
        false_at_reversed_rank = all_at_reversed_rank - true_at_reversed_rank

        # if the scores are ordered, it's possible to count the number of
        # incorrectly ordered paires in linear time by cumulatively counting
        # how many false _labels of a given score have a score higher than the
        # accumulated true _labels with lower score.
        loss[i] = np.dot(true_at_reversed_rank.cumsum(),
                         false_at_reversed_rank)

    n_positives = np.diff(y_true.indptr)
    with np.errstate(divide="ignore", invalid="ignore"):
        loss /= ((n_labels - n_positives) * n_positives)

    # When there is no positive or no negative _labels, those values should
    # be consider as correct, i.e. the ranking doesn't matter.
    loss[np.logical_or(n_positives == 0, n_positives == n_labels)] = 0.

    return np.average(loss, weights=sample_weight)
def label_ranking_loss ( y_true , y_score , sample_weight = None ) : check_consistent_length ( y_true , y_score , sample_weight ) y_type = type_of_target ( y_true ) if y_type not in ( "multilabel" , ) : raise ValueError ( "{0} format is not supported" . format ( y_type ) ) if y_true . shape != y_score . shape : raise ValueError ( "y_true and y_score have different shape" ) n_samples , n_labels = y_true . shape y_true = csr_matrix ( y_true ) loss = np . zeros ( n_samples ) for i , ( start , stop ) in enumerate ( zip ( y_true . indptr , y_true . indptr [ 1 : ] ) ) : # Sort and bin the label scores unique_scores , unique_inverse = np . unique ( y_score [ i ] , return_inverse = True ) true_at_reversed_rank = np . bincount ( unique_inverse [ y_true . indices [ start : stop ] ] , minlength = len ( unique_scores ) ) all_at_reversed_rank = np . bincount ( unique_inverse , minlength = len ( unique_scores ) ) false_at_reversed_rank = all_at_reversed_rank - true_at_reversed_rank # if the scores are ordered, it's possible to count the number of # incorrectly ordered paires in linear time by cumulatively counting # how many false _labels of a given score have a score higher than the # accumulated true _labels with lower score. loss [ i ] = np . dot ( true_at_reversed_rank . cumsum ( ) , false_at_reversed_rank ) n_positives = np . diff ( y_true . indptr ) with np . errstate ( divide = "ignore" , invalid = "ignore" ) : loss /= ( ( n_labels - n_positives ) * n_positives ) # When there is no positive or no negative _labels, those values should # be consider as correct, i.e. the ranking doesn't matter. loss [ np . logical_or ( n_positives == 0 , n_positives == n_labels ) ] = 0. return np . average ( loss , weights = sample_weight )