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
|
#!/usr/bin/python
import numpy as np
import os
import sys
import pickle
import math
prun = 0
runs = {}
labels = {}
users = pickle.load((open(sys.argv[3])))
recs = map(lambda x:0,users)
conf = {}
thresh = 0.0
try:
thresh = float(sys.argv[4])
except:
pass
for line in open(sys.argv[1]):
line = line.split(',')
try:
run = int(line[3])
except:
continue
user = line[1]
if run not in labels:
#runs[run] = 0
labels[run] = users.index(user) + 1
for line in open(sys.argv[2]):
line = line.split(',')
try:
run = int(line[3])
except:
continue
user = line[1]
rec = ' '.join(line[6].split('@')[0].split('_'))
if run != prun and prun > 0:
runs[prun] = recs.index(max(recs))+1
recs = map(lambda x:0,users)
recs[users.index(rec)] += 1
maxc = float(line[7])/100.0
i = 9
cvec = []
while len(cvec) < len(users)-1:
if i < len(line):
cvec += [float(line[i])/100.0 - maxc]
else:
cvec += [-maxc]
conf[run] = math.log(np.sum(np.exp(cvec)))
prun = run
for i in range(999)+list(np.arange(999,1000,0.01)):
thresh = 5-i/100.0
t=0.0
tp=0.0
fp=0.0
fn=0.0
for (k,v) in runs.items():
#print v,labels[k]
if conf[k] > thresh:
fn += 1
elif v != labels[k]:
fp += 1
else:
tp += 1
t += 1
#print runs[167],labels[167]
#print tp,fp,fn
#print("Precision: ",tp/(tp+fp))
#print("False positives: ",fp/(tp+fp))
#print("Recall: ",1.0-fn/t)
try:
print str(1.0-fn/t)+","+str(tp/(tp+fp))
except:
print "0,1"
|