def test_implicit_bpr_train_rec(): algo = BPR(25) ratings = lktu.ml_pandas.renamed.ratings algo.fit(ratings) recs = algo.recommend(100, n=20) assert len(recs) == 20
def test_implicit_bpr_train_rec(): algo = BPR(25) assert algo.factors == 25 ratings = lktu.ml_pandas.renamed.ratings algo.fit(ratings) recs = algo.recommend(100, n=20) assert len(recs) == 20 _log.info('serializing implicit model') mod = pickle.dumps(algo) _log.info('serialized to %d bytes') a2 = pickle.loads(mod) r2 = a2.recommend(100, n=20) assert len(r2) == 20 assert all(r2 == recs)
def test_implicit_pickle_untrained(tmp_path): mf = tmp_path / 'bpr.dat' algo = BPR(25) with mf.open('wb') as f: pickle.dump(algo, f) with mf.open('rb') as f: a2 = pickle.load(f) assert a2 is not algo assert a2.factors == 25
def get_topn_algo_class(algo): if algo == 'popular': return basic.Popular() elif algo == 'bias': return basic.TopN(basic.Bias()) elif algo == 'itemitem': return basic.TopN(iknn.ItemItem(nnbrs=-1, center=False, aggregate='sum')) elif algo == 'useruser': return basic.TopN(uknn.UserUser(nnbrs=5, center=False, aggregate='sum')) elif algo == 'biasedmf': return basic.TopN(als.BiasedMF(50, iterations=10)) elif algo == 'implicitmf': return basic.TopN(als.ImplicitMF(20, iterations=10)) elif algo == 'funksvd': return basic.TopN(svd.FunkSVD(20, iterations=20)) elif algo == 'bpr': return basic.TopN(BPR(25))
def test_implicit_bpr_train_rec(): algo = BPR(25, use_gpu=False) assert algo.factors == 25 ratings = lktu.ml_test.ratings algo.fit(ratings) recs = algo.recommend(100, n=20) assert len(recs) == 20 preds = algo.predict_for_user(100, [20, 30, 23148010]) assert all(preds.index == [20, 30, 23148010]) assert all(preds.isna() == [False, False, True]) _log.info('serializing implicit model') mod = pickle.dumps(algo) _log.info('serialized to %d bytes') a2 = pickle.loads(mod) r2 = a2.recommend(100, n=20) assert len(r2) == 20 assert all(r2 == recs)
pruned_data_5.reset_index(inplace=True) #pairs_user = list(partition_users(pruned_data_5, 5, xf.SampleN(1))) pairs_user = list(sample_users(pruned_data_5, 5, 12000, xf.SampleN(1))) pickle_out = open("sample_user.pickle", "wb") pickle.dump(pairs_user, pickle_out) pickle_out.close() truth = pd.concat((p.test for p in pairs_user)) #truth.to_csv(r'results/steam/pruned_5.csv') def algo_eval(path, algo, dataset): evaluation = batch.MultiEval(path=path, predict=False, recommend=100) evaluation.add_algorithms(algos=algo) evaluation.add_datasets(data=dataset) evaluation.run() algo_ii = item_knn.ItemItem(20, center=False, aggregate='sum') #algo_uu = user_knn.UserUser(30, center=False, aggregate='sum') algo_pop = basic.Popular() algo_mf = ImplicitMF(40) algo_bpr = BPR() algo_tf_idf = tf_idf() algo_LDA = LDA() algo_eval('results/steam/all_algo_sample_user', [algo_LDA, algo_tf_idf, algo_ii, algo_pop, algo_mf, algo_bpr], pairs_user)
item_to_item_100 = item_knn.ItemItem(100) item_to_item_200 = item_knn.ItemItem(200) item_to_item_500 = item_knn.ItemItem(500) user_to_user_100 = user_knn.UserUser(100) user_to_user_200 = user_knn.UserUser(200) user_to_user_500 = user_knn.UserUser(500) biased_mf_50 = als.BiasedMF(50) biased_mf_100 = als.BiasedMF(100) biased_mf_200 = als.BiasedMF(200) implicit_mf_50 = als.ImplicitMF(50) implicit_mf_100 = als.ImplicitMF(100) implicit_mf_200 = als.ImplicitMF(200) funk_svd_mf_50 = funksvd.FunkSVD(50) funk_svd_mf_100 = funksvd.FunkSVD(100) funk_svd_mf_200 = funksvd.FunkSVD(200) bayesian = BPR() hierarchical_poisson_fact_50 = HPF(50) hierarchical_poisson_fact_100 = HPF(100) hierarchical_poisson_fact_200 = HPF(200) train, test = train_test_split(ratings[['user', 'item', 'rating']], test_size=0.2) eval = batch.MultiEval('../recs/cf', recommend=NUM_OF_RECS) eval.add_datasets((train, test), name='ml-1m') eval.add_algorithms(random, name='random') eval.add_algorithms(popular, name='popular') eval.add_algorithms(item_to_item_100, name='item_to_item_100') eval.add_algorithms(item_to_item_200, name='item_to_item_200') eval.add_algorithms(item_to_item_500, name='item_to_item_500') eval.add_algorithms(user_to_user_100, name='user_to_user_100')
def default(implicit): return BPR(50)
def instantiate(opts, implicit): feats, reg, lrate = opts return BPR(feats, regularization=reg, learning_rate=lrate)
""" Code for optimizing the item-item algorithm. """ from skopt.space import Integer, Real from lenskit.algorithms.implicit import BPR from bookgender.config import have_gpu dimensions = [Integer(5, 250), Real(0, 0.25), Real(0, 0.1)] def instantiate(opts, implicit): feats, reg, lrate = opts return BPR(feats, regularization=reg, learning_rate=lrate) def default(implicit): return BPR(50) update = None sweep_points = [ BPR(factors=nf, use_gpu=have_gpu()) for nf in range(25, 250, 25) ] sweep_attrs = ['factors']