import numpy as np
from sklearn.metrics import roc_curve
from sklearn.metrics import auc
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))
print("-----py1:",auc_calculate(label,pred))
print("-----py2:", AUC(label, pred))
|