def test_zero_known_item_scores(): train = get_random_coo_matrix().tocsr() predictions = np.random.random_sample(train.shape) r = BaseRecommender() safe = r._zero_known_item_scores(predictions,train) num_users,num_items = predictions.shape for u in xrange(num_users): for i in xrange(num_items): if i in train[u].indices: assert_less_equal(safe[u,i],0) else: assert_equal(safe[u,i],predictions[u,i])
def test_zero_known_item_scores(): train = get_random_coo_matrix().tocsr() predictions = np.random.random_sample(train.shape) r = BaseRecommender() safe = r._zero_known_item_scores(predictions, train) num_users, num_items = predictions.shape for u in xrange(num_users): for i in xrange(num_items): if i in train[u].indices: assert_less_equal(safe[u, i], 0) else: assert_equal(safe[u, i], predictions[u, i])
def test_save_load(): r = save_load(BaseRecommender()) assert_equal(type(r), BaseRecommender) r = MyRecommender() r2 = save_load(r) assert_equal(type(r2), type(r)) assert_array_equal(r2.foo, r.foo) assert_equal(r2.description, r.description)
def load_recommender(filepath): """ Load a recommender model from file after it has been saved by save_recommender(). Parameters ---------- filepath : str The filepath to read from. """ return BaseRecommender.load(filepath)
def read_recommender_description(filepath): """ Read a recommender model description from file after it has been saved by save_recommender(), without loading all the associated data into memory. Parameters ---------- filepath : str The filepath to read from. """ return BaseRecommender.read_recommender_description(filepath)
def test_read_recommender_description(): check_read_description(BaseRecommender()) check_read_description(MyRecommender())
def test_save_filepath_condition(): r = BaseRecommender() invalid_filepath = 'no suffix' assert_raises(ValueError, r.save, invalid_filepath)
def check_read_description(r): f, path = tempfile.mkstemp(suffix='.npz') r.save(path) d = BaseRecommender.read_recommender_description(path) assert_equal(str(r), d)
def save_load(r): f, path = tempfile.mkstemp(suffix='.npz') r.save(path) return BaseRecommender.load(path)
def check_read_description(r): f,path = tempfile.mkstemp(suffix='.npz') r.save(path) d = BaseRecommender.read_recommender_description(path) assert_equal(str(r),d)
def save_load(r): f,path = tempfile.mkstemp(suffix='.npz') r.save(path) return BaseRecommender.load(path)