def test_split_data(self): ratings = pd.DataFrame( [ [1, STAR_WARS, 9, '2013-10-12 23:21:27+00:00'], [1, WONDER_WOMAN, 10, '2014-10-12 23:22:27+00:00'], [1, AVENGERS, 10, '2015-11-12 23:20:27+00:00'], [1, WOLVERINE, 8, '2015-08-12 23:20:27+00:00'], [1, PIRATES_OF, 10, '2015-10-12 22:20:27+00:00'], [1, HARRY, 10, '2015-10-12 23:21:27+00:00'], [1, CAPTAIN_AMERICA, 10, '2014-10-12 23:20:27+00:00'], [1, ALIEN, 6, '2015-10-12 23:22:27+00:00'], [1, JACQUES, 6, '2015-10-12 11:20:27+00:00'], [2, STAR_WARS, 10, '2013-10-12 23:20:27+00:00'], [2, WONDER_WOMAN, 10, '2014-10-12 23:20:27+00:00'], [2, AVENGERS, 9, '2016-10-12 23:20:27+00:00'], [2, PIRATES_OF, 6, '2010-10-12 23:20:27+00:00'], [2, CAPTAIN_AMERICA, 10, '2005-10-12 23:20:27+00:00'], [2, DR_STRANGELOVE, 10, '2015-01-12 23:20:27+00:00'], [3, STAR_WARS, 9, '2013-10-12 20:20:27+00:00'], [3, AVENGERS, 10, '2015-10-12 10:20:27+00:00'], [3, PIRATES_OF, 9, '2013-03-12 23:20:27+00:00'], [3, HARRY, 8, '2016-10-13 23:20:27+00:00'], [3, DR_STRANGELOVE, 10, '2016-09-12 23:20:27+00:00'], ], columns=['user_id', 'movie_id', 'rating', 'rating_timestamp']) bpr = BayesianPersonalizationRanking('') bpr.initialize_factors(ratings) for d in bpr.draw(2): print(d)
def test_split_data(self): ratings = pd.DataFrame( [[1, STAR_WARS, 9, '2013-10-12 23:21:27+00:00'], [1, WONDER_WOMAN, 10, '2014-10-12 23:22:27+00:00'], [1, AVENGERS, 10, '2015-11-12 23:20:27+00:00'], [1, WOLVERINE, 8, '2015-08-12 23:20:27+00:00'], [1, PIRATES_OF, 10, '2015-10-12 22:20:27+00:00'], [1, HARRY, 10, '2015-10-12 23:21:27+00:00'], [1, CAPTAIN_AMERICA, 10, '2014-10-12 23:20:27+00:00'], [1, ALIEN, 6, '2015-10-12 23:22:27+00:00'], [1, JACQUES, 6, '2015-10-12 11:20:27+00:00'], [2, STAR_WARS, 10, '2013-10-12 23:20:27+00:00'], [2, WONDER_WOMAN, 10, '2014-10-12 23:20:27+00:00'], [2, AVENGERS, 9, '2016-10-12 23:20:27+00:00'], [2, PIRATES_OF, 6, '2010-10-12 23:20:27+00:00'], [2, CAPTAIN_AMERICA, 10, '2005-10-12 23:20:27+00:00'], [2, DR_STRANGELOVE, 10, '2015-01-12 23:20:27+00:00'], [3, STAR_WARS, 9, '2013-10-12 20:20:27+00:00'], [3, AVENGERS, 10, '2015-10-12 10:20:27+00:00'], [3, PIRATES_OF, 9, '2013-03-12 23:20:27+00:00'], [3, HARRY, 8, '2016-10-13 23:20:27+00:00'], [3, DR_STRANGELOVE, 10, '2016-09-12 23:20:27+00:00'], ], columns=['user_id', 'movie_id', 'rating', 'rating_timestamp']) bpr = BayesianPersonalizationRanking('') bpr.initialize_factors(ratings) for d in bpr.draw(2): print(d)
def evaluate_bpr_recommender(): number_of_factors = 30 number_of_iterations = 5 N = 10 save_path = './models/bpr/' timestr = time.strftime("%Y%m%d-%H%M%S") file_name = '{}-bpr-k.csv'.format(timestr) with open(file_name, 'a', 1) as logfile: logfile.write( "ar, map, mae, N, error, factors, number_of_iterations\n") builder = BayesianPersonalizationRanking(save_path) for number_of_factors in np.arange(5, 50, 5): recommender = BPRRecs(save_path + 'model/') er = EvaluationRunner(0, builder, recommender, N, params={ 'k': number_of_factors, 'num_iterations': number_of_iterations, 'save_path': save_path + 'model/' }) result = er.calculate(10, 5) map = result['map'] mae = result['mae'] ar = result['ar'] error = builder.error if builder else 0 logfile.write("{}, {}, {}, {}, {}, {}, {}\n".format( ar, map, mae, N, error, number_of_factors, number_of_iterations)) logfile.flush()
def evaluate_bpr_recommender(coverage=False): save_path = './models/bpr/' timestr = time.strftime("%Y%m%d-%H%M%S") file_name = '{}-bpr-k.csv'.format(timestr) with open(file_name, 'a', 1) as logfile: logfile.write("rak,pak,mae,k,user_coverage,movie_coverage\n") builder = BayesianPersonalizationRanking(save_path) for k in np.arange(0, 20, 2): recommender = BPRRecs() er = EvaluationRunner(0, builder, recommender, k, params={ 'k': 10, 'num_iterations': 20, 'save_path': save_path + 'model/' }) result = er.calculate(1, 5) builder = None user_coverage, movie_coverage = 0, 0 if coverage: user_coverage, movie_coverage = RecommenderCoverage( recommender).calculate_coverage(k) map = result['map'] mae = result['mae'] ar = result['ar'] logfile.write("{}, {}, {}, {}, {}, {}\n".format( ar, map, mae, k, user_coverage, movie_coverage)) logfile.flush()