def test_FriendSuggestion_have_direction(self): train_uu = matrix2dataframe(train_mat_yes) train_ui = raw2std(train_user_item) result = RecommendSocial(train_uu, train_ui, N=4) print(result) self.assertEqual(result["user1"][0], 7) self.assertEqual(result["user9"][3], 7)
def test_recommend_cold_start(self): train = raw2std(train1) cold_start_items = [] for i in train.index: if train.ix[i].any() == 0: cold_start_items.append(i) result = RecommendRandom(cold_start_items, 3) self.assertEqual(len(result), 3) self.assertGreater(result[1], 4)
def test_PersonalRank(self): train = { # 1 2 3 4 5 6 7 8 9 10 "user1": [1, 1, 1, 1, 0, 0, 1, 0, 0, 1], "user2": [1, 0, 0, 0, 1, 0, 1, 0, 1, 1], "user3": [0, 1, 1, 1, 0, 1, 0, 0, 0, 0], "user4": [0, 1, 1, 0, 1, 0, 1, 0, 1, 0], "user5": [1, 1, 1, 0, 1, 0, 0, 1, 0, 1] } std_index = [ "item1", "item2", "item3", "item4", "item5", "item6", "item7", "item8", "item9", "item10" ] train = raw2std(train, index=std_index) rank = PersonalRank(train, p=0.8, repeat_times=10000) self.assertAlmostEqual(rank["user1"]["item1"], 0) #注意,随机游走算法的结果会变化,但是随着训练次数的增多,理论上应该不会错 self.assertGreater(rank["user1"]["item5"], 0.4) recommend = FilterAndSort(train, rank) self.assertEqual(recommend["user1"][0], "item5")
"user2": ["item1", "item4", "item5"], "user3": ["item2", "item6", "item7"], "user4": ["item3", "item4", "item6"], "user5": ["item5", "item6", "item7"] } raw_data2 = { "user1": [1, 1, 1, 0, 0, 0, 0], "user2": [1, 0, 0, 1, 1, 0, 0], "user3": [0, 1, 0, 0, 0, 1, 1], "user4": [0, 0, 1, 1, 0, 1, 0], "user5": [0, 0, 0, 0, 1, 1, 1] } std_index = ["item1", "item2", "item3", "item4", "item5", "item6", "item7"] std_data = raw2std(raw_data2, index=std_index) ''' class SimilarityTest(unittest.TestCase): def test_usersimilarity(self): train = std_data similarity = UserSimilarityCF(train) self.assertEqual(similarity["user1"]["user5"], 0) self.assertAlmostEqual(similarity["user1"]["user2"], 0.3333333) def test_usersimilarity_down_hot(self): train = { # 1 2 3 4 5 6 7 8 9 10 "user1": [1, 1, 1, 1, 0, 0, 1, 0, 0, 1], "user2": [1, 0, 0, 0, 1, 0, 1, 0, 1, 1], "user3": [0, 1, 1, 1, 0, 1, 0, 0, 0, 0],
def test_FriendSuggestion_no_direction(self): train_uu = graph2dataframe(raw_data_no) train_ui = raw2std(train_user_item) result = RecommendSocial(train_uu, train_ui, N=4) self.assertEqual(result["user1"][0], 7) self.assertEqual(result["user9"][3], 7)
def test_most_hot(self): train = raw2std(train1) result = RecommendMostHotEver(train) self.assertEqual(result[0], 1)