-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathTopN.py
104 lines (88 loc) · 2.34 KB
/
TopN.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
import numpy as np
import os
import cPickle
def mysort(car0, car1):
return -cmp(car0['score'], car1['score'])
def mysort2(car0, car1):
return cmp(car0['score'], car1['score'])
def top20(fn, descend=True):
carlist = []
for line in file(fn):
line = line.replace('\n', '')
parts = line.split(',')
car = {}
car['id'] = parts[0]
car['name'] = parts[1]
car['score'] = float(parts[2])
carlist.append(car)
# print len(carlist)
if descend:
carlist.sort(mysort)
else:
carlist.sort(mysort2)
# print carlist
# exit()
top_20 = carlist[:20]
return top_20
def get_all_top(folder, descend=True):
qcars_list = []
for qcar in os.listdir(folder):
fname = qcar.split('.list')[0]
if qcar.endswith('list') is False:
continue
parts = fname.split('=')
# print qcar, parts
qcarinfo = {}
qcarinfo['id'] = parts[1]
qcarinfo['name'] = parts[2]
top_20 = top20(folder+'/'+qcar, descend)
qcarinfo['data'] = top_20
# print qcarinfo
qcars_list.append(qcarinfo)
return qcars_list
def get_rightrate(qcar_list):
topnum_list = [1, 2, 3, 4, 5, 10, 15, 20]
print 'TopN:', topnum_list
topsize = len(topnum_list)
toprate_num = np.zeros(topsize)
for qcar in qcar_list:
qid = qcar['id']
qname = qcar['name']
idx = 0
useds = np.zeros(topsize, np.bool)
for scar in qcar['data']:
sid = scar['id']
sname = scar['name']
if sid==qid:
for n, topid in enumerate(topnum_list):
if idx < topid:
useds[n:] = True
break
idx+=1
toprate_num[useds]+=1
qnum = len(qcar_list)
toprate = toprate_num / qnum
return toprate
def get_rightrate_each(qcar_list):
no_plate = []
has_plate = []
for car in qcar_list:
if car['name'].find('noplate')>=0:
no_plate.append(car)
else:
has_plate.append(car)
tr0 = get_rightrate(has_plate)
print 'has_plate:', tr0
tr1 = get_rightrate(no_plate)
print 'no_plate:', tr1
if __name__=='__main__':
if True:
folder = 'Result'
# qcar_list = get_all_top(folder, descend=True)
qcar_list = get_all_top(folder, descend=False)
cPickle.dump(qcar_list, open('top20.test.bin', 'wb'))
if True:
qcar_list = cPickle.load(open('top20.test.bin', 'rb'))
tr = get_rightrate(qcar_list)
print 'all_car:', tr
get_rightrate_each(qcar_list)