def test_deprecated_way(): """Test all Dataset constructors without passing rating_scale as a parameter. Make sure we revert back to the Reader object, with a warning message. Also, make sure ValueError is raised if reader has no rating_scale in this context. Not using dataset fixtures here for more control. """ # test load_from_file toy_data_path = (os.path.dirname(os.path.realpath(__file__)) + '/custom_dataset') with pytest.warns(UserWarning): reader = Reader(line_format='user item rating', sep=' ', skip_lines=3, rating_scale=(1, 5)) data = Dataset.load_from_file(file_path=toy_data_path, reader=reader) with pytest.raises(ValueError): reader = Reader(line_format='user item rating', sep=' ', skip_lines=3, rating_scale=None) data = Dataset.load_from_file(file_path=toy_data_path, reader=reader) # test load_from_folds train_file = os.path.join(os.path.dirname(__file__), './u1_ml100k_train') test_file = os.path.join(os.path.dirname(__file__), './u1_ml100k_test') with pytest.warns(UserWarning): reader = Reader(line_format='user item rating timestamp', sep='\t', rating_scale=(1, 5)) data = Dataset.load_from_folds([(train_file, test_file)], reader=reader) with pytest.raises(ValueError): reader = Reader(line_format='user item rating timestamp', sep='\t', rating_scale=None) data = Dataset.load_from_folds([(train_file, test_file)], reader=reader) # test load_from_df ratings_dict = {'itemID': [1, 1, 1, 2, 2], 'userID': [9, 32, 2, 45, '10000'], 'rating': [3, 2, 4, 3, 1]} df = pd.DataFrame(ratings_dict) with pytest.warns(UserWarning): reader = Reader(rating_scale=(1, 5)) data = Dataset.load_from_df(df[['userID', 'itemID', 'rating']], reader=reader) with pytest.raises(ValueError): reader = Reader(rating_scale=None) data = Dataset.load_from_df(df[['userID', 'itemID', 'rating']], # noqa reader=reader)
def test_gridsearchcv_same_splits(): """Ensure that all parameter combinations are tested on the same splits (we check their RMSE scores are the same once averaged over the splits, which should be enough). We use as much parallelism as possible.""" data_file = os.path.join(os.path.dirname(__file__), './u1_ml100k_test') data = Dataset.load_from_file(data_file, reader=Reader('ml-100k'), rating_scale=(1, 5)) kf = KFold(3, shuffle=True, random_state=4) # all RMSE should be the same (as param combinations are the same) param_grid = {'n_epochs': [5], 'lr_all': [.2, .2], 'reg_all': [.4, .4], 'n_factors': [5], 'random_state': [0]} gs = GridSearchCV(SVD, param_grid, measures=['RMSE'], cv=kf, n_jobs=1) gs.fit(data) rmse_scores = [m for m in gs.cv_results['mean_test_rmse']] assert len(set(rmse_scores)) == 1 # assert rmse_scores are all equal # Note: actually, even when setting random_state=None in kf, the same folds # are used because we use product(param_comb, kf.split(...)). However, it's # needed to have the same folds when calling fit again: gs.fit(data) rmse_scores += [m for m in gs.cv_results['mean_test_rmse']] assert len(set(rmse_scores)) == 1 # assert rmse_scores are all equal
def small_ml(): """Return a Dataset object with 2000 movielens-100k ratings. """ data_file = os.path.join(os.path.dirname(__file__), './u1_ml100k_test') data = Dataset.load_from_file(data_file, Reader('ml-100k'), rating_scale=(1, 5)) return data
def toy_data(toy_data_reader): toy_data_path = (os.path.dirname(os.path.realpath(__file__)) + '/custom_dataset') data = Dataset.load_from_file(file_path=toy_data_path, reader=toy_data_reader, rating_scale=(1, 5)) return data
def __init__(self, algo: AlgoBase, path: str=None, fmt='user item rating', sep=','): self.algo = algo if path: self.data = Dataset.load_from_file(path, reader=Reader(line_format=fmt, sep=sep, skip_lines=1)) else: self.data = None self.trainset = None self.init()
def test_randomizedsearchcv_cv_results(): """Test the cv_results attribute""" f = os.path.join(os.path.dirname(__file__), './u1_ml100k_test') data = Dataset.load_from_file(f, Reader('ml-100k'), rating_scale=(1, 5)) kf = KFold(3, shuffle=True, random_state=4) param_distributions = {'n_epochs': [5], 'lr_all': uniform(.2, .3), 'reg_all': uniform(.4, .3), 'n_factors': [5], 'random_state': [0]} n_iter = 5 rs = RandomizedSearchCV(SVD, param_distributions, n_iter=n_iter, measures=['RMSE', 'mae'], cv=kf, return_train_measures=True) rs.fit(data) # test keys split*_test_rmse, mean and std dev. assert rs.cv_results['split0_test_rmse'].shape == (n_iter,) assert rs.cv_results['split1_test_rmse'].shape == (n_iter,) assert rs.cv_results['split2_test_rmse'].shape == (n_iter,) assert rs.cv_results['mean_test_rmse'].shape == (n_iter,) assert np.allclose(rs.cv_results['mean_test_rmse'], np.mean([rs.cv_results['split0_test_rmse'], rs.cv_results['split1_test_rmse'], rs.cv_results['split2_test_rmse']], axis=0)) assert np.allclose(rs.cv_results['std_test_rmse'], np.std([rs.cv_results['split0_test_rmse'], rs.cv_results['split1_test_rmse'], rs.cv_results['split2_test_rmse']], axis=0)) # test keys split*_train_mae, mean and std dev. assert rs.cv_results['split0_train_rmse'].shape == (n_iter,) assert rs.cv_results['split1_train_rmse'].shape == (n_iter,) assert rs.cv_results['split2_train_rmse'].shape == (n_iter,) assert rs.cv_results['mean_train_rmse'].shape == (n_iter,) assert np.allclose(rs.cv_results['mean_train_rmse'], np.mean([rs.cv_results['split0_train_rmse'], rs.cv_results['split1_train_rmse'], rs.cv_results['split2_train_rmse']], axis=0)) assert np.allclose(rs.cv_results['std_train_rmse'], np.std([rs.cv_results['split0_train_rmse'], rs.cv_results['split1_train_rmse'], rs.cv_results['split2_train_rmse']], axis=0)) # test fit and train times dimensions. assert rs.cv_results['mean_fit_time'].shape == (n_iter,) assert rs.cv_results['std_fit_time'].shape == (n_iter,) assert rs.cv_results['mean_test_time'].shape == (n_iter,) assert rs.cv_results['std_test_time'].shape == (n_iter,) assert rs.cv_results['params'] is rs.param_combinations # assert that best parameter in rs.cv_results['rank_test_measure'] is # indeed the best_param attribute. best_index = np.argmin(rs.cv_results['rank_test_rmse']) assert rs.cv_results['params'][best_index] == rs.best_params['rmse'] best_index = np.argmin(rs.cv_results['rank_test_mae']) assert rs.cv_results['params'][best_index] == rs.best_params['mae']
def test_nearest_neighbors(): """Ensure the nearest neighbors are different when using user-user similarity vs item-item.""" reader = Reader(line_format='user item rating', sep=' ', skip_lines=3) data_file = os.path.dirname(os.path.realpath(__file__)) + '/custom_train' data = Dataset.load_from_file(data_file, reader, rating_scale=(1, 5)) trainset = data.build_full_trainset() algo_ub = KNNBasic(sim_options={'user_based': True}) algo_ub.fit(trainset) algo_ib = KNNBasic(sim_options={'user_based': False}) algo_ib.fit(trainset) assert algo_ub.get_neighbors(0, k=10) != algo_ib.get_neighbors(0, k=10)
def test_randomizedsearchcv_refit(u1_ml100k): """Test refit method of RandomizedSearchCV class.""" data_file = os.path.join(os.path.dirname(__file__), './u1_ml100k_test') data = Dataset.load_from_file(data_file, Reader('ml-100k'), rating_scale=(1, 5)) param_distributions = {'n_epochs': [5], 'lr_all': uniform(0.002, 0.003), 'reg_all': uniform(0.4, 0.2), 'n_factors': [2]} # assert rs.fit() and rs.test will use best estimator for mae (first # appearing in measures) rs = RandomizedSearchCV(SVD, param_distributions, measures=['mae', 'rmse'], cv=2, refit=True) rs.fit(data) rs_preds = rs.test(data.construct_testset(data.raw_ratings)) mae_preds = rs.best_estimator['mae'].test( data.construct_testset(data.raw_ratings)) assert rs_preds == mae_preds # assert rs.fit() and rs.test will use best estimator for rmse rs = RandomizedSearchCV(SVD, param_distributions, measures=['mae', 'rmse'], cv=2, refit='rmse') rs.fit(data) rs_preds = rs.test(data.construct_testset(data.raw_ratings)) rmse_preds = rs.best_estimator['rmse'].test( data.construct_testset(data.raw_ratings)) assert rs_preds == rmse_preds # test that predict() can be called rs.predict(2, 4) # assert test() and predict() cannot be used when refit is false rs = RandomizedSearchCV(SVD, param_distributions, measures=['mae', 'rmse'], cv=2, refit=False) rs.fit(data) with pytest.raises(ValueError): rs.test(data.construct_testset(data.raw_ratings)) with pytest.raises(ValueError): rs.predict('1', '2') # test that error is raised if used with load_from_folds rs = RandomizedSearchCV(SVD, param_distributions, measures=['mae', 'rmse'], cv=2, refit=True) with pytest.raises(ValueError): rs.fit(u1_ml100k)
def test_gridsearchcv_refit(u1_ml100k): """Test refit function of GridSearchCV.""" data_file = os.path.join(os.path.dirname(__file__), './u1_ml100k_test') data = Dataset.load_from_file(data_file, Reader('ml-100k'), rating_scale=(1, 5)) param_grid = {'n_epochs': [5], 'lr_all': [0.002, 0.005], 'reg_all': [0.4, 0.6], 'n_factors': [2]} # assert gs.fit() and gs.test will use best estimator for mae (first # appearing in measures) gs = GridSearchCV(SVD, param_grid, measures=['mae', 'rmse'], cv=2, refit=True) gs.fit(data) gs_preds = gs.test(data.construct_testset(data.raw_ratings)) mae_preds = gs.best_estimator['mae'].test( data.construct_testset(data.raw_ratings)) assert gs_preds == mae_preds # assert gs.fit() and gs.test will use best estimator for rmse gs = GridSearchCV(SVD, param_grid, measures=['mae', 'rmse'], cv=2, refit='rmse') gs.fit(data) gs_preds = gs.test(data.construct_testset(data.raw_ratings)) rmse_preds = gs.best_estimator['rmse'].test( data.construct_testset(data.raw_ratings)) assert gs_preds == rmse_preds # test that predict() can be called gs.predict(2, 4) # assert test() and predict() cannot be used when refit is false gs = GridSearchCV(SVD, param_grid, measures=['mae', 'rmse'], cv=2, refit=False) gs.fit(data) with pytest.raises(ValueError): gs_preds = gs.test(data.construct_testset(data.raw_ratings)) with pytest.raises(ValueError): gs.predict('1', '2') # test that error is raised if used with load_from_folds gs = GridSearchCV(SVD, param_grid, measures=['mae', 'rmse'], cv=2, refit=True) with pytest.raises(ValueError): gs.fit(u1_ml100k)
def test_LeaveOneOut(toy_data): loo = LeaveOneOut() with pytest.raises(ValueError): next(loo.split(toy_data)) # each user only has 1 item so trainsets fail reader = Reader('ml-100k') data_path = (os.path.dirname(os.path.realpath(__file__)) + '/u1_ml100k_test') data = Dataset.load_from_file(file_path=data_path, reader=reader, rating_scale=(1, 5)) # Test random_state parameter # If random_state is None, you get different split each time (conditioned # by rng of course) loo = LeaveOneOut(random_state=None) testsets_a = [testset for (_, testset) in loo.split(data)] testsets_b = [testset for (_, testset) in loo.split(data)] assert testsets_a != testsets_b # Repeated called to split when random_state is set lead to the same folds loo = LeaveOneOut(random_state=1) testsets_a = [testset for (_, testset) in loo.split(data)] testsets_b = [testset for (_, testset) in loo.split(data)] assert testsets_a == testsets_b # Make sure only one rating per user is present in the testset loo = LeaveOneOut() for _, testset in loo.split(data): cnt = Counter([uid for (uid, _, _) in testset]) assert all(val == 1 for val in itervalues(cnt)) # test the min_n_ratings parameter loo = LeaveOneOut(min_n_ratings=5) for trainset, _ in loo.split(data): assert all(len(ratings) >= 5 for ratings in itervalues(trainset.ur)) loo = LeaveOneOut(min_n_ratings=10) for trainset, _ in loo.split(data): assert all(len(ratings) >= 10 for ratings in itervalues(trainset.ur)) loo = LeaveOneOut(min_n_ratings=10000) # too high with pytest.raises(ValueError): next(loo.split(data))
reverse=True) print() print("Genres of the top 10 movies for %d th component are:" % col) for i in range(10): genres = movies_dict['genres'] print(genres[movies_dict['movieID'].index(Sorted_V_dict[i][0])]) # Set starting timer start = timer() # Load dataset reader = Reader(line_format='user item rating timestamp', sep=',', skip_lines=1) data = Dataset.load_from_file('ml-latest-small/ratings.csv', reader=reader) # 10-fold cross validation rmse, r_list = nmf_cv(data) # get optimal r min_idx = rmse.index(min(rmse)) r_hat = r_list[min_idx] # Training and testing trainset, testset = train_test_split(data, test_size=0.2) algo = NMF(n_factors=r_hat, biased=False) algo.fit(trainset) U = algo.pu V = algo.qi predictions = algo.test(testset)
from surprise import KNNWithMeans from surprise import Dataset, print_perf, Reader from surprise.model_selection import cross_validate import os # 指定文件所在路径 file_path = os.path.expanduser('mydata.csv') # 告诉文本阅读器,文本的格式是怎么样的 reader = Reader(line_format='user item rating', sep=',') # 加载数据 data = Dataset.load_from_file(file_path, reader=reader) trainset = data.build_full_trainset() # Use user_based true/false to switch between user-based or item-based collaborative filtering algo = KNNWithMeans(k=50, sim_options={'user_based': False})#取最相似的用户进行计算时,只取最相似的k个 algo.fit(trainset) # we can now query for specific predicions uid = str(5) # raw user id iid = str(1) # raw item id # get a prediction for specific users and items. pred = algo.predict(uid, iid) print('rating of user-{0} to item-{1} is '.format(uid, iid), pred.est)# rating of user-5 to item-1 #---------------------------- uid = str(5) # raw user id iid = str(5) # raw item id # get a prediction for specific users and items. pred = algo.predict(uid, iid) print('rating of user-{0} to item-{1} is '.format(uid, iid), pred.est)
def test_ShuffleSplit(): reader = Reader(line_format='user item rating', sep=' ', skip_lines=3, rating_scale=(1, 5)) custom_dataset_path = (os.path.dirname(os.path.realpath(__file__)) + '/custom_dataset') data = Dataset.load_from_file(file_path=custom_dataset_path, reader=reader) with pytest.raises(ValueError): ss = ShuffleSplit(n_splits=0) with pytest.raises(ValueError): ss = ShuffleSplit(test_size=10) next(ss.split(data)) with pytest.raises(ValueError): ss = ShuffleSplit(train_size=10) next(ss.split(data)) with pytest.raises(ValueError): ss = ShuffleSplit(test_size=3, train_size=3) next(ss.split(data)) with pytest.raises(ValueError): ss = ShuffleSplit(test_size=3, train_size=0) next(ss.split(data)) with pytest.raises(ValueError): ss = ShuffleSplit(test_size=0, train_size=3) next(ss.split(data)) # No need to cover the entire dataset ss = ShuffleSplit(test_size=1, train_size=1) next(ss.split(data)) # test test_size to int and train_size to None (complement) ss = ShuffleSplit(test_size=1) assert all(len(testset) == 1 for (_, testset) in ss.split(data)) assert all(trainset.n_ratings == 4 for (trainset, _) in ss.split(data)) # test test_size to float and train_size to None (complement) ss = ShuffleSplit(test_size=.2) # 20% of 5 = 1 assert all(len(testset) == 1 for (_, testset) in ss.split(data)) assert all(trainset.n_ratings == 4 for (trainset, _) in ss.split(data)) # test test_size to int and train_size to int ss = ShuffleSplit(test_size=2, train_size=2) assert all(len(testset) == 2 for (_, testset) in ss.split(data)) assert all(trainset.n_ratings == 2 for (trainset, _) in ss.split(data)) # test test_size to None (complement) and train_size to int ss = ShuffleSplit(test_size=None, train_size=2) assert all(len(testset) == 3 for (_, testset) in ss.split(data)) assert all(trainset.n_ratings == 2 for (trainset, _) in ss.split(data)) # test test_size to None (complement) and train_size to float ss = ShuffleSplit(test_size=None, train_size=.2) assert all(len(testset) == 4 for (_, testset) in ss.split(data)) assert all(trainset.n_ratings == 1 for (trainset, _) in ss.split(data)) # test default parameters: 5 splits, test_size = .2, train_size = None ss = ShuffleSplit() assert len(list(ss.split(data))) == 5 assert all(len(testset) == 1 for (_, testset) in ss.split(data)) assert all(trainset.n_ratings == 4 for (trainset, _) in ss.split(data)) # Test random_state parameter # If random_state is None, you get different split each time (conditioned # by rng of course) ss = ShuffleSplit(random_state=None) testsets_a = [testset for (_, testset) in ss.split(data)] testsets_b = [testset for (_, testset) in ss.split(data)] assert testsets_a != testsets_b # Repeated called to split when random_state is set lead to the same folds ss = ShuffleSplit(random_state=1) testsets_a = [testset for (_, testset) in ss.split(data)] testsets_b = [testset for (_, testset) in ss.split(data)] assert testsets_a == testsets_b # Test shuffle parameter, if False then splits are the same regardless of # random_state. ss = ShuffleSplit(random_state=1, shuffle=False) testsets_a = [testset for (_, testset) in ss.split(data)] testsets_b = [testset for (_, testset) in ss.split(data)] assert testsets_a == testsets_b
def test_randomizedsearchcv_cv_results(): """Test the cv_results attribute""" f = os.path.join(os.path.dirname(__file__), './u1_ml100k_test') data = Dataset.load_from_file(f, Reader('ml-100k')) kf = KFold(3, shuffle=True, random_state=4) param_distributions = { 'n_epochs': [5], 'lr_all': uniform(.2, .3), 'reg_all': uniform(.4, .3), 'n_factors': [5], 'random_state': [0] } n_iter = 5 rs = RandomizedSearchCV(SVD, param_distributions, n_iter=n_iter, measures=['RMSE', 'mae'], cv=kf, return_train_measures=True) rs.fit(data) # test keys split*_test_rmse, mean and std dev. assert rs.cv_results['split0_test_rmse'].shape == (n_iter, ) assert rs.cv_results['split1_test_rmse'].shape == (n_iter, ) assert rs.cv_results['split2_test_rmse'].shape == (n_iter, ) assert rs.cv_results['mean_test_rmse'].shape == (n_iter, ) assert np.allclose( rs.cv_results['mean_test_rmse'], np.mean([ rs.cv_results['split0_test_rmse'], rs.cv_results['split1_test_rmse'], rs.cv_results['split2_test_rmse'] ], axis=0)) assert np.allclose( rs.cv_results['std_test_rmse'], np.std([ rs.cv_results['split0_test_rmse'], rs.cv_results['split1_test_rmse'], rs.cv_results['split2_test_rmse'] ], axis=0)) # test keys split*_train_mae, mean and std dev. assert rs.cv_results['split0_train_rmse'].shape == (n_iter, ) assert rs.cv_results['split1_train_rmse'].shape == (n_iter, ) assert rs.cv_results['split2_train_rmse'].shape == (n_iter, ) assert rs.cv_results['mean_train_rmse'].shape == (n_iter, ) assert np.allclose( rs.cv_results['mean_train_rmse'], np.mean([ rs.cv_results['split0_train_rmse'], rs.cv_results['split1_train_rmse'], rs.cv_results['split2_train_rmse'] ], axis=0)) assert np.allclose( rs.cv_results['std_train_rmse'], np.std([ rs.cv_results['split0_train_rmse'], rs.cv_results['split1_train_rmse'], rs.cv_results['split2_train_rmse'] ], axis=0)) # test fit and train times dimensions. assert rs.cv_results['mean_fit_time'].shape == (n_iter, ) assert rs.cv_results['std_fit_time'].shape == (n_iter, ) assert rs.cv_results['mean_test_time'].shape == (n_iter, ) assert rs.cv_results['std_test_time'].shape == (n_iter, ) assert rs.cv_results['params'] is rs.param_combinations # assert that best parameter in rs.cv_results['rank_test_measure'] is # indeed the best_param attribute. best_index = np.argmin(rs.cv_results['rank_test_rmse']) assert rs.cv_results['params'][best_index] == rs.best_params['rmse'] best_index = np.argmin(rs.cv_results['rank_test_mae']) assert rs.cv_results['params'][best_index] == rs.best_params['mae']
from surprise import Dataset, Reader from surprise import KNNBasic, KNNWithMeans, KNNWithZScore, KNNBaseline from surprise.model_selection import KFold from surprise import accuracy reader = Reader(line_format='user item rating timestamp', sep=',', skip_lines=1) data = Dataset.load_from_file('./ratings.csv', reader=reader) # trainset = data.build_full_trainset() # ItemCF 计算得分 # 取最相似的用户计算时,只取最相似的k个 # KNNWithMeans algo = KNNWithMeans(k=50, sim_options={'user_based': False, 'verbose': 'True'}) # 定义K折交叉验证迭代器,K=3 kf = KFold(n_splits=3) # k 值越大,泛化结果越好,但是时间也越长 for trainset, testset in kf.split(data): # 训练并测试 algo.fit(trainset) predictions = algo.test(testset) # 计算RSME accuracy.rmse(predictions, verbose=True) accuracy.mae(predictions, verbose=True) # 196这个用户对于302这个电影的预测分数是怎么样的 uid = str(196) iid = str(302) #输出uid对iid的预测结果,原来的实际值r_ui是4分
data2 = data2.reset_index() indices = pd.Series(data2.index, index = data2['title_y']) get_recommendation('The Dark Knight Rises', cosine_sim2) get_recommendation('The Godfather', cosine_sim2) #Collaborative Filtering # 1) User based filtering ratings = pd.read_csv('ratings_small.csv') from surprise import Reader, evaluate, Dataset, SVD #surprise contains lot of dataset related to ratings which are small and easy to use reader = Reader() data = Dataset.load_from_file(ratings[['userID','movieID','rating']], reader) data.split(n_folds = 5) svd = np.linalg.SVD() evaluate(svd, data, measures = ['RMSE','MAE']) trainset = data.build_full_trainset() svd.fit(trainset) ratings(ratings["userId"]==1) svd.predict(2,303,4)
# if user not in user_average: # user_average[user] = [0, 0] # for business in user_businesses[user]: # user_average[user][0] += pair_rating[(user, business)] # user_average[user][1] += 1#{user: [sum, count]} for business in business_users.keys(): if business not in business_average: business_average[business] = [0, 0] for user in business_users[business]: business_average[business][0] += pair_rating[(user, business)] business_average[business][1] += 1 #{business: [sum, count]} reader = Reader(line_format='user item rating', sep=',', skip_lines=1) # fold_path = [(train_file, test_file)] # data = Dataset.load_from_folds(fold_path, reader=reader) data = Dataset.load_from_file(train_file, reader=reader) trainset = data.build_full_trainset() # min_rmse = 2 # min_rs = 51 # for rs in range(50,100,1): algo = SVD(n_factors=20, lr_bu=0.008, lr_bi=0.008, lr_pu=0.009, lr_qi=0.01, reg_all=0.2, n_epochs=23, random_state=21).fit(trainset) sum_error = 0 count = 0
import surprise from surprise.model_selection import train_test_split, PredefinedKFold from surprise import BaselineOnly from surprise.model_selection import cross_validate from surprise import SVD from surprise import accuracy import matplotlib.pyplot as plt dataset = 'ratings_small.csv' reader = surprise.Reader(line_format='user item rating timestamp', sep=',', skip_lines=1, rating_scale=(1, 5)) data = Dataset.load_from_file(dataset, reader=reader) trainset, testset = train_test_split(data, test_size=0.25) # algo = SVD(biased = False) # results=cross_validate(algo=algo, data=data, measures=['RMSE', 'MAE'], cv=5, return_train_measures=True) # print("Average rmse of the PMF is ",results['test_rmse'].mean()) # print("Average mae of the PMF is ",results['test_mae'].mean()) # # algo = KNNBasic() # results=cross_validate(algo=algo, data=data, measures=['RMSE', 'MAE'], cv=5, return_train_measures=True) # print("Average rmse of the user collaborative filtering is ",results['test_rmse'].mean()) # print("Average mae of the user collaborative filtering is ",results['test_mae'].mean()) # # sim_options = {'user_based': False} # algo = KNNBasic(sim_options=sim_options)
from surprise import Reader, Dataset from surprise import accuracy from surprise import SVD, evaluate reader = Reader(line_format='user item rating', sep=';', rating_scale=(0, 1)) data = Dataset.load_from_file('./tost/u.data', reader=reader) data.split(n_folds=3) algo = SVD() #evaluate(algo, data, measures=['RMSE', 'MAE']) #Retrieve the trainset : training on the whole dataset for trainset, testset in data.folds(): algo.train(trainset) predictions = algo.test(testset) rmse = accuracy.rmse(predictions, verbose=True)
import pickle from surprise import SVD, SVDpp, KNNBasic, CoClustering, NormalPredictor from surprise import Dataset, Reader from surprise import evaluate, print_perf, accuracy reader_params = dict(line_format='user item rating', rating_scale=(1, 5), sep=',') reader = Reader(**reader_params) train_data = Dataset.load_from_file( file_path='/Shared/bdagroup7/download/train.data', reader=reader) probe_data = Dataset.load_from_file( file_path='/Shared/bdagroup7/download/probe.data', reader=reader) training_set = train_data.build_full_trainset() test_set_temp = probe_data.build_full_trainset() test_set = test_set_temp.build_testset() # Save training and test_set #with open('/Shared/bdagroup7/download/test_set.dat', "wb") as f: # pickle.dump(test_set, f) #with open('/Shared/bdagroup7/download/training_set.dat', "wb") as f: # pickle.dump(training_set, f) # Read the dataset #test_set = None #training_set = None #with open('/Shared/bdagroup7/download/test_set.dat', "rb") as f: # test_set = pickle.load(f) #with open('/Shared/bdagroup7/download/training_set.dat', "rb") as f: # training_set = pickle.load(f)
df.drop(df[bool_contain_item].index, axis=0, inplace=True) df.columns = ['user_id', 'movie_id'] df.to_csv('probe.csv', header=True, index=False) return df #读取训练数据 rate_data = pd.read_table('combined_data_1.txt', sep='/t', header=None) #数据处理 data = process_df(rate_data) # 数据读取 reader = Reader(line_format='user item rating timestamp', sep=',', skip_lines=1) suprise_data = Dataset.load_from_file( '../Kaggle/netflix-prize-data/first_file.csv', reader=reader) train_set = suprise_data.build_full_trainset() # ALS优化 bsl_options = {'method': 'als', 'n_epochs': 50, 'reg_u': 12, 'reg_i': 5} # SGD优化 #bsl_options = {'method': 'sgd','n_epochs': 5} algo = BaselineOnly(bsl_options=bsl_options) #algo = BaselineOnly() #algo = NormalPredictor() # 定义K折交叉验证迭代器,K=3 kf = KFold(n_splits=3) for trainset, testset in kf.split(suprise_data): # 训练并预测 algo.fit(trainset)
def main(): raw_uid = 'b80344d063b5ccb3212f76538f3d9e43d87dca9e' dir_data = './collaborative_filtering/cf_data' recommend_number = 10 file_path = '{}/dataset_user_5.txt'.format(dir_data) file_song_path = '{}/dataset_song_5.txt'.format(dir_data) if not os.path.exists(dir_data): os.makedirs(dir_data) db = pymysql.connect("localhost", "root", "", "music_recommender", charset='utf8') cursor = db.cursor() sql = """SELECT uid, song_id, rating FROM user_rating WHERE 1""" cursor.execute(sql) results = cursor.fetchall() with open(file_path, "w+", encoding="utf-8") as data_f: for result in results: uid, song_id, rating = result data_f.writelines("{}\t{}\t{}\n".format(uid, song_id, rating)) reader = Reader(line_format='user item rating', sep='\t') data = Dataset.load_from_file(file_path, reader=reader) data_user = pd.read_csv(file_path, header=None, sep='\t', dtype={'song_id': object}) data_user.columns = ["user_id", "song_id", "rating"] types_dict = {'user_id': str, 'song_id': str, 'rating': float} for col, col_type in types_dict.items(): data_user[col] = data_user[col].astype(col_type) if not os.path.exists(file_path): raise IOError("Dataset file is not exists!") # file_path = "" sql_song = """SELECT song_id, song_name, artist_name, album_id, album_name FROM song_information WHERE 1""" cursor.execute(sql_song) results_song = cursor.fetchall() # print(results_song) with open(file_song_path, "w+", encoding='utf-8') as data_f: for result in results_song: song_id, song_name, artist_name, album_id, album_name = result # print(result) data_f.writelines("{}\t{}\t{}\t{}\t{}\n".format( song_id, song_name, artist_name, album_id, album_name)) # reader = Reader(line_format='song_id song_name artist_name album_name', sep='\t') data_song = pd.read_csv(file_song_path, header=None, sep='\t', dtype={'song_id': str}) data_song.columns = [ "song_id", "song_name", "artist_name", "album_id", "album_name" ] if not os.path.exists(file_song_path): raise IOError("Dataset file is not exists!") # file_path = "" song_df = pd.merge(data_user, data_song.drop_duplicates(['song_id']), on="song_id", how="left") train_data, test_data = train_test_split(song_df, test_size=0.20, random_state=0) ib_model = ItemBasedRecommender() # print(song_df) ib_model.read_data(train_data, "user_id", "song_id") user_items = ib_model.get_user_songs(raw_uid) # print("------------------------------------------------------------------------------------") # print("Training data songs for the user userid: %s:" % raw_uid) # print("------------------------------------------------------------------------------------") # for user_item in user_items: # print(user_item) print( "----------------------------------------------------------------------" ) print("Recommendation process going on:") print( "----------------------------------------------------------------------" ) #Recommend songs for the user using personalized model result = ib_model.recommend(raw_uid, recommend_number) print(result)
if (i[0] in userDict): userDict[i[0].strip()].append(temp) else: userDict[i[0].strip()] = [temp] # 计算ItemUser {'1',[1,2,3..],...} if (i[1] in ItemUser): ItemUser[i[1].strip()].append(i[0].strip()) else: ItemUser[i[1].strip()] = [i[0].strip()] return userDict, ItemUser # First train an SVD algorithm on the movielens dataset. # data = Dataset.load_builtin('ml-100k') reader = Reader(line_format='user item rating', sep=',') data = Dataset.load_from_file("data/csv/ratingInfo2013.csv", reader=reader) # data = Dataset.load_from_file("toySet.csv", reader=reader) trainset = data.build_full_trainset() algo = SVD() algo.fit(trainset) # Than predict ratings for all pairs (u, i) that are NOT in the training set. testset = trainset.build_anti_testset() testset = trainset.build_testset() predictions = algo.test(testset) top_n = get_top_n(predictions, n=10) ratings = readFile("data/csv/ratingInfo2013.csv") userDict, ItemUser = formatRate(ratings) # for i in self.ratings:
for uid, iid, true_r, est, _ in predictions: top_n[uid].append((iid, est)) # THEN SORT THE PREDICTIONS FOR EACH USER AND RETRIEVE THE K Highest ones # uid = 0 for iid, user_ratings in top_n.items(): user_ratings.sort(key=lambda x: x[1], reverse=True) top_n[uid] = user_ratings[:n] return top_n # ======== FUNCTION END sim_op = {'name': algorithm, 'user_based': mode} algo = KNNBasic(sim_options=sim_op) reader = Reader(line_format="user item rating", sep='\t') df0 = Dataset.load_from_file('po_cluster0.csv', reader=reader) df1 = Dataset.load_from_file('po_cluster1.csv', reader=reader) # START TRAINING trainset = df0.build_full_trainset() # APPLYING ALGORITHM KNN Basic algo.train(trainset) print "ALGORITHM USED: \n", algo testset = trainset.build_anti_testset() predictions = algo.test(testset=testset) top_n = get_top_n(predictions, 5) # ---------------------------------------------------- PREDICTION VERIFICATION - CL0 (945) print "\t\tINITIATING IN CLUSTER 0 (945)\n"
'''Testing renaming of train() into fit()''' import os import pytest from surprise import Dataset from surprise import Reader from surprise import AlgoBase data_file = os.path.join(os.path.dirname(__file__), './u1_ml100k_train') data = Dataset.load_from_file(data_file, Reader('ml-100k')) data.split(2) def test_new_style_algo(): '''Test that new algorithms (i.e. algoritms that only define fit()) can support both calls to fit() and to train() - algo.fit() is the new way of doing things - supporting algo.train() is needed for the (unlikely?) case where a user has defined custom tools that use algo.train(). ''' class CustomAlgoFit(AlgoBase): def __init__(self): AlgoBase.__init__(self) self.cnt = -1 def fit(self, trainset): AlgoBase.fit(self, trainset) self.est = 3 self.bu, self.bi = 1, 1
def rec(): reviewsPath = 'data/reviews_ssc.csv' df_reviews = pd.read_csv(reviewsPath, sep=',') df_reviews['unixReviewTime'] = pd.to_numeric(df_reviews['unixReviewTime'], errors='coerce') reader = Reader(line_format='user item rating timestamp', sep=',', rating_scale=(1, 5), skip_lines=1) reviewsData = Dataset.load_from_file(reviewsPath, reader=reader) trainset, testset = train_test_split(reviewsData, test_size=.25) """ param_grid = {'k':[40,50], 'min_k':[3,7], 'sim_options': {'name': ['msd'], 'min_support': [1,5], 'user_based': [False]}} gs = GridSearchCV(KNNWithMeans, param_grid, measures=['rmse'],cv=5) gs.fit(reviewsData) print(gs.best_score['rmse']) print(gs.best_params['rmse'])""" results = [] n_cltr_u = [3, 5, 7, 9, 11] n_cltr_i = [3, 5, 7, 9, 11] for a in n_cltr_u: for b in n_cltr_i: algo = CoClustering(n_cltr_u=a, n_cltr_i=b) predictions = algo.fit(trainset).test(testset) rmse = accuracy.rmse(predictions, verbose=False) mae = accuracy.mae(predictions, verbose=False) fcp = accuracy.fcp(predictions, verbose=False) results.append((rmse, mae, fcp, a, b)) print('{} {} {} {} {}'.format(rmse, mae, fcp, a, b)) #rows = sorted(results, key=lambda x: x[0]) df = pd.DataFrame(results, columns=['rmse', 'mae', 'fcp', 'k', 'min_k']) df.to_csv('co_clustering.csv', index=False) """ param_grid = {'lr_pu': [0.019775, 0.019825], 'reg_bi': [0.06275, 0.06325], 'reg_pu': [0.20775, 0.20825], 'lr_bu': [0.01075, 0.01125], 'lr_bi': [0.005275, 0.005325], 'reg_bu': [0.06675, 0.06725], 'reg_qi': [0.14775, 0.14825], 'lr_qi': [0.014775, 0.014825]} results = [] lr_bu = [0.001,0.005,0.01] lr_bi = [0.001,0.005,0.01] lr_pu = [0.001,0.005,0.01] lr_qi = [0.001,0.005,0.01] reg_bu = [0.005,0.02,0.05] reg_bi = [0.005,0.02,0.05] reg_pu = [0.005,0.02,0.05] reg_qi = [0.005,0.02,0.05] g = itt.product(lr_bu,lr_bi,lr_pu,lr_qi,reg_bu,reg_bi,reg_pu,reg_qi) for i in g: algo = SVD(n_factors=200,n_epochs=50,lr_bu=i[0],lr_bi=i[1],lr_pu=i[2], lr_qi=i[3],reg_bu=i[4],reg_bi=i[5],reg_pu=i[6],reg_qi=i[7]) predictions = algo.fit(trainset).test(testset) acc = accuracy.rmse(predictions, verbose=False) results.append((acc,)+i) rows = sorted(results, key=lambda x: x[0]) df = pd.DataFrame(rows, columns=['rmse','lr_bu','lr_bi','lr_pu','lr_qi', 'reg_bu','reg_bi','reg_pu','reg_qi']) df.to_csv('svd.csv',index=False)""" print('done')
from collections import defaultdict from surprise import Reader, Dataset from surprise import KNNWithMeans from surprise import accuracy from surprise.model_selection import train_test_split # Define the format reader = Reader(line_format='user item rating timestamp', sep='\t') # Load the data from the file using the reader format data = Dataset.load_from_file('ml-100k/u.data', reader=reader) def get_top_n(predictions, n=10): '''Return the top-N recommendation for each user from a set of predictions. Args: predictions(list of Prediction objects): The list of predictions, as returned by the test method of an algorithm. n(int): The number of recommendation to output for each user. Default is 10. Returns: A dict where keys are user (raw) ids and values are lists of tuples: [(raw item id, rating estimation), ...] of size n. ''' # First map the predictions to each user.
# Normailise dataset header = ['user', 'item', 'rating', 'timestamp'] ratings_data = pd.read_csv('movielens100k/ml-100k/u.data', sep='\t', names=header) ratings_data.rating = (ratings_data.rating / 5.0) ratings_data.to_csv("./normalised_movielens.data", sep='\t', index=False, header=False) folds = 5 reader = dataset.Reader(line_format='user item rating', sep='\t', rating_scale=(0, 1)) data = Dataset.load_from_file('./normalised_movielens.data', reader) data.split(n_folds=folds) # We'll use the famous SVD algorithm. algo = SVD() rsquared_folds = np.zeros(folds) rmse_folds = np.zeros(folds) mse_folds = np.zeros(folds) fold = 0 for trainset, testset in data.folds(): start_time2 = time.time() # train and test algorithm. algo.train(trainset) predictions = algo.test(testset)
users.append(int(user)) items.append(int(item)) rates.append(int(rate)) data = {"users": users, "items": items, "rates": rates} data = pd.DataFrame(data) data.info() print(data.describe()) data.to_csv("abc.txt", index=None, header=None, columns=["users", "items", "rates"]) reader = Reader(line_format='user item rating', rating_scale=(0, 10), sep=',') data = Dataset.load_from_file("abc.txt", reader=reader) data.split(n_folds=10) # sim_options = {'name': 'cosine', # 'user_based': False # compute similarities between items # } # algo = KNNBasic(sim_options=sim_options) # We'll use the famous SVD algorithm. algo = SVD(verbose=True) for _ in range(10): perf = evaluate(algo, data, measures=['RMSE', 'MAE']) print_perf(perf) dump_obj = {'predictions': perf, 'algo': algo} pickle.dump(dump_obj, open(result_path, 'wb')) exit()
#uid为用户id,iid为项目id,true_r为真实的概率,est为分解后的估值 for uid, iid, true_r, est, _ in predictions: top_n[uid].append((iid, est)) # Then sort the predictions for each user and retrieve the k highest ones. for uid, user_ratings in top_n.items(): user_ratings.sort(key=lambda x: x[1], reverse=True) top_n[uid] = user_ratings[:n] return top_n # 加载数据集 reader = dataset.Reader(line_format='user item rating', sep='\t') data = Dataset.load_from_file('u.data', reader) trainset = data.build_full_trainset() algo = KNNWithMeans() algo.train(trainset) #推荐不在训练数据集里得Top—N个数据 # Than predict ratings for all pairs (u, i) that are NOT in the training set. testset = trainset.build_anti_testset() predictions = algo.test(testset) top_n = get_top_n(predictions, n=10) w = open('KNNWithMeans_algopredicions_10.txt', 'w+') # Print the recommended items for each user for uid, user_ratings in top_n.items(): w.write(str(uid) + ':' + str([iid for (iid, _) in user_ratings]) + '\n') w.close()
from surprise import SVD from surprise import Dataset from surprise import Reader from surprise.model_selection import cross_validate reader = Reader(line_format='user item rating timestamp', sep='::') data = Dataset.load_from_file('./ml-1m/ratings.dat', reader=reader) algo = SVD() cross_validate(algo, data, measures=['RMSE', 'MAE'], cv=5, verbose=True)
def test_KFold(): reader = Reader(line_format='user item rating', sep=' ', skip_lines=3, rating_scale=(1, 5)) custom_dataset_path = (os.path.dirname(os.path.realpath(__file__)) + '/custom_dataset') data = Dataset.load_from_file(file_path=custom_dataset_path, reader=reader) # Test n_folds parameter kf = KFold(n_splits=5) assert len(list(kf.split(data))) == 5 with pytest.raises(ValueError): kf = KFold(n_splits=10) next(kf.split(data)) # Too big (greater than number of ratings) with pytest.raises(ValueError): kf = KFold(n_splits=1) next(kf.split(data)) # Too low (must be >= 2) # Make sure data has not been shuffled. If not shuffled, the users in the # testsets are 0, 1, 2... 4 (in that order). kf = KFold(n_splits=5, shuffle=False) users = [int(testset[0][0][-1]) for (_, testset) in kf.split(data)] assert users == list(range(5)) # Make sure that when called two times without shuffling, folds are the # same. kf = KFold(n_splits=5, shuffle=False) testsets_a = [testset for (_, testset) in kf.split(data)] testsets_b = [testset for (_, testset) in kf.split(data)] assert testsets_a == testsets_b # test once again with another KFold instance kf = KFold(n_splits=5, shuffle=False) testsets_a = [testset for (_, testset) in kf.split(data)] assert testsets_a == testsets_b # We'll now shuffle b and check that folds are different. # (this is conditioned by seed setting at the beginning of file) kf = KFold(n_splits=5, random_state=None, shuffle=True) testsets_b = [testset for (_, testset) in kf.split(data)] assert testsets_a != testsets_b # test once again: two calls to kf.split make different splits when # random_state=None testsets_a = [testset for (_, testset) in kf.split(data)] assert testsets_a != testsets_b # Make sure that folds are the same when same KFold instance is used with # suffle is True but random_state is set to some value kf = KFold(n_splits=5, random_state=1, shuffle=True) testsets_a = [testset for (_, testset) in kf.split(data)] testsets_b = [testset for (_, testset) in kf.split(data)] assert testsets_a == testsets_b # Make sure raw ratings are not shuffled by KFold old_raw_ratings = copy(data.raw_ratings) kf = KFold(n_splits=5, shuffle=True) next(kf.split(data)) assert old_raw_ratings == data.raw_ratings # Make sure kf.split() and the old data.split() have the same folds. np.random.seed(3) with pytest.warns(UserWarning): data.split(2, shuffle=True) testsets_a = [testset for (_, testset) in data.folds()] kf = KFold(n_splits=2, random_state=3, shuffle=True) testsets_b = [testset for (_, testset) in kf.split(data)]
from surprise import SVD import numpy as np import surprise # run 'pip install scikit-surprise' to install surprise from surprise import BaselineOnly from surprise import Dataset from surprise import Reader from surprise.model_selection import cross_validate import time from guppy import hpy reader = Reader(line_format='user item rating timestamp', sep='\t') algo = surprise.KNNBasic() print "-----------------------------------------------------" print "Datasize = 10 Thousand Tuples" data = Dataset.load_from_file('Sample_10k.data', reader=reader) data.split(2) # split data for 2-folds cross validation start_time = time.time() surprise.evaluate(algo, data, measures=['RMSE']) print("time taken for execution: {} seconds".format(time.time()-start_time)) h = hpy() print h.heap() print "-----------------------------------------------------" print "-----------------------------------------------------" print "Datasize = 100 Thousand Tuples" data = Dataset.load_from_file('Sample_100k.data', reader=reader) data.split(2) # split data for 2-folds cross validation start_time = time.time() surprise.evaluate(algo, data, measures=['RMSE']) print("time taken for execution: {} seconds".format(time.time()-start_time)) h = hpy()
def test_gridsearchcv_refit(): """Test refit function of GridSearchCV.""" data_file = os.path.join(os.path.dirname(__file__), './u1_ml100k_test') data = Dataset.load_from_file(data_file, Reader('ml-100k')) param_grid = { 'n_epochs': [5], 'lr_all': [0.002, 0.005], 'reg_all': [0.4, 0.6], 'n_factors': [2] } # assert gs.fit() and gs.test will use best estimator for mae (first # appearing in measures) gs = GridSearchCV(SVD, param_grid, measures=['mae', 'rmse'], cv=2, refit=True) gs.fit(data) gs_preds = gs.test(data.construct_testset(data.raw_ratings)) mae_preds = gs.best_estimator['mae'].test( data.construct_testset(data.raw_ratings)) assert gs_preds == mae_preds # assert gs.fit() and gs.test will use best estimator for rmse gs = GridSearchCV(SVD, param_grid, measures=['mae', 'rmse'], cv=2, refit='rmse') gs.fit(data) gs_preds = gs.test(data.construct_testset(data.raw_ratings)) rmse_preds = gs.best_estimator['rmse'].test( data.construct_testset(data.raw_ratings)) assert gs_preds == rmse_preds # test that predict() can be called gs.predict(2, 4) # assert test() and predict() cannot be used when refit is false gs = GridSearchCV(SVD, param_grid, measures=['mae', 'rmse'], cv=2, refit=False) gs.fit(data) with pytest.raises(ValueError): gs_preds = gs.test(data.construct_testset(data.raw_ratings)) with pytest.raises(ValueError): gs.predict('1', '2') # test that error is raised if used with load_from_folds train_file = os.path.join(os.path.dirname(__file__), './u1_ml100k_train') test_file = os.path.join(os.path.dirname(__file__), './u1_ml100k_test') data = Dataset.load_from_folds([(train_file, test_file)], Reader('ml-100k')) gs = GridSearchCV(SVD, param_grid, measures=['mae', 'rmse'], cv=2, refit=True) with pytest.raises(ValueError): gs.fit(data)
def read_items(file_path): data = pd.read_csv(file_path, encoding = 'gb18030') id_to_name = {} name_to_id = {} for i in range(len(data['movieId'])): id_to_name[data['movieId'][i]] = data['title'][i] name_to_id[data['title'][i]] = data['movieId'][i] return id_to_name, name_to_id file_path = (r'C:\Users\yy\Desktop\BI\L4\L4-1\L4-code\MovieLens\movies.csv') id_to_name, name_to_id = read_items(file_path) # 数据读取 reader = Reader(line_format='user item rating timestamp', sep=',', skip_lines=1) data = Dataset.load_from_file(r'C:\Users\yy\Desktop\BI\L4\L4-1\L4-code\MovieLens\ratings.csv', reader=reader) # train_set = data.build_full_trainset() """方法1:使用SlopeOne推荐算法""" # 定义SlopeOne算法 algo = SlopeOne() # 定义K折交叉验证迭代器,K=5 kf = KFold(n_splits=5) for trainset, testset in kf.split(data): # 训练并预测 algo.fit(trainset) predictions = algo.test(testset) # 计算RMSE accuracy.rmse(predictions, verbose=True) #RMSE: 0.8653
def test_randomizedsearchcv_refit(): """Test refit method of RandomizedSearchCV class.""" data_file = os.path.join(os.path.dirname(__file__), './u1_ml100k_test') data = Dataset.load_from_file(data_file, Reader('ml-100k')) param_distributions = { 'n_epochs': [5], 'lr_all': uniform(0.002, 0.003), 'reg_all': uniform(0.4, 0.2), 'n_factors': [2] } # assert rs.fit() and rs.test will use best estimator for mae (first # appearing in measures) rs = RandomizedSearchCV(SVD, param_distributions, measures=['mae', 'rmse'], cv=2, refit=True) rs.fit(data) rs_preds = rs.test(data.construct_testset(data.raw_ratings)) mae_preds = rs.best_estimator['mae'].test( data.construct_testset(data.raw_ratings)) assert rs_preds == mae_preds # assert rs.fit() and rs.test will use best estimator for rmse rs = RandomizedSearchCV(SVD, param_distributions, measures=['mae', 'rmse'], cv=2, refit='rmse') rs.fit(data) rs_preds = rs.test(data.construct_testset(data.raw_ratings)) rmse_preds = rs.best_estimator['rmse'].test( data.construct_testset(data.raw_ratings)) assert rs_preds == rmse_preds # test that predict() can be called rs.predict(2, 4) # assert test() and predict() cannot be used when refit is false rs = RandomizedSearchCV(SVD, param_distributions, measures=['mae', 'rmse'], cv=2, refit=False) rs.fit(data) with pytest.raises(ValueError): rs.test(data.construct_testset(data.raw_ratings)) with pytest.raises(ValueError): rs.predict('1', '2') # test that error is raised if used with load_from_folds train_file = os.path.join(os.path.dirname(__file__), './u1_ml100k_train') test_file = os.path.join(os.path.dirname(__file__), './u1_ml100k_test') data = Dataset.load_from_folds([(train_file, test_file)], Reader('ml-100k')) rs = RandomizedSearchCV(SVD, param_distributions, measures=['mae', 'rmse'], cv=2, refit=True) with pytest.raises(ValueError): rs.fit(data)
train_set = train_data.build_full_trainset() p_set = p_data.build_full_trainset() assert (train_set.n_users == p_set.n_users) assert (train_set.n_items == p_set.n_items) P = np.zeros((train_set.n_users, train_set.n_items)) for u, i, p in p_set.all_ratings(): P[train_set.to_inner_uid(p_set.to_raw_uid(u)), train_set.to_inner_iid(p_set.to_raw_iid(i))] = p return P if dataset_name.startswith('ml-100k'): num = np.int(dataset_name.split('-')[-1]) reader = Reader(line_format='user item rating', sep='\t') data = Dataset.load_from_file(os.path.join('ml-100k', str(num), 'observed_X.txt'), reader=reader) elif dataset_name == 'coat': reader = Reader(line_format='user item rating', sep='\t') data = Dataset.load_from_file('coat/train_surprise_format.csv', reader=reader) reader = Reader(line_format='user item rating', sep='\t') NB_p_data = Dataset.load_from_file(os.path.join('coat/P_NB.txt'), reader=reader) NB_P = construct_inner_P(NB_p_data, data) reader = Reader(line_format='user item rating', sep='\t') LR_p_data = Dataset.load_from_file(os.path.join('coat/P_LR.txt'), reader=reader) LR_P = construct_inner_P(LR_p_data, data)
from auto_surprise.engine import Engine if __name__ == '__main__': print("Starting benchmark") # Surprise algorithms to evaluate algorithms = (SVD, SVDpp, NMF, SlopeOne, KNNBasic, KNNWithMeans, KNNWithZScore, KNNBaseline, CoClustering, BaselineOnly, NormalPredictor) # Load Movielens 100k dataset Dataset file_path = os.path.expanduser('../datasets/ml-100k/u.data') reader = Reader(line_format='user item rating timestamp', sep='\t', rating_scale=(1, 5)) data = Dataset.load_from_file(file_path, reader=reader) benchmark_results = {'Algorithm': [], 'RMSE': [], 'MAE': [], 'Time': []} # Evaluate AutoSurprise start_time = time.time() engine = Engine(debug=False) time_limt = 43200 # Run for 12 hours best_model, best_params, best_score, tasks = engine.train( data=data, target_metric='test_rmse', quick_compute=False, cpu_time_limit=time_limt, max_evals=1000000, hpo_algo=hyperopt.atpe.suggest)
def read_train_data(path): file_path = os.path.normpath(path) reader = Reader(line_format='timestamp user item rating', sep=',') data = Dataset.load_from_file(file_path, reader=reader) return data
def main(): ############################ ## Data Extraction ##### ############################ # variables for question 1~6 dataset = load_data_ratings('ratings.csv') userIds = dataset.userID movieIds = dataset.movieID ratings = dataset.ratings dataset_movie = load_data_movies('movies.csv') movie_dict = dict(zip(dataset_movie.movieID, dataset_movie.genres)) num_of_user = max(userIds) print num_of_user movie_range = [] for movieId in movieIds: if movieId not in movie_range: movie_range.append(movieId) num_of_movie = len(movie_range) print num_of_movie # variables for question 10 and rest file_path = os.path.expanduser('ratings.csv') reader = Reader(line_format='user item rating timestamp', sep=',', rating_scale=(0, 5), skip_lines=1) data = Dataset.load_from_file(file_path, reader=reader) print(data) # question 1: sparsity print ("Question 1 beigns") print("The sparsity of the " + str(1.0*len(ratings)/(num_of_user*num_of_movie))) # question 2: print ("Question 2 beigns") rating_range = [0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5] plt.hist(ratings, bins=rating_range) plt.show() #question 3: print ("Question 3 beigns") id_ratings = zip(movieIds, ratings) id_ratings.sort() id_list = [] rating_list =[ ] index = -1 for i in range(len(id_ratings)): if id_ratings[i][0] not in id_list: id_list.append(id_ratings[i][0]) rating_list.append(0) index += 1 rating_list[index] += 1 rating_id = zip(rating_list, id_list) rating_id.sort(reverse=True) rating_list, id_list = zip(*rating_id) plt.plot(rating_list) plt.show() #question 4: print ("Question 4 beigns") user=[] user_movie=[] #calculate the num for i in userIds: if i not in user: user.append(i) user_movie.append(userIds.count(i)) result=zip(user_movie,user) result.sort(reverse=True) user_movie.sort(reverse=True) #plot plt.figure() plt.plot(user_movie) plt.xlabel("User") plt.ylabel("Num of Movies") plt.show() #question 5: #question 6: print ("Question 6 beigns") movie = [] movie_rating = [] tmp = [] #calculate the variance movie.append(movieIds[0]) for i in range(len(movieIds)): if movieIds[i] not in movie: movie.append(movieIds[i]) var=np.var(tmp) tmp=[] movie_rating.append(var) tmp.append(ratings[i]) var=np.var(tmp) movie_rating.append(var) #plot plt.figure() upper=math.floor(max(movie_rating)+1) bins=np.arange(0,upper,0.5) plt.xlim(0,upper) plt.hist(movie_rating[:],bins=bins,alpha=0.5) plt.xlabel("Variance") plt.ylabel("Num of Movie") plt.show() #problem 10: knn cross-validation print ("Question 10 beigns") rmse_range = [] mae_range = [] k_range = range(2, 101, 2) min_rmse = 1000.0 min_mae = 1000.0 k_min = 0 for k in k_range: algo = KNNWithMeans(k=k, sim_options={'name': 'pearson'}) dict = cross_validate(algo, data, measures=['RMSE', 'MAE'], cv=10) rmse = sum(dict['test_rmse'])/10.0 mae = sum(dict['test_mae'])/10.0 if(rmse<min_rmse and mae<min_mae): min_rmse = rmse min_mae = mae k_min = k rmse_range.append(rmse) mae_range.append(mae) plt.plot(k_range, rmse_range) plt.plot(k_range, mae_range) plt.show() kf = KFold(n_splits=10) k_range = range(2, 101,2) #Question12 print ("Question 12 beigns") rmse=[] average_rmse=[] for k in k_range: algo = KNNWithMeans(k=k, sim_options={'name': 'pearson'}) for trainset, testset in kf.split(data): algo.fit(trainset) testset=trim_dataset12(testset,2) predictions = algo.test(testset) rmse_temp=accuracy.rmse(predictions, verbose=True) rmse.append(rmse_temp) rmse_average=sum(rmse)/10.0 average_rmse.append(rmse_average) rmse=[] plt.plot(k_range, average_rmse) plt.show() print 'minimum RMSE:' print min(average_rmse) #Question13 print ("Question 13 beigns") rmse = [] average_rmse = [] for k in k_range: algo = KNNWithMeans(k=k, sim_options={'name': 'pearson'}) for trainset, testset in kf.split(data): algo.fit(trainset) testset = trim_dataset13(testset) predictions = algo.test(testset) rmse_temp = accuracy.rmse(predictions, verbose=True) rmse.append(rmse_temp) rmse_average = sum(rmse) / 10.0 average_rmse.append(rmse_average) rmse = [] plt.plot(k_range, average_rmse) plt.show() print 'minimum RMSE:' print min(average_rmse) #Question14 print ("Question 14 beigns") rmse = [] average_rmse = [] for k in k_range: algo = KNNWithMeans(k=k, sim_options={'name': 'pearson'}) for trainset, testset in kf.split(data): algo.fit(trainset) testset = trim_dataset14(testset) predictions = algo.test(testset) rmse_temp = accuracy.rmse(predictions, verbose=True) rmse.append(rmse_temp) rmse_average = sum(rmse) / 10.0 average_rmse.append(rmse_average) rmse = [] plt.plot(k_range, average_rmse) plt.show() print 'minimum RMSE:' print min(average_rmse) # question 15: plot roc curve and calculate the auc area print ("Question 15 beigns") pre_ratings=[] act_ratings=[] auc_area=[] binary_threadhold=[2.5,3,3.5,4] kf = KFold(n_splits=10) algo = KNNWithMeans(k=20, sim_options={'name': 'pearson'}) trainset, testset = train_test_split(data,test_size=0.1) algo.fit(trainset) predictions = algo.test(testset) for i in range(len(predictions)): act_ratings.append(predictions[i][2]) pre_ratings.append(predictions[i][3]) for k in range(len(binary_threadhold)): binary_ratings=[] for i in range(len(act_ratings)): if(act_ratings[i]>=binary_threadhold[k]): binary_ratings.append(1) else: binary_ratings.append(0) fpr, tpr, threadhold = metrics.roc_curve(binary_ratings, pre_ratings, pos_label=1) auc_score=metrics.auc(fpr, tpr) auc_area.append(auc_score) print("Auc area is %f with threshold=%s" % (auc_score,str(binary_threadhold[k]))) plt.figure() plt.plot(fpr, tpr) plt.title("ROC with threshold=%s" % str(binary_threadhold[k])) plt.xlabel("False Positive Rate") plt.ylabel("True Positive Rate") # problem 17: NMF knn cross-validation print ("Question 17 beigns") NMF_rmse_range = [] NMF_mae_range = [] k_range = range(2, 51, 2) min_rmse = 1000.0 min_mae = 1000.0 k_min = 0 for k in k_range: algo = NMF(n_factors=k) dict = cross_validate(algo, data, measures=['RMSE', 'MAE'], cv=10) rmse = sum(dict['test_rmse']) / 10.0 mae = sum(dict['test_mae']) / 10.0 if (rmse < min_rmse and mae < min_mae): min_rmse = rmse min_mae = mae k_min = k NMF_rmse_range.append(rmse) NMF_mae_range.append(mae) plt.plot(k_range, NMF_rmse_range) plt.plot(k_range, NMF_mae_range) plt.show() # question 18: find out the 'minimum k' print ("Question 18 beigns") print ("minimum k should be " + str(k_min)) """ """ kf = KFold(n_splits=10) k_range = range(2, 51, 2) # Question19 print ("Question 19 beigns") rmse = [] average_rmse = [] for k in k_range: algo = NMF(n_factors=k) for trainset, testset in kf.split(data): algo.fit(trainset) testset = trim_dataset12(testset, 2) predictions = algo.test(testset) rmse_temp = accuracy.rmse(predictions, verbose=True) rmse.append(rmse_temp) rmse_average = sum(rmse) / 10.0 average_rmse.append(rmse_average) rmse = [] plt.plot(k_range, average_rmse) plt.show() print 'minimum RMSE:' print min(average_rmse) # Question20 print ("Question 20 beigns") rmse = [] average_rmse = [] for k in k_range: algo = NMF(n_factors=k) for trainset, testset in kf.split(data): algo.fit(trainset) testset = trim_dataset13(testset) predictions = algo.test(testset) rmse_temp = accuracy.rmse(predictions, verbose=True) rmse.append(rmse_temp) rmse_average = sum(rmse) / 10.0 average_rmse.append(rmse_average) rmse = [] plt.plot(k_range, average_rmse) plt.show() print 'minimum RMSE:' print min(average_rmse) # Question21 print ("Question 21 beigns") rmse = [] average_rmse = [] for k in k_range: algo = NMF(n_factors=k) for trainset, testset in kf.split(data): algo.fit(trainset) testset = trim_dataset14(testset) predictions = algo.test(testset) rmse_temp = accuracy.rmse(predictions, verbose=True) rmse.append(rmse_temp) rmse_average = sum(rmse) / 10.0 average_rmse.append(rmse_average) rmse = [] plt.plot(k_range, average_rmse) plt.show() print 'minimum RMSE:' print min(average_rmse) # question 22: plot roc curve and calculate the auc area print ("Question 22 beigns") pre_ratings=[] act_ratings=[] auc_area=[] binary_threadhold=[2.5,3,3.5,4] trainset, testset = train_test_split(data,test_size=0.1) algo = NMF(n_factors=20) algo.fit(trainset) predictions = algo.test(testset) for i in range(len(predictions)): act_ratings.append(predictions[i][2]) pre_ratings.append(predictions[i][3]) for k in range(len(binary_threadhold)): binary_ratings=[] for i in range(len(act_ratings)): if(act_ratings[i]>=binary_threadhold[k]): binary_ratings.append(1) else: binary_ratings.append(0) fpr, tpr, threadhold = metrics.roc_curve(binary_ratings, pre_ratings, pos_label=1) auc_score=metrics.auc(fpr, tpr) auc_area.append(auc_score) print("Auc area is %f with threshold=%s" % (auc_score,str(binary_threadhold[k]))) plt.figure() plt.plot(fpr, tpr) plt.title("ROC with threshold=%s" % str(binary_threadhold[k])) plt.xlabel("False Positive Rate") plt.ylabel("True Positive Rate") # question 23: Interpretability of NNMF print ("Question 23 beigns") trainset = data.build_full_trainset() algo = NMF(n_factors=20, random_state=0) algo.fit(trainset) V_matrix = algo.qi.transpose() for column in range(20): rankings = np.argsort(-V_matrix[column])[0:10] print("column " + str(column)) for ranking in rankings: raw_id = long(trainset.to_raw_iid(ranking)) print(str(raw_id) + " " + movie_dict.get(raw_id)) print("end\n") # Question24&25 print ("Question 24&25 beigns") SVD_rmse_range = [] SVD_mae_range = [] k_range = range(2, 51, 2) min_rmse = 1000.0 min_mae = 1000.0 k_min = 0 for k in k_range: algo = SVD(n_factors=k, random_state=0, n_epochs=50) dict1 = cross_validate(algo, data, measures=['RMSE', 'MAE'], cv=10) rmse = sum(dict1['test_rmse']) / 10.0 mae = sum(dict1['test_mae']) / 10.0 if (rmse < min_rmse and mae < min_mae): min_rmse = rmse min_mae = mae k_min = k SVD_rmse_range.append(rmse) SVD_mae_range.append(mae) plt.plot(k_range, SVD_rmse_range) plt.plot(k_range, SVD_mae_range) plt.show() print ("minimum k should be " + str(k_min)) kf = KFold(n_splits=10) k_range = range(2, 51, 2) # Question26 print ("Question 26 beigns") rmse = [] average_rmse = [] for k in k_range: algo = SVD(n_factors=k) for trainset, testset in kf.split(data): algo.fit(trainset) testset = trim_dataset12(testset, 2) predictions = algo.test(testset) rmse_temp = accuracy.rmse(predictions, verbose=True) rmse.append(rmse_temp) rmse_average = sum(rmse) / 10.0 average_rmse.append(rmse_average) rmse = [] plt.plot(k_range, average_rmse) plt.show() print 'minimum RMSE:' print min(average_rmse) # Question27 print ("Question 27 beigns") rmse = [] average_rmse = [] for k in k_range: algo = SVD(n_factors=k) for trainset, testset in kf.split(data): algo.fit(trainset) testset = trim_dataset13(testset) predictions = algo.test(testset) rmse_temp = accuracy.rmse(predictions, verbose=True) rmse.append(rmse_temp) rmse_average = sum(rmse) / 10.0 average_rmse.append(rmse_average) rmse = [] plt.plot(k_range, average_rmse) plt.show() print 'minimum RMSE:' print min(average_rmse) # Question28 print ("Question 28 beigns") rmse = [] average_rmse = [] for k in k_range: algo = SVD(n_factors=k) for trainset, testset in kf.split(data): algo.fit(trainset) testset = trim_dataset14(testset) predictions = algo.test(testset) rmse_temp = accuracy.rmse(predictions, verbose=True) rmse.append(rmse_temp) rmse_average = sum(rmse) / 10.0 average_rmse.append(rmse_average) rmse = [] plt.plot(k_range, average_rmse) plt.show() print 'minimum RMSE:' print min(average_rmse) # question 29: plot roc curve and calculate the auc area print ("Question 29 beigns") pre_ratings=[] act_ratings=[] auc_area=[] binary_threadhold=[2.5,3,3.5,4] trainset, testset = train_test_split(data,test_size=0.1) algo = SVD(n_factors=20) algo.fit(trainset) predictions = algo.test(testset) for i in range(len(predictions)): act_ratings.append(predictions[i][2]) pre_ratings.append(predictions[i][3]) for k in range(len(binary_threadhold)): binary_ratings=[] for i in range(len(act_ratings)): if(act_ratings[i]>=binary_threadhold[k]): binary_ratings.append(1) else: binary_ratings.append(0) fpr, tpr, threadhold = metrics.roc_curve(binary_ratings, pre_ratings, pos_label=1) auc_score=metrics.auc(fpr, tpr) auc_area.append(auc_score) print("Auc area is %f with threshold=%s" % (auc_score,str(binary_threadhold[k]))) plt.figure() plt.plot(fpr, tpr) plt.title("ROC with threshold=%s" % str(binary_threadhold[k])) plt.xlabel("False Positive Rate") plt.ylabel("True Positive Rate") # Question30 print ("Question 30 beigns") user_rating = [] userID = 1 user_average_rating = 0 total_user_error = 0 for index in range(len(userIds)): if userIds[index] != userID: user_average_rating = 1.0 * sum(user_rating) / len(user_rating) for rating in user_rating: total_user_error = total_user_error + (rating - user_average_rating) ** 2 total_user_error = total_user_error + (user_average_rating ** 2) * (9066 - len(user_rating)) userID = userID + 1 user_rating = [] user_rating.append(ratings[index]) user_average_rating = user_average_rating + ratings[index] total_user_error = math.sqrt(total_user_error / (671 * 9066)) print total_user_error # Question31 movie_list = [] for moive in movieIds: if moive not in movie_list: movie_list.append(moive) rmse_list = [] for movie_id in movie_list: rating_list = [] for i in range(len(movieIds)): if movieIds[i] == movie_id: rating_list.append(ratings[i]) if len(rating_list) > 2: mean = np.mean(rating_list) mse = 0 for rating in rating_list: mse = mse + (rating - mean) ** 2 mse = mse + (671 - len(rating_list)) * (mean) ** 2 rmse_list.append(mse) rmse = math.sqrt(sum(rmse_list) / (671 * 9066)) print ("Question 31 beigns") print(rmse) # Question32 movie_list = [] for moive in movieIds: if moive not in movie_list: movie_list.append(moive) rmse_list = [] for movie_id in movie_list: rating_list = [] for i in range(len(movieIds)): if movieIds[i] == movie_id: rating_list.append(ratings[i]) if len(rating_list) <= 2: mean = np.mean(rating_list) mse = 0 for rating in rating_list: mse = mse + (rating - mean) ** 2 mse = mse + (671 - len(rating_list)) * (mean) ** 2 rmse_list.append(mse) rmse = math.sqrt(sum(rmse_list) / (671 * 9066)) print ("Question 32 beigns") print(rmse) # Question33 movie_list = [] for moive in movieIds: if moive not in movie_list: movie_list.append(moive) rmse_list = [] for movie_id in movie_list: rating_list = [] for i in range(len(movieIds)): if movieIds[i] == movie_id: rating_list.append(ratings[i]) if len(rating_list) >= 5 and np.var(rating_list) >= 2: mean = np.mean(rating_list) mse = 0 for rating in rating_list: mse = mse + (rating - mean) ** 2 mse = mse + (671 - len(rating_list)) * (mean) ** 2 rmse_list.append(mse) rmse = math.sqrt(sum(rmse_list) / (671 * 9066)) print ("Question 33 beigns") print(rmse) # question 34: plot roc curve and calculate the auc area print ("Question 34 beigns") pre_ratings_knn=[] pre_ratings_nmf=[] pre_ratings_mf=[] act_ratings=[] auc_area=[] binary_threadhold=[2.5,3,3.5,4] trainset, testset = train_test_split(data,test_size=0.1) algo = KNNWithMeans(k=20, sim_options={'name': 'pearson'}) algo.fit(trainset) predictions = algo.test(testset) for i in range(len(predictions)): act_ratings.append(predictions[i][2]) pre_ratings_knn.append(predictions[i][3]) algo = NMF(n_factors=20, random_state=0) algo.fit(trainset) predictions = algo.test(testset) for i in range(len(predictions)): pre_ratings_nmf.append(predictions[i][3]) algo = SVD(n_factors=20) algo.fit(trainset) predictions = algo.test(testset) for i in range(len(predictions)): pre_ratings_mf.append(predictions[i][3]) binary_ratings=[] for i in range(len(act_ratings)): if(act_ratings[i]>=binary_threadhold[1]): binary_ratings.append(1) else: binary_ratings.append(0) fpr_knn, tpr_knn, threadhold_knn = metrics.roc_curve(binary_ratings, pre_ratings_knn, pos_label=1) fpr_nmf, tpr_nmf, threadhold_nmf = metrics.roc_curve(binary_ratings, pre_ratings_nmf, pos_label=1) fpr_mf, tpr_mf, threadhold_mf = metrics.roc_curve(binary_ratings, pre_ratings_mf, pos_label=1) plt.figure() plt.plot(fpr_knn, tpr_knn,'r',label='KNN') plt.plot(fpr_nmf, tpr_nmf,'b',label='NMF') plt.plot(fpr_mf, tpr_mf,'yellow',label='MF') plt.legend(loc=4) plt.title("ROC with threshold=%s" % str(binary_threadhold[1])) plt.xlabel("False Positive Rate") plt.ylabel("True Positive Rate") plt.show() # question 36-38 print ("Question 36-38 beigns") def takeFirst(elem): return float(elem[0]) precision_range_knn = [] recall_range_knn = [] precision_range_nmf = [] recall_range_nmf = [] precision_range_mf = [] recall_range_mf = [] t_range = range(1, 26) for k in range(3): for t in t_range: precision_i = 0 recall_i = 0 for i in range(10): trainset, testset = train_test_split(data, test_size=0.1, random_state=i) if (k == 0): algo = KNNWithMeans(k=2, sim_options={'name': 'pearson'}) elif (k == 1): algo = NMF(n_factors=20, random_state=0) else: algo = SVD(n_factors=20) algo.fit(trainset) predictions = algo.test(testset) predictions.sort(key=takeFirst) user_recommendation = [] users_recommendations = [] user = predictions[0][0] for prediction in predictions: if user != prediction[0]: user_recommendation.sort(key=takeFirst) users_recommendations.append(user_recommendation) user_recommendation = [] user = prediction[0] user_recommendation.append([-prediction[3], int(prediction[1])]) testset.sort(key=takeFirst) user_groundtruth = [] users_groundtruthes = [] user = testset[0][0] for test in testset: if user != test[0]: users_groundtruthes.append(user_groundtruth) user_groundtruth = [] user = test[0] if test[2] >= 3: user_groundtruth.append(int(test[1])) common = 0 precision_total = 0 recall_total = 0 for index in range(len(users_recommendations)): if len(users_groundtruthes[index]) < t: continue precision_total = precision_total + min(t, len(users_recommendations[index])) recall_total = recall_total + len(users_groundtruthes[index]) for user_recommendation in users_recommendations[index][ 0:min(t, len(users_recommendations[index]))]: if user_recommendation[1] in users_groundtruthes[index]: common = common + 1 precision_i = precision_i + 1.0 * common / precision_total recall_i = recall_i + 1.0 * common / recall_total if (k == 0): precision_range_knn.append(precision_i / 10.0) recall_range_knn.append(recall_i / 10.0) elif (k == 1): precision_range_nmf.append(precision_i / 10.0) recall_range_nmf.append(recall_i / 10.0) elif (k == 2): precision_range_mf.append(precision_i / 10.0) recall_range_mf.append(recall_i / 10.0) # plot def plotresult(t_range, precision_range, recall_range, title): plt.figure() plt.plot(t_range, precision_range, label="precision-t", color="red") plt.plot(t_range, recall_range, label="recall-t", color="blue") plt.legend(loc="best") plt.title(title) plt.figure() plt.plot(recall_range, precision_range) plt.xlabel("recall") plt.ylabel("precision") plt.title(title) plt.show() print precision_range_knn plotresult(t_range, precision_range_knn, recall_range_knn, "KNN predictions") plotresult(t_range, precision_range_nmf, recall_range_nmf, "NMF predictions") plotresult(t_range, precision_range_mf, recall_range_mf, "MF predictions") # question 39 print ("Question 39 beigns") plt.figure() plt.plot(recall_range_knn, precision_range_knn, label="knn", color="red") plt.plot(recall_range_nmf, precision_range_nmf, label="nmf", color="blue") plt.plot(recall_range_mf, precision_range_mf, label="mf", color="green") plt.legend(loc="best") plt.xlabel("recall") plt.ylabel("precision") plt.show()
with open('../data-new/train.txt', 'r') as f: train_file = f.readlines() f.close() for train in train_file: line = train.strip() if line.find('|') != -1: user_id, user_item_count = line.split('|') else: if line == "": continue item_id, rate_str = line.split() write_file.write('%s,%s,%s\n' % (user_id, item_id, rate_str)) write_file.close() print("reading......") reader = Reader(line_format='user item rating', sep=',', rating_scale=(0, 100)) data = Dataset.load_from_file("train.csv", reader=reader) algo = SVD(n_factors=10, n_epochs=10, lr_all=0.015, reg_all=0.01) ''' bsl_options = {'method': 'als', 'n_epochs': 5, 'reg_u': 12, 'reg_i': 5 } ''' #algo = BaselineOnly(bsl_options=bsl_options) ''' kf = KFold(n_splits=3) print('------begin train user cf model------------') for trainset, testset in kf.split(train_cf): # 训练并测试算法