-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathauc.py
37 lines (27 loc) · 1.12 KB
/
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
33
34
35
36
37
import numpy as np
import sklearn.metrics
def calc_auc(error_array, cutoff=0.25):
error_array = error_array.squeeze()
error_array = np.sort(error_array)
num_values = error_array.shape[0]
plot_points = np.zeros((num_values, 2))
midfraction = 1.
for i in range(num_values):
fraction = (i + 1) * 1.0 / num_values
value = error_array[i]
plot_points[i, 1] = fraction
plot_points[i, 0] = value
if i > 0:
lastvalue = error_array[i - 1]
if lastvalue < cutoff < value:
midfraction = (lastvalue * plot_points[i - 1, 1] + value * fraction) / (value + lastvalue)
if plot_points[-1, 0] < cutoff:
plot_points = np.vstack([plot_points, np.array([cutoff, 1])])
else:
plot_points = np.vstack([plot_points, np.array([cutoff, midfraction])])
sorting = np.argsort(plot_points[:, 0])
plot_points = plot_points[sorting, :]
auc = sklearn.metrics.auc(plot_points[plot_points[:, 0] <= cutoff, 0],
plot_points[plot_points[:, 0] <= cutoff, 1])
auc = auc / cutoff
return auc, plot_points