forked from susanli2016/Machine-Learning-with-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhitrate.py
124 lines (104 loc) · 3.87 KB
/
hitrate.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
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
from classes import usermovie
import numpy as np
from sklearn.metrics import mean_squared_error
from math import sqrt
def hit_rate(users, movies):
hits = 0
denom = 0
actual = []
predicted = []
actualall = []
predictedall = []
for u1 in users:
u = users[u1]
userid = u.userid
usermovies = []
if userid in users:
denom = denom + 1
ufactor = users[userid].factor
for m1 in movies:
m = movies[m1]
mfactor = m.factor
dotp = np.dot(ufactor, mfactor)
if m.movieid in u.movies_all:
actualall.append(u.movies_all[m.movieid])
predictedall.append(float(dotp))
if m.movieid in u.movies_test:
actual.append(u.movies_test[m.movieid])
predicted.append(dotp)
usermovied = usermovie()
usermovied.userid = userid
usermovied.movieid = m.movieid
usermovied.rating = dotp
usermovies.append(usermovied)
usermovies.sort(key=lambda x: x.rating, reverse=True)
count = 0
for um in usermovies:
userid = um.userid
movieid = um.movieid
#rating = um.rating
if movieid in users[userid].movies_test:
hits = hits + 1
break
count = count + 1
if count > 9:
break
sortedpredicted = predicted
least = min(sortedpredicted)
sortedpredicted = [x + least for x in sortedpredicted]
sortedpredicted = [x / max(sortedpredicted) for x in sortedpredicted]
sortedpredicted = [x * 5 for x in sortedpredicted]
predicted = sortedpredicted
sortedpredicted = predictedall
least = min(sortedpredicted)
sortedpredicted = [x + least for x in sortedpredicted]
sortedpredicted = [x / max(sortedpredicted) for x in sortedpredicted]
sortedpredicted = [x * 5 for x in sortedpredicted]
predictedall = sortedpredicted
rms = sqrt(mean_squared_error(actual, predicted))
rmsall = sqrt(mean_squared_error(actualall, predictedall))
return hits, denom, rms, rmsall
def hit_rate_SVD(users, movies, svd):
hits = 0
denom = 0
actual = []
predicted = []
actualall = []
predictedall = []
for u1 in users:
u = users[u1]
userid = u.userid
usermovies = []
if userid in users:
denom = denom + 1
for m1 in movies:
m = movies[m1]
dotp = float(svd.predict(int(userid), int(m.movieid))[3])
if m.movieid in u.movies_all:
actualall.append(u.movies_all[m.movieid])
predictedall.append(float(dotp))
if (str(m.movieid) in u.movies_test) | (int(m.movieid) in u.movies_test):
actual.append(u.movies_test[m.movieid])
predicted.append(float(dotp))
usermovied = usermovie()
usermovied.userid = userid
usermovied.movieid = m.movieid
usermovied.rating = dotp
usermovies.append(usermovied)
usermovies.sort(key=lambda x: x.rating, reverse=True)
count = 0
for um in usermovies:
userid = um.userid
movieid = um.movieid
if (str(movieid) in users[userid].movies_test) | (int(movieid) in users[userid].movies_test):
hits = hits + 1
break
count = count + 1
if count > 9:
break
rms = sqrt(mean_squared_error(actual, predicted))
rmsall = sqrt(mean_squared_error(actualall, predictedall))
return hits, denom, rms, rmsall