def test_random_rec_from_candidates(): algo = basic.Random() items = lktu.ml_test.ratings['item'].unique() users = lktu.ml_test.ratings['user'].unique() user1, user2 = np.random.choice(users, size=2, replace=False) algo.fit(lktu.ml_test.ratings) # recommend from candidates candidates = np.random.choice(items, 500, replace=False) recs1 = algo.recommend(user1, 100, candidates=candidates) recs2 = algo.recommend(user2, 100, candidates=candidates) assert len(recs1) == 100 assert len(recs2) == 100
def test_random_derive_seed(): algo = basic.Random(rng_spec='user') model = algo.fit(lktu.ml_test.ratings) items = lktu.ml_test.ratings['item'].unique() nitems = len(items) assert model is not None recs1 = algo.recommend(2038, 100) recs2 = algo.recommend(2028, 100) assert len(recs1) == 100 assert len(recs2) == 100 # with very high probabilities assert set(recs1['item']) != set(recs2['item']) recs_all = algo.recommend(2038) assert len(recs_all) == nitems assert set(items) == set(recs_all['item'])
def test_random(): # test case: no seed algo = basic.Random() model = algo.fit(lktu.ml_test.ratings) items = lktu.ml_test.ratings['item'].unique() users = lktu.ml_test.ratings['user'].unique() nitems = len(items) nusers = len(users) assert model is not None recs1 = algo.recommend(2038, 100) recs2 = algo.recommend(2028, 100) assert len(recs1) == 100 assert len(recs2) == 100 # with very high probabilities assert set(recs1['item']) != set(recs2['item']) recs_all = algo.recommend(2038) assert len(recs_all) == nitems assert set(items) == set(recs_all['item'])
from lenskit import crossfold as xf from lenskit.algorithms import basic, Recommender, als, item_knn, user_knn from lenskit.algorithms import funksvd from lenskit.algorithms.implicit import BPR from lenskit.algorithms.hpf import HPF from sklearn.model_selection import train_test_split import pandas as pd import matplotlib NUM_OF_RECS = 10 # num of items to rec for each user ml1m = ML1M('../datasets/ml-1m') ratings = ml1m.ratings random = basic.Random() popular = basic.Popular() 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)