-
Notifications
You must be signed in to change notification settings - Fork 15
/
collabFilter.py
executable file
·279 lines (222 loc) · 9.83 KB
/
collabFilter.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
"""
Executing code:
Python collabFilter.py ratings-dataset.tsv Kluver 'The Fugitive' 10
Changes Log
- Oct. 26, 2015
1. due date extend to Oct. 27, 2015, final check
2. recovery judgement for invalid input file and user id
- Oct. 24, 2015
1. change logic for updated requirement, If all similarities are same, sort them by descending order of user id for K nearest neighbors.
- Oct. 22, 2015
1. remove tips description
2. remove parameters judgement
3. add judgement for predict when denominator is 0
- Oct. 21, 2015
1. get rid of round function
- Oct. 20, 2015
1. modify file name as firstname_lastname_collabFilter.py
2. modify executing code based on new filename
3. add parameter validation inside initialize method
4. add exit state description
"""
import sys
import math
import os
class Collaborate_Filter:
def __init__(self, input_file_name, user_id, movie, k):
self.input_file_name = input_file_name
self.user_id = user_id
self.movie = movie
self.k = k
self.dataset = None
self.uu_dataset = None
self.ii_dataset = None
def initialize(self):
"""
Initialize and check parameters
"""
# check file exist and if it's a file or dir
if not os.path.isfile(self.input_file_name):
self.quit("Input file doesn't exist or it's not a file")
# load data
self.dataset, self.uu_dataset, self.ii_dataset = self.load_data(self.input_file_name)
# check if user exist
users = self.uu_dataset.keys()
if self.user_id not in users:
self.quit("User ID doesn't exist")
"""
# check if movie exist
items = self.ii_dataset.keys()
if self.movie not in items:
self.quit("Movie doesn't exist")
# check k validation
max_k = len(users) - 1
min_k = 1
if self.k < min_k or self.k > max_k:
self.quit("k value for k nearest neighbors is not valid, it should be inside [" + str(min_k) + ", " + str(max_k) +"]")
"""
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""" Pearson Correlation """
""" """
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
def pearson_correlation(self, user1, user2):
result = 0.0
user1_data = self.uu_dataset[user1]
user2_data = self.uu_dataset[user2]
rx_avg = self.user_average_rating(user1_data)
ry_avg = self.user_average_rating(user2_data)
sxy = self.common_items(user1_data, user2_data)
top_result = 0.0
bottom_left_result = 0.0
bottom_right_result = 0.0
for item in sxy:
rxs = user1_data[item]
rys = user2_data[item]
top_result += (rxs - rx_avg)*(rys - ry_avg)
bottom_left_result += pow((rxs - rx_avg), 2)
bottom_right_result += pow((rys - ry_avg), 2)
bottom_left_result = math.sqrt(bottom_left_result)
bottom_right_result = math.sqrt(bottom_right_result)
result = top_result/(bottom_left_result * bottom_right_result)
return result
def user_average_rating(self, user_data):
avg_rating = 0.0
size = len(user_data)
for (movie, rating) in user_data.items():
avg_rating += float(rating)
avg_rating /= size * 1.0
return avg_rating
def common_items(self, user1_data, user2_data):
result = []
ht = {}
for (movie, rating) in user1_data.items():
ht.setdefault(movie, 0)
ht[movie] += 1
for (movie, rating) in user2_data.items():
ht.setdefault(movie, 0)
ht[movie] += 1
for (k, v) in ht.items():
if v == 2:
result.append(k)
return result
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""" K Nearest Neighbors """
""" """
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
def k_nearest_neighbors(self, user, k):
neighbors = []
result = []
for (user_id, data) in self.uu_dataset.items():
if user_id == user:
continue
upc = self.pearson_correlation(user, user_id)
# upc = round(upc, 11)
neighbors.append([user_id, upc])
# neighbors_ht.setdefault(user_id, upc) # assume there are not duplicate user_id
# sorted_neighbors_ht = sorted(neighbors_ht.iteritems(), key=lambda neighbors_ht : neighbors_ht[1], reverse=True)
sorted_neighbors = sorted(neighbors, key=lambda neighbors: (neighbors[1], neighbors[0]), reverse=True) # - for desc sort
# testitems = [('a', 3), ('o', 5), ('g', 6), ('c', 1), ('b', 1)]
# sorted_testitems = sorted(testitems, key=lambda testitems: (-testitems[1], testitems[0])) # - for desc sort
for i in range(k):
if i >= len(sorted_neighbors):
break
result.append(sorted_neighbors[i])
return result
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""" Predict """
""" """
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
def predict(self, user, item, k_nearest_neighbors):
valid_neighbors = self.check_neighbors_validattion(item, k_nearest_neighbors)
if not len(valid_neighbors):
return 0.0
top_result = 0.0
bottom_result = 0.0
for neighbor in valid_neighbors:
neighbor_id = neighbor[0]
neighbor_similarity = neighbor[1] # Wi1
rating = self.uu_dataset[neighbor_id][item] # rating i,item
top_result += neighbor_similarity * rating
bottom_result += neighbor_similarity
result = top_result/bottom_result
return result
def check_neighbors_validattion(self, item, k_nearest_neighbors):
result = []
for neighbor in k_nearest_neighbors:
neighbor_id = neighbor[0]
# print item
if item in self.uu_dataset[neighbor_id].keys():
result.append(neighbor)
return result
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""" Helper Functions """
""" """
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
def load_data(self, input_file_name):
"""
load data and return three outputs for extention purpose
only one output is enough in practice (uu_dataset)
"""
input_file = open(input_file_name, 'rU')
dataset = []
uu_dataset = {}
ii_dataset = {}
for line in input_file:
row = str(line)
row = row.split("\t")
row[2] = row[2][:-1]
dataset.append(row)
"""
user-user dataset: [0: Movie Name 1: Rating]
"""
uu_dataset.setdefault(row[0], {})
uu_dataset[row[0]].setdefault(row[2], float(row[1]))
# uu_dataset[row[0]].append([row[2],row[1]])
"""
item-item dataset: [0: user id 1: Rating]
"""
ii_dataset.setdefault(row[2], {})
ii_dataset[row[2]].setdefault(row[0], float(row[1]))
# ii_dataset[row[2]].append([row[0], row[1]])
return dataset, uu_dataset, ii_dataset
def display(self, k_nearest_neighbors, prediction):
for neighbor in k_nearest_neighbors:
print neighbor[0], neighbor[1]
print "\n"
print prediction
def quit(self, err_desc):
tips = "\n" + "TIPS: " + "\n" \
+ "--------------------------------------------------------" + "\n" \
+ "Pragram name: lingzhe_teng_collabFilter.py" + "\n" \
+ "First parameter: Input File, e.g. ratings-dataset.tsv" + "\n" \
+ "Second parameter: User ID, e.g. Kluver" + "\n" \
+ "Thrid parameter: Movie, e.g. The Fugitive" + "\n" \
+ "Fourth parameter: K, e.g. 10" + "\n" \
+ "--------------------------------------------------------" + "\n" \
+ "Note:" + "\n" \
+ "Please use double quotation marks, such as \"USER\'S ID\" or \"MOVIEW\'S NAME\", for User ID and Moview parameters" + "\n"
raise SystemExit('\n'+ "PROGRAM EXIT: " + err_desc + ', please check your input' + '\n' + tips)
if __name__ == '__main__':
# publish
input_file_name = sys.argv[1] # ratings-dataset.tsv
user_id = sys.argv[2] # user name
movie = sys.argv[3] # movie name
k = int(sys.argv[4]) # k neighbors
# test
# input_file_name = "ratings-dataset.tsv"
# user_id = "Kluver"
# movie = 'The Fugitive'
# k = 10
cf = Collaborate_Filter(input_file_name, user_id, movie, k)
cf.initialize()
# cf.pearson_correlation(user_id, user_id)
# cf.pearson_correlation("Flesh", "Nathan_Studanski")
k_nearest_neighbors = cf.k_nearest_neighbors(user_id, k)
# cf.k_nearest_neighbors("Flesh", 2)
prediction = cf.predict(user_id, movie, k_nearest_neighbors)
cf.display(k_nearest_neighbors, prediction)
# test
# print input_file_name
# print user_id
# print movie
# print k