python 计算 AUC

import numpy as np
def AUC1(labels, preds):      
    """
    所有的正负样本对中,正样本排在负样本前面占样本对数的比例。
    [1,1,1,1,0,0], [0.9,0.8,0.7,0.8,0.3,0.2], AUC=1
    
    
    """
    pos = [i for i in range(len(labels)) if labels[i] == 1]
    neg = [i for i in range(len(labels)) if labels[i] == 0]
 
    auc = 0
    for i in pos:
        for j in neg:
            if preds[i] > preds[j]:
                auc += 1
            elif preds[i] == preds[j]:
                auc += 0.5
 
    return auc / (len(pos)*len(neg))

def AUC2(labels,preds,n_bins=100):
    postive_len = sum(labels)
    negative_len = len(labels) - postive_len
    total_case = postive_len * negative_len
    pos_histogram = [0 for _ in range(n_bins)]
    neg_histogram = [0 for _ in range(n_bins)]
    bin_width = 1.0 / n_bins
    for i in range(len(labels)):
        nth_bin = int(preds[i]/bin_width)
        if labels[i]==1:
            pos_histogram[nth_bin] += 1
        else:
            neg_histogram[nth_bin] += 1
    accumulated_neg = 0
    satisfied_pair = 0
    for i in range(n_bins):
        satisfied_pair += (pos_histogram[i]*accumulated_neg + pos_histogram[i]*neg_histogram[i]*0.5)
        accumulated_neg += neg_histogram[i]

    return satisfied_pair / float(total_case)

if __name__ == '__main__':

    y = np.array([1,0,0,0,1,0,1,0,])
    pred = np.array([0.9, 0.8, 0.3, 0.1,0.4,0.9,0.66,0.7])
    print(AUC1(y,pred))
    print(AUC2(y,pred))

参考

posted @ 2019-08-07 20:00  机器狗mo  阅读(334)  评论(0编辑  收藏  举报