(y_true, y_score, sample_weight=None)
return aveprec*1.0/test_data_num
Compute average precision (AP) from prediction scores

    AP summarizes a precision-recall curve as the weighted mean of precisions
    achieved at each threshold, with the increase in recall from the previous
    threshold used as the weight:

    .. math::
        \\text{AP} = \\sum_n (R_n - R_{n-1}) P_n

    Parameters
    ----------
    y_true : array, shape = [n_samples] or [n_samples, n_classes]
        True binary labels or binary label indicators.

    y_score : array, shape = [n_samples] or [n_samples, n_classes]
        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
    -------
    average_precision : float
Compute average precision (AP) from prediction scores
Compute average precision ( AP ) from prediction scores
def average_precision_score(y_true, y_score, sample_weight=None):
    """
        Compute average precision (AP) from prediction scores

    AP summarizes a precision-recall curve as the weighted mean of precisions
    achieved at each threshold, with the increase in recall from the previous
    threshold used as the weight:

    .. math::
        \\text{AP} = \\sum_n (R_n - R_{n-1}) P_n

    Parameters
    ----------
    y_true : array, shape = [n_samples] or [n_samples, n_classes]
        True binary labels or binary label indicators.

    y_score : array, shape = [n_samples] or [n_samples, n_classes]
        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
    -------
    average_precision : 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))

    test_data_num = y_score.shape[0]
    class_num = y_score.shape[1]
    temp_outputs = []
    temp_test_target = []
    instance_num = 0
    labels_index = []
    not_labels_index = []
    labels_size = []
    for i in range(test_data_num):
        if sum(y_true[i]) != class_num and sum(y_true[i]) != 0:
            instance_num = instance_num + 1
            temp_outputs.append(y_score[i])
            temp_test_target.append(y_true[i])
            labels_size.append(sum(y_true[i] == 1))
            index1, index2 = find(y_true[i], 1, 0)            
            labels_index.append(index1)
            not_labels_index.append(index2)
    
    aveprec = 0
    for i in range(instance_num):
        tempvalue, index = sort(temp_outputs[i])
        indicator = np.zeros((class_num,))     
        for j in range(labels_size[i]):
            loc = findIndex(labels_index[i][j], index)
            indicator[loc] = 1
        summary = 0
        for j in range(labels_size[i]):
            loc = findIndex(labels_index[i][j], index)
            summary = summary + sum(indicator[loc:class_num])*1.0/(class_num-loc)
        aveprec = aveprec + summary*1.0/labels_size[i]
    return aveprec*1.0/test_data_num
def 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 ) ) test_data_num = y_score . shape [ 0 ] class_num = y_score . shape [ 1 ] temp_outputs = [ ] temp_test_target = [ ] instance_num = 0 labels_index = [ ] not_labels_index = [ ] labels_size = [ ] for i in range ( test_data_num ) : if sum ( y_true [ i ] ) != class_num and sum ( y_true [ i ] ) != 0 : instance_num = instance_num + 1 temp_outputs . append ( y_score [ i ] ) temp_test_target . append ( y_true [ i ] ) labels_size . append ( sum ( y_true [ i ] == 1 ) ) index1 , index2 = find ( y_true [ i ] , 1 , 0 ) labels_index . append ( index1 ) not_labels_index . append ( index2 ) aveprec = 0 for i in range ( instance_num ) : tempvalue , index = sort ( temp_outputs [ i ] ) indicator = np . zeros ( ( class_num , ) ) for j in range ( labels_size [ i ] ) : loc = findIndex ( labels_index [ i ] [ j ] , index ) indicator [ loc ] = 1 summary = 0 for j in range ( labels_size [ i ] ) : loc = findIndex ( labels_index [ i ] [ j ] , index ) summary = summary + sum ( indicator [ loc : class_num ] ) * 1.0 / ( class_num - loc ) aveprec = aveprec + summary * 1.0 / labels_size [ i ] return aveprec * 1.0 / test_data_num