-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecommender.py
274 lines (248 loc) · 7.92 KB
/
recommender.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
from math import sqrt
import copy
"""
@desc get the neighbours of a user
@param prefs user-item matrix
userid id of a user
bookid id of a book
n number of neighbours
@return neighbours list
"""
def getNeighbours(prefs,userid,bookid, n=5):
orgscores = [(similarity(prefs,userid,other),other)
for other in prefs if other != userid]
# remove neighbours which do not have read the books[bookid]
scores = {}
for sim, userid in orgscores:
if bookid in prefs[userid].keys():
scores[userid] = sim
# sort the neighbours by similarity
scores = sorted(scores.items(), key=lambda d:d[1])
scores.reverse()
return scores[0:n]
"""
@desc calculate the similarity by Pearson correlation coefficient
@param prefs user-item matrix
p1 userid`
p1 the other userid
@return similarity value
"""
def similarity(prefs,p1,p2):
#Get the list of mutually rated items
si = {}
for item in prefs[p1]:
if item in prefs[p2]:
si[item] = 1
#if they are no rating in common, return 0
if len(si) == 0:
return 0
#sum calculations
n = len(si)
#sum of all preferences
sum1 = sum([prefs[p1][it] for it in si])
sum2 = sum([prefs[p2][it] for it in si])
#Sum of the squares
sum1Sq = sum([pow(prefs[p1][it],2) for it in si])
sum2Sq = sum([pow(prefs[p2][it],2) for it in si])
#Sum of the products
pSum = sum([prefs[p1][it] * prefs[p2][it] for it in si])
#Calculate r (Pearson score)
num = pSum - (sum1 * sum2/n)
den = sqrt((sum1Sq - pow(sum1,2)/n) * (sum2Sq - pow(sum2,2)/n))
if den == 0:
return 0
r = num/den
# if r is negative, make it 0
if r < 0:
r = 0
return r
"""
@desc convert the rating scale
@param rating rating value
@return rating value in new scale
"""
def convertScale(rating):
scale = {
'0': -1,
'-5': 0.00,
'-3': 0.25,
'1': 0.50,
'3': 0.75,
'5': 1.00,
}
return scale[rating]
"""
@desc load the data file
@param path dir path of the datafile
return testPrefs test dataset dict
prefs dataset dict
users user list
books book list
"""
def loadDataset(path=""):
# load the books info
books = []
for line in open(path+"books_list.txt"):
line = line.strip('\n').strip()
books.append(line)
# load the user book rating
testPrefs = {}
prefs = {}
users = []
index = 1
userid = 0
for line in open(path+"books_ratings.txt"):
line = line.strip('\n').strip()
# odd lines are usernames
if index % 2 == 1:
users.append(line)
index += 1
continue
# even lines are ratings
ratings = line.split(" ")
testRatings = {}
allRatings = {}
count = 0
bookid = 0
valid = 0
for rating in ratings:
# use two points with smallest IDs as test dataset
if rating != '0' and count < 2:
testRatings[bookid] = convertScale(rating)
allRatings[bookid] = convertScale(rating)
count += 1
else:
if rating != '0':
valid = 1
allRatings[bookid] = convertScale(rating)
bookid += 1
if valid == 1:
testPrefs[userid] = testRatings
# user book rating, delete the ratings of books that user have never read
prefs[userid] = allRatings
userid += 1
index += 1
return testPrefs,prefs,users,books
"""
@desc: predict the rating possibly given by a user
@param: prefs user-item matrix
userid id of a user
bookid id of a book
neighbours top N nearest to user[userid]
@return rating value
"""
def predict(prefs, userid, bookid, neighbours):
sumSim = 0
for uid, sim in neighbours:
sumSim += sim
if sumSim == 0:
return -1
weight = {}
rating = 0
for uid, sim in neighbours:
weight[uid] = sim/sumSim
if bookid in prefs[uid].keys():
if prefs[uid][bookid] != -1:
rating += prefs[uid][bookid] * weight[uid]
return rating
"""
@desc: remove user[userid]'s ratings of two books
in prefs to generate train preferences
@param: prefs user-items matrix
userid id of a user
items items to be removed
@return: train dataset dict
"""
def generateTrainPrefs(prefs, userid, items):
trainPrefs = copy.deepcopy(prefs)
for key in items.keys():
trainPrefs[userid].pop(key)
return trainPrefs
"""
calculate root mean square error
@param predictRatings predict ratings dict
realRatings test ratings dict
@return rmse value
"""
def rmse(predictRatings, realRatings):
predictRatingList = []
for userid, items in predictRatings.items():
for itemid, value in items.items():
predictRatingList.append(value)
realRatingList = []
for userid, items in realRatings.items():
for itemid, value in items.items():
realRatingList.append(value)
# print zip(predictRatingList, realRatingList)
return sqrt(sum([(f - o) ** 2 for f, o in zip(predictRatingList, realRatingList)]) / len(predictRatingList))
"""
@desc evaluation by mean method
@param testPrefs test dataset
prefs complete dataset
"""
def evaluation_mean(testPrefs, prefs):
rating = {}
ratings = {}
count = {}
# initialize rating, count to 0
for (userid, items) in testPrefs.items():
rating.setdefault(userid, {})
count.setdefault(userid, {})
for (bookid, value) in items.items():
rating[userid][bookid] = 0
count[userid][bookid] = 0
# add others' ratings on each book in test dataset
for testUserid in testPrefs.keys():
rating.setdefault(testUserid, {})
for (userid, items) in prefs.items():
# not oneself
if userid == testUserid:
continue
for (bookid,value) in items.items():
if bookid in rating[testUserid].keys() and value != -1:
# sum rating of a book from others
rating[testUserid][bookid] += value
# sum count of a book from others
count[testUserid][bookid] += 1
for testUserid in testPrefs.keys():
for bookid in rating[testUserid].keys():
# mean rating of a book from others
rating[testUserid][bookid] = rating[testUserid][bookid]/count[testUserid][bookid]
ratings[testUserid] = rating[testUserid]
return rmse(ratings, testPrefs)
"""
@desc evaluation by collaborative filtering method
@param testPrefs test dataset
prefs complete dataset
neighbour_num number of neighbours
"""
def evaluation_cf(testPrefs, prefs, neighbour_num = 5):
ratings = {}
missUser = []
validTestPrefs = {}
for userid in testPrefs.keys():
trainPrefs = generateTrainPrefs(prefs, userid, testPrefs[userid])
ratings.setdefault(userid, {})
for bookid in testPrefs[userid].keys():
neighbours = getNeighbours(trainPrefs, userid, bookid, neighbour_num)
ratings[userid][bookid] = predict(trainPrefs, userid, bookid, neighbours)
"""
some users might always get 0 similarity
in this case, they cannot get prediction
"""
if ratings[userid][bookid] == -1:
ratings.pop(userid)
missUser.append(userid)
break
validTestPrefs[userid] = testPrefs[userid]
return rmse(ratings, validTestPrefs)
if __name__ == '__main__':
testPrefs,prefs,users,_ = loadDataset("")
print evaluation_mean(testPrefs, prefs)
print evaluation_cf(testPrefs, prefs, 5);
print evaluation_cf(testPrefs, prefs, 10);
"""
RMSE for MEAN method: 0.287120678495
RMSE for CF(5) method: 0.328335900317
RMSE for CF(10) method: 0.323508363794
"""