def test_nx_page_rank(self): # Because graph based recommendation needs to have all items to predict in the ratings dataframe recs_number = 1 graph = NXFullGraph(ratings) alg = NXPageRank() rs = GraphBasedRS(alg, graph) # Test prediction and ranking with the Page Rank algorithm, prediction will raise exception # since it's not a PredictionAlgorithm with self.assertRaises(NotPredictionAlg): rs.fit_predict('A000') result_rank = rs.fit_rank('A000') self.assertEqual(len(result_rank), 3) # Test prediction and ranking with the Page Rank algorithm on specified items, prediction will raise exception # since it's not a PredictionAlgorithm with self.assertRaises(NotPredictionAlg): rs.fit_predict('A000', filter_list=self.filter_list) result_rank_filtered = rs.fit_rank('A000', filter_list=self.filter_list) self.assertEqual(len(result_rank_filtered), 2) # Test top-n ranking with the Page Rank algorithm result_rank_numbered = rs.fit_rank('A000', recs_number=recs_number) self.assertEqual(len(result_rank_numbered), recs_number)
def test_fit_graph_w_testrating_methodology(self): graph = NXFullGraph(ratings) rs = GraphBasedRS(NXPageRank(), graph) em = EvalModel(rs, KFoldPartitioning(), metric_list=[Precision()]) sys_result, users_result = em.fit() self.assertIsInstance(sys_result, pd.DataFrame) self.assertIsInstance(users_result, pd.DataFrame)
def test_multiple(self): recs_number = 3 user_id_list = ['A000', 'A001'] # Test prediction and ranking with the Classifier Recommender algorithm graph = NXFullGraph(ratings) alg = NXPageRank() rs = GraphBasedRS(alg, graph) # Prediction with self.assertRaises(NotPredictionAlg): rs.multiple_fit_predict(user_id_list, filter_list=self.filter_list) # Test ranking with the Classifier Recommender algorithm on specified items result_rank_filtered = rs.multiple_fit_rank( user_id_list, filter_list=self.filter_list) self.assertEqual(set(user_id_list), set(result_rank_filtered['from_id'])) for user in user_id_list: self.assertEqual( len(result_rank_filtered.query('from_id == @user')), len(self.filter_list)) # Test top-n ranking with the Classifier Recommender algorithm result_rank_numbered = rs.multiple_fit_rank(user_id_list, recs_number=recs_number) self.assertEqual(set(user_id_list), set(result_rank_numbered['from_id'])) for user in user_id_list: self.assertEqual( len(result_rank_numbered.query('from_id == @user')), recs_number)
def test_graph(self): catalog = set(ratings.to_id) users_dir = os.path.join(dir_test_files, 'complex_contents', 'users_codified/') graph = NXFullGraph( ratings, user_contents_dir=users_dir, item_contents_dir=items_dir, item_exo_representation="dbpedia", user_exo_representation='local', item_exo_properties=['starring'], user_exo_properties=['1' ] # It's the column in the users .DAT which # identifies the gender ) graph_rs = GraphBasedRS(NXPageRank(), graph) em = EvalModel(graph_rs, KFoldPartitioning(), metric_list=[ Precision(relevant_threshold=3), Recall(), FMeasure(beta=1), FMeasure(beta=2, sys_average='micro'), MRR(), Correlation('pearson'), GiniIndex(), DeltaGap({ 'popular': 0.5, 'niche': 0.5 }), PredictionCoverage(catalog), PopProfileVsRecs(user_groups={ 'popular': 0.5, 'niche': 0.5 }, out_dir='plots/'), LongTailDistr('plots/', format='svg'), PopRecsCorrelation('plots/') ], verbose_predictions=True, methodology=TestItemsMethodology()) em.fit()
def test_fit_graph_w_allitems_methodology(self): graph = NXFullGraph(ratings) rs = GraphBasedRS(NXPageRank(), graph) items = set([ os.path.splitext(f)[0] for f in os.listdir(items_dir) if os.path.isfile(os.path.join(items_dir, f)) and f.endswith('xz') ]) em = EvalModel(rs, KFoldPartitioning(), metric_list=[Precision()], methodology=AllItemsMethodology(items)) sys_result, users_result = em.fit() self.assertIsInstance(sys_result, pd.DataFrame) self.assertIsInstance(users_result, pd.DataFrame)
def test_calc_rank_graph_based(self): graph = NXFullGraph(self.ratings_original) recsys = GraphBasedRS(NXPageRank(), graph) # We just need a Metric of the RankingNeededMetric class to test metric_list = [NDCG()] valid_metric = PredictionCalculator(self.split_list, recsys).calc_predictions( self.test_items_list, metric_list) rank_truth = RankingNeededMetric.rank_truth_list # We expect this to be empty, since there are no ScoresNeededMetric in the metric list score_truth = ScoresNeededMetric.score_truth_list self.assertEqual(valid_metric, metric_list) self.assertGreater(len(rank_truth), 0) self.assertEqual(len(score_truth), 0)