-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathml.py
More file actions
executable file
·82 lines (77 loc) · 1.82 KB
/
ml.py
File metadata and controls
executable file
·82 lines (77 loc) · 1.82 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
from UserCF import *
from SplitData import *
# import matplotlib.pyplot as plt
def Recall(train,test,W,N,K):
hit = 0
all = 0
i = 0
for user in train.keys():
if not test.has_key(user):
continue
tu = test[user]
rank = Recommend(user,train,W,K)
s = sorted(rank.items(),key=itemgetter(1),reverse=True)[0:N]
for item, pui in s:
if item in tu:
hit += 1
all += len(tu)
i = i+1
print "there are",i,"users"
return hit/(all*1.0)
def Precision(train,test,W,N,K):
hit = 0
all = 0
for user in train.keys():
if not test.has_key(user):
continue
tu = test[user]
tu = test[user]
rank = Recommend(user,train,W,K)
s = sorted(rank.items(),key=itemgetter(1),reverse=True)[0:N]
for item, pui in s:
if item in tu:
hit += 1
all += N
return hit/(all*1.0)
# {user_id:{item_id:score}}
# N: top N item
# K: size of neighborhood
def main():
# test = ReadImplicitData('u2.test')
# train = ReadImplicitData('u2.base')
M = 8
k = 2
seed = 10
data = ReadData("Newexample20.txt")
(traindata,testdata) = SplitData(data,M,k,seed)
train = ImplicitData(traindata)
test = ImplicitData(testdata)
W = UserSimilarity(train)
R = []
P = []
# for K in range(10,41):
# r = Recall(train,test,W,10,K)
# R.append(r)
# print "recall(K=%d) is: %f"%(K,r)
# p = Precision(train,test,W,10,K)
# P.append(p)
# print "precision(K=%d) is: %f"%(K,p)
# for N in range(3,13):
# r = Recall(train,test,W,N,20)
# R.append(r)
# print "recall(N=%d) is: %f"%(N,r)
# p = Precision(train,test,W,N,20)
# P.append(p)
# print "precision(N=%d) is: %f"%(N,p)
# F = 2*p*r/(p+r)
# print "F-value: %f"%F
# # print "Recall",R
# # print "Precision",P
# plt.plot(R,P)
# plt.ylabel('precision')
# plt.xlabel('recall')
# plt.show()
print Recall(train,test,W,10,20)
print Precision(train,test,W,10,20)
if __name__ == '__main__':
main()