forked from bc3527/Netflix-Rating-Prediction-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Netflix.py
192 lines (144 loc) · 4.49 KB
/
Netflix.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
#!/usr/bin/env python
#!/usr/bin/env python
# Project 3 - Netflix
# Brett Canino
# Instructor - Glenn Downing
# Cs 373
# Netflix.py
# -------------
# netflix_solve
# -------------
def netflix_start (r, w) :
"""
calls methods to create average rating and creates user
and movie caches from files then calls eval
r is a reader
w is a writer
"""
user_cache = {}
netflix_user_cache(user_cache)
movie_cache = {}
netflix_movie_cache(movie_cache)
avg_rating = netflix_average_rating(user_cache)
assert type(avg_rating) is float
assert type(user_cache) is dict
assert type(movie_cache) is dict
assert user_cache # asserts that user_cache is not empty
assert movie_cache # asserts that movie_cache is not empty
netflix_eval(r, w, avg_rating, user_cache, movie_cache)
# ------------
# netflix_eval
# ------------
def netflix_eval(r, w, avg_rating, user_cache, movie_cache) :
"""
reads input file and makes a predicted rating of movies
and then writes them to a file
user_cache is a cache containing user_cache average rating
movie_cache is a cache containing average ratings for each movie
r is a reader
w is a writer
avg_rating is the overall average rating
"""
assert type(avg_rating) is float
assert avg_rating > 0
assert movie_cache # asserts that movie_cache is not empty
assert user_cache # asserts that user_cache is not empty
movie_off = 0.0
user_off = 0.0
prediction = 0.0
for line in r:
s = line.split(":")
if len(s) == 2 :
movie_id = s[0]
movie_rating = movie_cache[movie_id]
assert type(movie_rating) is float
movie_off = netflix_movie_offset(avg_rating, movie_rating)
w.write(line)
else :
assert len(s) == 1
user_id = s[0].strip()
user_rating = user_cache[int(user_id)]
assert type(user_rating) is float
movie_off = netflix_movie_offset(avg_rating, movie_rating)
user_off = netflix_user_offset(avg_rating, user_rating)
prediction = avg_rating + movie_off + user_off
assert type(prediction) is float
w.write(user_id + "," + str(prediction) + "\n")
assert user_off != 0
assert movie_off != 0
# --------------------
# netflix_movie_offset
# --------------------
def netflix_movie_offset(avg_rating, movie_rating) :
"""
returns offset from overall average rating and this movies average rating
avg_rating is the overall average rating
movie_rating is the average rating for this movie
"""
assert avg_rating > 0
assert movie_rating > 0
return movie_rating - avg_rating
# -------------------
# netflix_user_offset
# -------------------
def netflix_user_offset(avg_rating, user_rating) :
"""
returns offset from average user rating and this movies average rating
user_rating is the average rating for this user
movie_rating is the average rating for this movie
"""
assert user_rating > 0
assert avg_rating > 0
return user_rating - avg_rating
# ------------------
# netflix_user_cache
# ------------------
def netflix_user_cache(user_cache) :
"""
Creates a cache containing users average ratings
user_cache is a dict containing nothing at first and all the avg user data at the end
"""
assert user_cache == {} # asserts that user_cache is empty at start
file_name = "UserRatingsCache.txt"
uFile = open(file_name, 'r')
for line in uFile :
s = line.split("-")
if len(s) == 2 :
user_cache[int(s[0])] = float(s[1])
assert type(user_cache[int(s[0])]) is float
assert user_cache # asserts that user_cache is not empty
# -------------------
# netflix_movie_cache
# -------------------
def netflix_movie_cache(movie_cache) :
"""
Creates a cache containing movies average ratings
movie_cache is a dictionary containing nothing at first and all
the movie data at the end
"""
assert movie_cache == {} # asserts that movie_cache is empty at start
file_name = "MovieRatingsCache.txt"
mFile = open(file_name, 'r')
for line in mFile :
s = line.split(":")
movie_cache[s[0]] = float(s[1])
assert type(movie_cache[s[0]]) is float
assert movie_cache # asserts that movie_cache is not empty
# ----------------------
# netflix_average_rating
# ----------------------
def netflix_average_rating(user_cache) :
"""
Returns the average overall rating made by any user
user_cache is a dictionary that contains the average of all user_cache ratings
"""
assert user_cache # asserts that user_cache is not empty
total = 0.0
count = 0.0
for a, b in user_cache.items() :
total += b
count += 1
avg = total/count
assert type(avg) is float
assert avg > 0
return avg