Skip to content

Commit

Permalink
Added average caches. Completed Unit Tests. Fixes #14 and fixes #3
Browse files Browse the repository at this point in the history
  • Loading branch information
lapluviosilla committed Feb 13, 2014
1 parent c020199 commit 428ed74
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 12 deletions.
33 changes: 29 additions & 4 deletions Netflix.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@
CACHE_PROBE = 0
CACHE_GENERAL = 1

cache_probe = {}
cache_probe = None
cache_movie_averages = None
cache_cust_averages = None
cache_num_ratings = None
cache_movie_decade_avg = None

PROBE_MEAN = 3.7
AVG_NUM_RATINGS = 5654.5

class Prediction:
def __init__(self, movie, custids, predictions):
Expand All @@ -23,7 +30,7 @@ def display(self, writer):
writer.write(str(round(rating, 1)) + "\n")

def calculate_rmse(self) :
return rmse(self.predictions, real_ratings())
return rmse(self.predictions, self.real_ratings())

def real_ratings(self) :
return map(lambda x: get_cache_probe()[self.movie][x], self.custids)
Expand All @@ -35,7 +42,7 @@ def netflix_solve(r, w) :
r is a reader
w is a writer
"""
get_cache_probe() # Load probe cache if it has not been loaded yet
netflix_load_caches()

assert(len(cache_probe) > 0)

Expand Down Expand Up @@ -64,11 +71,29 @@ def netflix_solve(r, w) :
def netflix_predict(movie, custids) :
assert(movie > 0)
assert(len(custids) > 0)
prediction = Prediction(movie, custids, len(custids) * [3.7])
netflix_load_caches()
predicted_ratings = []
for custid in custids:
rating = cache_movie_decade_avg[movie] + (cache_movie_averages[movie] - cache_movie_decade_avg[movie]) + (cache_cust_averages[custid] - PROBE_MEAN)
# rating = rating / 4
rating = max(min(rating, 5.0), 1.0) # Ratings can only be between 1.0 and 5.0
predicted_ratings.append(rating)
prediction = Prediction(movie, custids, predicted_ratings)
assert(prediction is not None)
return prediction

# with open("irvin-user_avg_json", "r")
#
def netflix_load_caches():
get_cache_probe()
global cache_movie_averages
global cache_cust_averages
global cache_num_ratings
global cache_movie_decade_avg
cache_movie_averages = cache_movie_averages or netflix_load_cache(CACHE_GENERAL, "ericweb2-movieAveragesOneLine.txt")
cache_cust_averages = cache_cust_averages or netflix_load_cache(CACHE_GENERAL, "ericweb2-custAveragesOneLine.txt")
cache_num_ratings = cache_num_ratings or netflix_load_cache(CACHE_GENERAL, "ericweb2-numRatingsOneLine.txt")
cache_movie_decade_avg = cache_movie_decade_avg or netflix_load_cache(CACHE_GENERAL, "ericweb2-movieDecadeAvgRatingOneLine.txt")

def netflix_load_cache(cache_type, path):
"""
Expand Down
30 changes: 22 additions & 8 deletions TestNetflix.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import io
import unittest

from Netflix import netflix_solve, netflix_predict, rmse, netflix_load_cache, set_cache_probe, get_cache_probe, Prediction, CACHE_PROBE, CACHE_GENERAL
from Netflix import netflix_solve, netflix_predict, rmse, netflix_load_cache, netflix_load_caches, set_cache_probe, get_cache_probe, Prediction, CACHE_PROBE, CACHE_GENERAL

class TestNetflix (unittest.TestCase) :
# ---
Expand All @@ -34,23 +34,29 @@ def test_solve3 (self) :
netflix_solve(r, w)
self.assertEqual(w.getvalue(), "10007:\n2.0\n2.7\n2.2\nRMSE: 1.6254316644013882\n")

def test_solve4 (self) :
r = io.StringIO("3676:\n1982226\n1495499\n1669842\n2639376\n1982597\n1426939\n")
w = io.StringIO()
netflix_solve(r, w)
self.assertEqual(w.getvalue(), "3676:\n3.7\n3.6\n4.5\n2.9\n4.2\n4.2\nRMSE: 0.6597351087461958\n")

# ---
# netflix_predict
# ---
def test_predict(self) :
prediction = netflix_predict(1, (4,8,10,12))
self.assertEqual(prediction.movie, 1)
self.assertEqual(prediction.predictions, 4 * [3.7])
prediction = netflix_predict(3674, (1100257, 347434, 2472269))
self.assertEqual(prediction.movie, 3674)
self.assertEqual(prediction.predictions, [4.21234183018522, 3.377376155128011, 3.660141657622665])

def test_predict2(self) :
prediction = netflix_predict(2, (1,2,3,4))
prediction = netflix_predict(3675, (1982589, 1376081, 2309102))
s = io.StringIO()
self.assertEqual(prediction.movie, 2)
self.assertEqual(prediction.movie, 3675)
prediction.display(s)
self.assertEqual(s.getvalue(), "2:\n3.7\n3.7\n3.7\n3.7\n")
self.assertEqual(s.getvalue(), "3675:\n2.3\n3.1\n2.9\n")

def test_predict3(self) :
prediction = netflix_predict(1, (1,2,3))
prediction = netflix_predict(3674, (1100257, 347434, 2472269))
self.assertTrue(type(prediction), Prediction)

def test_prediction_display(self) :
Expand All @@ -65,6 +71,10 @@ def test_prediction_display2(self) :
prediction.display(s)
self.assertEqual(s.getvalue(), "2:\n2\n3\n")

def test_prediction_real_ratings(self) :
prediction = Prediction(3675, (1982589, 1376081, 2309102), [4,5,2])
self.assertEqual(list(prediction.real_ratings()), [4, 3, 1])

def test_prediction_rmse(self) :
set_cache_probe({1: {1: 3, 4:2, 5:5}})
prediction = Prediction(1, [1,4,5], [3.7, 2.1, 4.2])
Expand Down Expand Up @@ -106,6 +116,10 @@ def test_get_cache_probe(self) :
self.assertEqual(len(cache[10]), 2)
self.assertEqual(cache[10][1531863], 3)

def test_load_caches(self) :
netflix_load_caches()
self.assertTrue(get_cache_probe is not None)

# ---
# rmse
# ---
Expand Down

0 comments on commit 428ed74

Please sign in to comment.