-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
225 lines (162 loc) · 7 KB
/
model.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
"""Models and database functions for Ratings project."""
from flask_sqlalchemy import SQLAlchemy
import correlation
import time
# This is the connection to the PostgreSQL database; we're getting this through
# the Flask-SQLAlchemy helper library. On this, we can find the `session`
# object, where we do most of our interactions (like committing, etc.)
db = SQLAlchemy()
##############################################################################
# Model definitions
class User(db.Model):
"""User of ratings website."""
__tablename__ = "users"
user_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
email = db.Column(db.String(64))
password = db.Column(db.String(64))
age = db.Column(db.Integer)
zipcode = db.Column(db.String(15))
ratings = db.relationship('Rating')
def __repr__(self):
"""Representation of User instance"""
return "<User: user_id={}, email={}>".format(self.user_id, self.email)
def similarity(self, movie_scores):
"""Find similatiry between two users"""
# print ' enter similatiry'
ratings = db.session.execute("""\
SELECT movie_id, score
FROM ratings
WHERE user_id = :curr_user
ORDER BY movie_id\
""", {'curr_user': self.user_id}).fetchall()
u_ratings = {}
paired_ratings = []
# print ' init sim vals'
for movie_id, score in ratings:
u_ratings[movie_id] = score
# print ' get user ratings'
for movie_id, o_score in movie_scores.items():
u_rating = u_ratings.get(movie_id)
if u_rating:
paired_ratings.append((u_ratings[movie_id], o_score))
# print ' get other ratings'
# print time_diff
if paired_ratings:
# print ' exit similarity - found paired_ratings'
return correlation.pearson(paired_ratings)
# print ' exit similarity - no paired_ratings'
return 0.0
def predict_rating(self, movie):
"""Predict user's rating of a movie."""
start_time = time.time()
other_ratings = db.session.execute("""\
SELECT user_id, movie_id, score
FROM ratings
WHERE user_id IN (
SELECT user_id
FROM ratings
WHERE movie_id = :movie_id
)
AND user_id != :curr_user
ORDER BY user_id, movie_id\
""", {'curr_user': self.user_id,
'movie_id': movie.movie_id}).fetchall()
user_movie_scores = {}
for other_rating in other_ratings:
user_id, movie_id, score = other_rating
if user_id not in user_movie_scores:
user_movie_scores[user_id] = {}
user_movie_scores[user_id][movie_id] = score
similarities = []
for user_id, other_ratings in user_movie_scores.items():
similarities.append(
(self.similarity(other_ratings),
user_movie_scores[user_id][movie.movie_id])
)
# print ' get similarities'
similarities.sort(reverse=True)
# print ' reverse similarities'
similarities = [(sim, score) for sim, score in similarities
if sim > 0]
# print ' filter similarities'
if not similarities:
# print ' exit predict_rating - no similarities'
return None
numerator = sum([score * sim for sim, score in similarities])
# print ' get numerator'
denominator = sum([sim for sim, _ in similarities])
# print ' get denominator'
end_time = time.time()
time_diff = end_time - start_time
print time_diff
# print 'exit predict_rating'
return numerator/denominator
def _predict_rating(self, movie):
"""Predict users rating of movie"""
other_ratings = sorted([(self.similarity(rating.user), rating)
for rating in movie.ratings])
# best_match = other_ratings[-1]
# print best_match[0], best_match[1].score, best_match[1].user
# print self.similarity(best_match[1].user, print_list=True)
# prediction = best_match[0] * best_match[1].score
upper_bound = 1.1
lower_bound = 0
pos_list = [sim * rating.score for sim, rating in other_ratings
if upper_bound > sim > lower_bound]
pos = sum(pos_list)
neg_list = [-sim * abs(rating.score-6) for sim, rating in other_ratings
if -upper_bound < sim < -lower_bound]
neg = sum(neg_list)
denominator = sum([abs(sim) for sim, _ in other_ratings
if upper_bound > sim > lower_bound
or -upper_bound < sim < -lower_bound])
print 'bounds', upper_bound, lower_bound
print 'pos', len(pos_list), '-', pos
print 'neg', len(neg_list), '-', neg
print 'denom', denominator
prediction = (pos + neg) / denominator
# prediction = pos / denominator
return prediction
class Movie(db.Model):
"""Movie object"""
__tablename__ = "movies"
movie_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
title = db.Column(db.String(128), nullable=False)
released_at = db.Column(db.DateTime, nullable=False)
imbd_url = db.Column(db.String(256))
ratings = db.relationship('Rating')
def __repr__(self):
"""Representation of Movie instance"""
return "<Movie: movie_id={}, title={}>".format(self.movie_id,
self.title)
class Rating(db.Model):
""" Rating object """
__tablename__ = "ratings"
rating_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
movie_id = db.Column(db.Integer, db.ForeignKey('movies.movie_id'),
nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('users.user_id'),
nullable=False)
score = db.Column(db.Integer, nullable=False)
user = db.relationship('User')
movie = db.relationship('Movie')
def __repr__(self):
"""Representation of User instance"""
return "<Rating: rating_id={}, movie_id={}, user_id={}, score={}>".format(
self.rating_id, self.movie_id, self.user_id, self.score)
##############################################################################
# Helper functions
def connect_to_db(app):
"""Connect the database to our Flask app."""
# Configure to use our PstgreSQL database
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql:///ratings'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# app.config['SQLALCHEMY_ECHO'] = True
db.app = app
db.init_app(app)
if __name__ == "__main__":
# As a convenience, if we run this module interactively, it will leave
# you in a state of being able to work with the database directly.
from server import app
connect_to_db(app)
print "Connected to DB."