forked from flyinglife001/cgvr
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun_utils.py
More file actions
121 lines (89 loc) · 2.69 KB
/
run_utils.py
File metadata and controls
121 lines (89 loc) · 2.69 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
from subprocess import Popen, PIPE
# import numpy as np
# from sklearn import metrics
def run(command):
process = Popen(command, stdout=PIPE, shell=True)
while True:
line = process.stdout.readline()
if not line:
break
yield line.strip()
def grid_generate(valid_dict):
"""
:param valid_dict: it's form is params with str items.
:return: paras dict with str items.
"""
#idx is reaching to the top of list or not.
#print 'idx',idx_L
idx_L = [0]*len(valid_dict) #参数长度
while True:
is_finished = True
comb_tuple = {} #中间变量
max_add_idx = -1 #最大值
i = 0
for k,S in valid_dict.items():
if idx_L[i] == len(S):
max_add_idx = i
elif idx_L[i] != len(S) - 1:
is_finished = False
comb_tuple[k] = S[idx_L[i]]
else:
comb_tuple[k] = S[idx_L[i]]
i += 1
#compute the next combination
for i in range(max_add_idx + 1):
idx_L[i] = 0
idx_L[max_add_idx + 1] += 1
if len(comb_tuple) == len(valid_dict):
yield comb_tuple
if is_finished:
break
def estimate_auc(pred, Y):
n = Y.shape[0]
seq = zip(pred,Y)
seq = sorted(seq,key=lambda x:x[0])
#print seq
ranks = []
last_v = -1e8
#[b_idx, e_idx)
b_idx = 0
e_idx = 0
i = 0
for v,y in seq:
ranks.append(i + 1)
if abs(v - last_v) > 1e-6:
e_idx = i
base_value = ranks[b_idx]
for k in range(b_idx,e_idx):
ranks[k] = base_value + (e_idx - b_idx - 1)/2.0
b_idx = e_idx
last_v = v
i += 1
#process the last range
e_idx = n
base_value = ranks[b_idx]
for k in range(b_idx,e_idx):
ranks[k] = base_value + (e_idx - b_idx - 1)/2.0
#print ranks
sum_pos_ranks = 0.0
n_pos = 0
#compute the accumulate value of positive values.
for i in range(n):
label = seq[n - 1 - i][1]
if int(label) == 1:
#print n - 1 - i, ranks[n - 1 - i]
sum_pos_ranks += ranks[n - 1 - i]
n_pos += 1
if n_pos == 0 or n_pos == n:
return 1
return (sum_pos_ranks - (n_pos + 1.0)*n_pos/2)/(n_pos*(n - n_pos))
# pred = np.array([0.2,0.3,0.3,0.1])
# y = np.array([1,0,1,0])
# print estimate_auc(pred,y)
# fpr, tpr, thresholds = metrics.roc_curve(y, pred)
# print metrics.auc(fpr, tpr)
# y = np.array([0, 0, 1, 1])
# pred = np.array([0.1, 0.4, 0.35, 0.8])
# fpr, tpr, thresholds = metrics.roc_curve(y, pred)
# print metrics.auc(fpr, tpr)
# print estimate_auc(pred,y)