AUC计算python实现

#!/usr/bin/env python
# coding=utf-8

import numpy as np
from sklearn.metrics import roc_curve
from sklearn.metrics import auc
from sklearn.metrics import roc_auc_score


def auc_calculate(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)


def AUC(label, pre):
    """
  适用于python3.0以上版本
   """

    # 计算正样本和负样本的索引,以便索引出之后的概率值
    pos = [i for i in range(len(label)) if label[i] == 1]
    neg = [i for i in range(len(label)) if label[i] == 0]

    auc = 0
    for i in pos:
        for j in neg:
            if pre[i] > pre[j]:
                auc += 1
            elif pre[i] == pre[j]:
                auc += 0.5

    return auc / (len(pos) * len(neg))

if __name__ == '__main__':

    label = [1,0,0,0,1,0,1,0]
    pred = [0.9, 0.8, 0.3, 0.1, 0.4, 0.9, 0.66, 0.7]


    fpr, tpr, thresholds = roc_curve(label, pred, pos_label=1)
    print("-----sklearn:",auc(fpr, tpr))  # 0.5666666666666667
    print("-----py1:",auc_calculate(label,pred))  # 0.5666666666666667
    print("-----py2:", AUC(label, pred))  # 0.5666666666666667
	
	# 最推荐这种,最简单
    print("-----py3:", roc_auc_score(y_true=label, y_score=pred))  # 0.5666666666666667

你可能感兴趣的:(机器学习,python,python,机器学习,AUC)