/
simanalyser.py
173 lines (119 loc) · 3.46 KB
/
simanalyser.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#! /usr/bin/env python
# -*- coding: utf-8 -*-
'''
Author: Xihao Liang
Created: 2016.05.05
'''
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import numpy as np
import cPickle
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from utils import progbar
def precision_at_n(ys, pred_probs):
n_test = len(ys)
y_dim = len(pred_probs[0])
hit = [0 for i in range(y_dim)]
for y, probs in zip(ys, pred_probs):
eid_prob = sorted(enumerate(probs), key = lambda k:-k[1])
for i, item in enumerate(eid_prob):
eid, prob = item
if eid in y:
hit[i] += 1
break
for i in range(1, y_dim):
hit[i] += hit[i - 1]
acc = [float(hi) / n_test for hi in hit]
return acc
def report(ys, pred_probs, prefix):
'''
analyse the result of test set after model training
'''
acc = precision_at_n(ys, pred_probs)
cPickle.dump(acc, open('%s_prec.pkl'%(prefix), 'w'))
y_dim = len(pred_probs[0])
plt.figure()
plt.axis([1, y_dim, 0., 1.])
plt.xlabel('Top N')
plt.ylabel('Precision')
plt.plot(range(1, y_dim + 1), acc)
#rand_x = range(1, y_dim + 1)
#rand_y = [float(xi) / y_dim for xi in rand_x]
#plt.plot(rand_x, rand_y, '--r')
plt.savefig('%s_precision.png'%(prefix))
def revalidate(fname_ysup, thr_rank, prefix, oprefix):
sups = cPickle.load(open(fname_ysup, 'r'))
test_y, pred_probs = cPickle.load(open('data/dataset/test/%s_test.pkl'%(prefix), 'r'))
y_sup = []
for y, sup in zip(test_y, sups):
if thr_rank is not None and len(sup) > thr_rank:
sup = sup[:thr_rank]
sup = set(sup)
sup.add(y)
y_sup.append(sup)
report(y_sup, pred_probs, 'data/dataset/test/%s'%(oprefix))
def mismatch(fname_ysup, thr_rank, prefix, ofname):
sups = cPickle.load(open(fname_ysup, 'r'))
test_y, pred_probs = cPickle.load(open('data/dataset/test/%s_test.pkl'%(prefix), 'r'))
y_sup = []
mispair = np.zeros((90, 90))
for y, sup, probs in zip(test_y, sups, pred_probs):
eid_prob = sorted(enumerate(probs), key = lambda k:-k[1])
if thr_rank is not None and len(sup) > thr_rank:
sup = sup[:thr_rank]
sup = set(sup)
sup.discard(y)
pred_y = eid_prob[0][0]
if pred_y in sup:
mispair[y][pred_y] += 1
cPickle.dump(mispair, open(ofname, 'w'))
def export_vote(thr_rate, ofname):
n_batch = 90
yhists = []
pbar = progbar.start(n_batch)
for batch_id in range(n_batch):
fname = 'data/simrecord_90_%d.pkl'%(batch_id)
records = cPickle.load(open(fname, 'r'))
for y, x_len, record in records:
thr = x_len * thr_rate
ys = [yi for yi, d in record if d <= thr]
yhist = {}
for yi in ys:
if yhist.has_key(yi):
yhist[yi] += 1
else:
yhist[yi] = 1.
yhist = sorted(yhist.items(), key = lambda k: -k[1])
yhists.append(yhist)
pbar.update(batch_id + 1)
pbar.finish()
cPickle.dump(yhists, open(ofname, 'w'))
def main():
ofname = sys.argv[1]
thr_rate = float(sys.argv[2])
n_batch = 90
y_sup = []
pbar = progbar.start(n_batch)
for batch_id in range(n_batch):
fname = 'data/simrecord_90_%d.pkl'%(batch_id)
records = cPickle.load(open(fname, 'r'))
for y, x_len, record in records:
thr = x_len * thr_rate
ys = [yi for yi, d in record if d <= thr]
yhist = {}
for yi in ys:
if yhist.has_key(yi):
yhist[yi] += 1
else:
yhist[yi] = 1.
yhist = sorted(yhist.items(), key = lambda k: -k[1])
sup = [yi for yi, f in yhist]
y_sup.append(sup)
pbar.update(batch_id + 1)
pbar.finish()
cPickle.dump(y_sup, open(ofname, 'w'))
if __name__ == '__main__':
main()