def test_funksvd_ev_build(self): k = 5 save_path = 'evaluation/precision/' recommender = FunkSVDRecs() er = EvaluationRunner(0, None, # builder recommender, k, params={'k': 20, 'save_path': save_path + 'model/'}) result = er.calculate(1, 5) print(result) recs1 = recommender.recommend_items('14620', num=5) k2 = 7 recommender2 = FunkSVDRecs() er = EvaluationRunner(0, None, # builder, recommender2, k2, params={'k': 20, 'save_path': save_path + 'model/'}) result = er.calculate(1, 5) print(result) recs2 = recommender2.recommend_items('14620', num=7) self.compare_recs(recs1, recs2)
def test_rec(self): recommender = FunkSVDRecs(self.save_path) recs = recommender.recommend_items_by_ratings('1', [{'movie_id': AVENGERS, 'rating': 7}, {'movie_id': ALIEN, 'rating': 10}, {'movie_id': CAPTAIN_AMERICA, 'rating': 6}], num=2) self.assertIsNotNone(recs) self.assertEqual(len(recs), 2)
def test_rec2(self): recommender = FunkSVDRecs(self.save_path) recs = recommender.recommend_items_by_ratings('5', [{'movie_id': AVENGERS, 'rating': 1}], num=5) self.assertIsNotNone(recs) self.assertEqual(len(recs), 5) top = [r[0] for r in recs][:2] self.assertIn(HARRY, top, '{} was missing from {}'.format(HARRY, top)) self.assertIn(JACQUES, top, '{} was missing from {}'.format(JACQUES, top))
def test_rec_increasing2(self): recommender = FunkSVDRecs(self.save_path) recs4 = recommender.recommend_items_by_ratings('5', [{'movie_id': AVENGERS, 'rating': 1}], num=4) self.assertIsNotNone(recs4) self.assertEqual(len(recs4), 4) self.assertAlmostEqual(recs4[1][1]['prediction'], 7.812836963) recs6 = recommender.recommend_items_by_ratings('5', [{'movie_id': AVENGERS, 'rating': 1}], num=6) self.assertIsNotNone(recs6) self.assertEqual(len(recs6), 6) self.compare_recs(recs4, recs6) recommender = FunkSVDRecs(self.save_path) recs42 = recommender.recommend_items_by_ratings('5', [{'movie_id': AVENGERS, 'rating': 1}], num=4) self.compare_recs(recs4, recs42) recs1 = recommender.recommend_items_by_ratings('5', [{'movie_id': AVENGERS, 'rating': 1}], num=7) recs2 = recommender.recommend_items_by_ratings('5', [{'movie_id': AVENGERS, 'rating': 1}], num=9) self.compare_recs(recs1, recs2)
def test_rec_increasing(self): recommender = FunkSVDRecs(self.save_path) recs1 = recommender.recommend_items_by_ratings('5', [{'movie_id': AVENGERS, 'rating': 1}], num=2) self.assertIsNotNone(recs1) self.assertEqual(len(recs1), 2) recs2 = recommender.recommend_items_by_ratings('5', [{'movie_id': AVENGERS, 'rating': 1}], num=3) self.assertIsNotNone(recs2) self.assertEqual(len(recs2), 3) self.assertEqual(recs1[0],recs2[0] ) self.assertEqual(recs1[1],recs2[1] )
def evaluate_funksvd_recommender(): save_path = './models/funkSVD/' K = 20 timestr = time.strftime("%Y%m%d-%H%M%S") file_name = '{}-funksvd-k.csv'.format(timestr) with open(file_name, 'a', 1) as logfile: logfile.write("rak,pak,mae,k\n") builder = MatrixFactorization(save_path) for k in np.arange(0, 20, 2): recommender = FunkSVDRecs(save_path + 'model/') er = EvaluationRunner(0, builder, recommender, k, params={ 'k': 20, 'save_path': save_path + 'model/' }) result = er.calculate(20, 10) builder = None map = result['map'] mae = result['mae'] ar = result['ar'] logfile.write("{}, {}, {}, {}, {}, {}\n".format(ar, map, mae, k)) logfile.flush()
def recs_funksvd(request, user_id, num=6): sorted_items = FunkSVDRecs().recommend_items(user_id, num) data = { 'user_id': user_id, 'data': sorted_items } return JsonResponse(data, safe=False)
def evaluate_funksvd_recommender(): save_path = '/Users/u0157492/Projects/moviegeek/evaluation/precision/' K = 20 timestr = time.strftime("%Y%m%d-%H%M%S") file_name = '{}-funksvd-k.csv'.format(timestr) with open(file_name, 'a', 1) as logfile: logfile.write("rak,pak,mae,k,user_coverage,movie_coverage\n") #builder = MatrixFactorization(save_path) for k in np.arange(5, 50, 2): recommender = FunkSVDRecs() er = EvaluationRunner( 0, None, #builder, recommender, k, params={ 'k': 20, 'save_path': save_path + 'model/' }) result = er.calculate(1, 5) builder = None # Enough to train it once. #user_coverage, movie_coverage = RecommenderCoverage(recommender).calculate_coverage() user_coverage, movie_coverage = 0, 0 pak = result['pak'] mae = result['mae'] rak = result['rak'] logfile.write("{}, {}, {}, {}, {}, {}\n".format( rak, pak, mae, k, user_coverage, movie_coverage)) #logfile.write("{}, {}, {}, {}\n".format(rak, pak, mae, K)) logfile.flush()
args = parser.parse_args() print(args.fwls) k = 10 cov = None if args.fwls: logger.debug("evaluating coverage of fwls") cov = RecommenderCoverage(FeatureWeightedLinearStacking) cov.calculate_coverage(K=k, recName='fwls{}'.format(k)) if args.cf: logger.debug("evaluating coverage of cf") cov = RecommenderCoverage(NeighborhoodBasedRecs()) cov.calculate_coverage(K=k, recName='cf{}'.format(k)) if args.cb: logger.debug("evaluating coverage of cb") cov = RecommenderCoverage(ContentBasedRecs()) cov.calculate_coverage(K=k, recName='cb{}'.format(k)) if args.ltr: logger.debug("evaluating coverage of ltr") cov = RecommenderCoverage(BPRRecs()) cov.calculate_coverage(K=k, recName='bpr{}'.format(k)) if args.funk: logger.debug("evaluating coverage of funk") cov = RecommenderCoverage(FunkSVDRecs()) cov.calculate_coverage(K=k, recName='funk{}'.format(k))