-
Notifications
You must be signed in to change notification settings - Fork 4
/
AUC.py
33 lines (30 loc) · 883 Bytes
/
AUC.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import csv
from sklearn import metrics
import numpy as np
def tiedrank(X):
Z = [(x, i) for i, x in enumerate(X)]
Z.sort()
n = len(Z)
Rx = [0]*n
for j, (x,i) in enumerate(Z):
Rx[i] = j+1
s = 1 # sum of ties.
start = end = 0 # starting and ending marks.
for i in range(1, n):
if Z[i][0] == Z[i-1][0] and i != n-1:
pos = Z[i][1]
s+= Rx[pos]
end = i
else: #end of similar x values.
tiedRank = float(s)/(end-start+1)
for j in range(start, end+1):
Rx[Z[j][1]] = tiedRank
for j in range(start, end+1):
Rx[Z[j][1]] = tiedRank
start = end = i
s = Rx[Z[i][1]]
return Rx
def AUC(labels, posterior):
r = tiedrank(posterior)
auc = (sum(r*(labels==1)) - sum(labels==1)*(sum(labels==1)+1)/2) / (sum(labels<1)*sum(labels==1));
return auc