def test_fit(self): """ [recommendation.models.TensorCoFi] Test size of matrix after tensorCoFi fit """ tf = TensorCoFi(n_users=len(self.df.user.unique()), n_items=len(self.df.item.unique()), 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])
def test_tensor_score_against_testfm(self): """ [recommendation.models.TensorCoFi] Test tensorcofi scores with test.fm benchmark """ evaluator = Evaluator() tc = TensorCoFi(n_users=len(self.df.user.unique()), n_items=len(self.df.item.unique()), n_factors=2) ptc = PyTensorCoFi() training, testing = testfm.split.holdoutByRandom(self.df, 0.9) items = training.item.unique() tc.fit(training) ptc.fit(training) tc_score = evaluator.evaluate_model(tc, testing, all_items=items)[0] ptc_score = evaluator.evaluate_model(ptc, testing, all_items=items)[0] assert abs(tc_score-ptc_score) < .15, \ "TensorCoFi score is not close enough to testfm benchmark (%.3f != %.3f)" % (tc_score, ptc_score)