Python计算AUC

如题:

n = int(input())
labels = []
prob = []
for i in range(n):
    a = input().split()
    labels.append(int(a[0]))
    prob.append(float(a[1]))

def calAUC(prob, labels):
    f = list(zip(prob, labels))
    rank = [values2 for values1, values2 in sorted(f, key=lambda x: x[0])]
    rankList = [i + 1 for i in range(len(rank)) if rank[i] == 1]
    posNum = 0
    negNum = 0
    for i in range(len(labels)):
        if (labels[i] == 1):
            posNum += 1
        else:
            negNum += 1
    auc = 0
    auc = (sum(rankList) - (posNum * (posNum + 1)) / 2) / (posNum * negNum)
    return auc

print(calAUC(prob, labels))

 

你可能感兴趣的:(Python计算AUC)