def test_dynamic_updates(self): """ TensorCoFi dynamic update We will take a tensor cofi. Train the model, evaluate it. Then we remove all the user factors and recompute them using the online_user_factors to check if the performance is almost the same... """ pyTF = PyTensorCoFi(n_factors=20, n_iterations=5, c_lambda=0.05, c_alpha=40) evaluator = Evaluator() tf = TensorCoFi(n_factors=2, n_iterations=100, c_lambda=0.05, c_alpha=40) df = pd.read_csv(resource_filename(testfm.__name__, "data/movielenshead.dat"), sep="::", header=None, names=["user", "item", "rating", "date", "title"]) training, testing = testfm.split.holdoutByRandom(df, 0.7) users = {user: list(entries) for user, entries in training.groupby("user")["item"]} tf.fit(training) map1 = evaluator.evaluate_model(tf, testing) # map of the original model #now we try to replace the original factors with on the fly computed factors #lets iterate over the training data of items and the users for u, items in users.items(): #user id in the tf uid = tf.data_map[tf.get_user_column()][u] # user id iids = [tf.data_map[tf.get_item_column()][i] for i in items] # item ids that user has seen #original_factors = tf.factors["user"][uid] new_factors = pyTF.online_user_factors(tf.factors[1], iids, p_param=40, lambda_param=0.05) #replace original factors with the new factors tf.factors[0][uid, :] = new_factors #tf.update_user_factors(uid, new_factors) #lets evaluate the new model with real-time updated factors map2 = evaluator.evaluate_model(tf, testing) #The difference should be smaller than 20% assert abs(map1[0]-map2[0]) < 0.2*map1[0]
def test_dynamic_updates(self): """ TensorCoFi dynamic update We will take a tensor cofi. Train the model, evaluate it. Then we remove all the user factors and recompute them using the online_user_factors to check if the performance is almost the same... """ pyTF = PyTensorCoFi(n_factors=20, n_iterations=5, c_lambda=0.05, c_alpha=40) evaluator = Evaluator() tf = TensorCoFi(n_factors=2, n_iterations=100, c_lambda=0.05, c_alpha=40) df = pd.read_csv(resource_filename(testfm.__name__, "data/movielenshead.dat"), sep="::", header=None, names=["user", "item", "rating", "date", "title"]) training, testing = testfm.split.holdoutByRandom(df, 0.7) users = { user: list(entries) for user, entries in training.groupby("user")["item"] } tf.fit(training) map1 = evaluator.evaluate_model(tf, testing) # map of the original model #now we try to replace the original factors with on the fly computed factors #lets iterate over the training data of items and the users for u, items in users.items(): #user id in the tf uid = tf.data_map[tf.get_user_column()][u] # user id iids = [tf.data_map[tf.get_item_column()][i] for i in items] # item ids that user has seen #original_factors = tf.factors["user"][uid] new_factors = pyTF.online_user_factors(tf.factors[1], iids, p_param=40, lambda_param=0.05) #replace original factors with the new factors tf.factors[0][uid, :] = new_factors #tf.update_user_factors(uid, new_factors) #lets evaluate the new model with real-time updated factors map2 = evaluator.evaluate_model(tf, testing) #The difference should be smaller than 20% assert abs(map1[0] - map2[0]) < 0.2 * map1[0]
def test_refit(self): """ [TensorCoFi] Test fit after first fitting """ tf = TensorCoFi(n_factors=2) tf.fit(self.df) tf.fit(self.df) # Second fit #item and user are row vectors self.assertEqual(len(self.df.user.unique()), tf.factors[0].shape[0]) self.assertEqual(len(self.df.item.unique()), tf.factors[1].shape[0]) tf.fit(self.df) # Third fit #item and user are row vectors self.assertEqual(len(self.df.user.unique()), tf.factors[0].shape[0]) self.assertEqual(len(self.df.item.unique()), tf.factors[1].shape[0])
def test_score(self): tf = TensorCoFi(n_factors=2) inp = [{"user": 10, "item": 100}, {"user": 10, "item": 110}, {"user": 12, "item": 120}] inp = pd.DataFrame(inp) tf.fit(inp) uid = tf.data_map[tf.get_user_column()][10] iid = tf.data_map[tf.get_item_column()][100] tf.factors[0][uid, 0] = 0 tf.factors[0][uid, 1] = 1 tf.factors[1][iid, 0] = 1 tf.factors[1][iid, 1] = 5 self.assertEqual(0*1+1*5, tf.get_score(10, 100))
def test_ids_returns(self): tf = TensorCoFi(n_factors=2) inp = [{"user": 10, "item": 100}, {"user": 10, "item": 110}, {"user": 12, "item": 120}] inp = pd.DataFrame(inp) tf.fit(inp) # Test the id in map uid = tf.data_map[tf.get_user_column()][10] iid = tf.data_map[tf.get_item_column()][100] self.assertEquals(uid, 0) self.assertEquals(iid, 0) # Test number of factors self.assertEquals(len(tf.factors[0][uid, :]), tf.number_of_factors) self.assertEquals(len(tf.factors[1][iid, :]), tf.number_of_factors)
def test_score(self): tf = TensorCoFi(n_factors=2) inp = [{ "user": 10, "item": 100 }, { "user": 10, "item": 110 }, { "user": 12, "item": 120 }] inp = pd.DataFrame(inp) tf.fit(inp) uid = tf.data_map[tf.get_user_column()][10] iid = tf.data_map[tf.get_item_column()][100] tf.factors[0][uid, 0] = 0 tf.factors[0][uid, 1] = 1 tf.factors[1][iid, 0] = 1 tf.factors[1][iid, 1] = 5 self.assertEqual(0 * 1 + 1 * 5, tf.get_score(10, 100))
def test_ids_returns(self): tf = TensorCoFi(n_factors=2) inp = [{ "user": 10, "item": 100 }, { "user": 10, "item": 110 }, { "user": 12, "item": 120 }] inp = pd.DataFrame(inp) tf.fit(inp) # Test the id in map uid = tf.data_map[tf.get_user_column()][10] iid = tf.data_map[tf.get_item_column()][100] self.assertEquals(uid, 0) self.assertEquals(iid, 0) # Test number of factors self.assertEquals(len(tf.factors[0][uid, :]), tf.number_of_factors) self.assertEquals(len(tf.factors[1][iid, :]), tf.number_of_factors)
def test_fit(self): tf = TensorCoFi(n_factors=2) tf.fit(self.df) #item and user are row vectors self.assertEqual(len(self.df.user.unique()), tf.factors[0].shape[0]) self.assertEqual(len(self.df.item.unique()), tf.factors[1].shape[0])