def algo_tester(data_object): ''' Produces a dataframe displaying all the different RMSE's, test & train times of the different surprise algorithms ---Parameters--- data_object(variable) created from the read_data_surprise function ---Returns--- returns a dataframe where you can compare the performance of different algorithms ''' benchmark = [] algos = [ sp.SVDpp(), sp.SVD(), sp.SlopeOne(), sp.NMF(), sp.NormalPredictor(), sp.KNNBaseline(), sp.KNNBasic(), sp.KNNWithMeans(), sp.KNNWithZScore(), sp.BaselineOnly(), sp.CoClustering() ] # Iterate over all algorithms for algorithm in algos: # Perform cross validation results = cross_validate(algorithm, data_object, measures=['RMSE'], cv=3, verbose=False) # Get results & append algorithm name tmp = pd.DataFrame.from_dict(results).mean(axis=0) tmp = tmp.append( pd.Series([str(algorithm).split(' ')[0].split('.')[-1]], index=['Algorithm'])) benchmark.append(tmp) benchmark = pd.DataFrame(benchmark).set_index('Algorithm').sort_values( 'test_rmse') return benchmark
def model_fit(self): ''' Train model using surprise.SVD algorithm. ''' self.build_trainset() algo = self._algo_choise if algo == 'SVD': self.algorithm = surprise.SVD() elif algo == 'Baseline': self.algorithm = surprise.BaselineOnly() elif algo == 'SlopeOne': self.algorithm = surprise.SlopeOne() elif algo == 'CoClustering': self.algorithm = surprise.CoClustering() else: self.algorithm = surprise.KNNBasic() print('Training Recommender System using %s...' % algo) self.algorithm.fit(self.trainset) self.ratings_changed = False print('Done')
epochs=2, validation_split=0.1, shuffle=True) y_pred = model.predict([df_hybrid_test['User'], df_hybrid_test['Movie'], test_tfidf]) y_true = df_hybrid_test['Rating'].values rmse = np.sqrt(mean_squared_error(y_pred=y_pred, y_true=y_true)) print('\n\nTesting Result With Keras Hybrid Deep Learning: {:.4f} RMSE'.format(rmse)) # Load dataset into surprise specific data-structure data = sp.Dataset.load_from_df(df_filterd[['User', 'Movie', 'Rating']].sample(20000), sp.Reader()) benchmark = [] # Iterate over all algorithms for algorithm in [sp.SVD(), sp.SVDpp(), sp.SlopeOne(), sp.NMF(), sp.NormalPredictor(), sp.KNNBaseline(), sp.KNNBasic(), sp.KNNWithMeans(), sp.KNNWithZScore(), sp.BaselineOnly(), sp.CoClustering()]: # Perform cross validation results = cross_validate(algorithm, data, measures=['RMSE', 'MAE'], cv=3, verbose=False) # Get results & append algorithm name tmp = pd.DataFrame.from_dict(results).mean(axis=0) tmp = tmp.append(pd.Series([str(algorithm).split(' ')[0].split('.')[-1]], index=['Algorithm'])) # Store data benchmark.append(tmp) # Store results surprise_results = pd.DataFrame(benchmark).set_index('Algorithm').sort_values('test_rmse', ascending=False) # Get data data = surprise_results[['test_rmse', 'test_mae']]
def main(train_df, target_df, cache_name="test", force_recompute=[]): """Train multiple models on train_df and predicts target_df Predictions are cached. If the indices don't match the indices of target_df, the cache is discarded. By default, if a method was already computed it is not recomputed again (except if the method name is listed in force_recompute). cache_name is the name to use to read and write the cache. Arguments: train_df {dataframe} -- Training dataframe target_df {dataframe} -- Testing dataframe Keyword Arguments: cache_name {str} -- Name to use for caching (default: {"test"}) force_recompute {list} -- Name(s) of methods to recompute, whether or not it was already computed. Useful to only recompute single methods without discarding the rest. (default: {[]}) Returns: Dataframe -- Dataframe with predictions for each methods as columns, IDs as indices """ global algo_in_use CACHED_DF_FILENAME = os.path.dirname( os.path.abspath(__file__)) +\ "/cache/cached_predictions_{}.pkl".format(cache_name) train_df = preprocess_df(train_df) trainset = pandas_to_data(train_df) ids_to_predict = target_df["Id"].to_list() # try to retrieve backup dataframe try: print("Retrieving cached predictions") all_algos_preds_df = pd.read_pickle(CACHED_DF_FILENAME) print("Ensuring cached IDs match given IDs") assert sorted(ids_to_predict) == sorted( all_algos_preds_df.index.values) print("Indices match, continuing") except (FileNotFoundError, AssertionError): print("No valid cached predictions found") all_algos_preds_df = pd.DataFrame(ids_to_predict, columns=["Id"]) all_algos_preds_df.set_index("Id", inplace=True) all_algos = { "SVD": spr.SVD(n_factors=200, n_epochs=100), "Baseline": spr.BaselineOnly(), "NMF": spr.NMF(n_factors=30, n_epochs=100), "Slope One": spr.SlopeOne(), "KNN Basic": spr.KNNBasic(k=60), "KNN Means": spr.KNNWithMeans(k=60), "KNN Baseline": spr.KNNBaseline(), "KNN Zscore": spr.KNNWithZScore(k=60), "SVD ++": spr.SVDpp(n_factors=40, n_epochs=100), "Co Clustering": spr.CoClustering() } for name in all_algos: print("##### {} ####".format(name)) if name in force_recompute and name in all_algos_preds_df.columns: all_algos_preds_df.drop(name, axis=1, inplace=True) if name in all_algos_preds_df.columns: print("Already computed {}, skipping".format(name)) continue algo = all_algos[name] time.sleep(1) algo.fit(trainset) time.sleep(1) algo_in_use = algo print("Generating predictions...") predictions = parallelize_predictions(ids_to_predict, 80) print("Done. Merging with previous results") this_algo_preds_df = pd.DataFrame(predictions, columns=["Id", name]) this_algo_preds_df.set_index("Id", inplace=True) all_algos_preds_df = pd.merge(all_algos_preds_df, this_algo_preds_df, left_index=True, right_index=True) all_algos_preds_df.to_pickle(CACHED_DF_FILENAME) print("DONE computing surprize") return all_algos_preds_df
def build_network(self): self.model = surprise.CoClustering()
dfs_path = os.path.join(root_dir, 'Data/datasets/') ratings_df = pd.read_csv(os.path.join(dfs_path, 'ratings_expl.csv'), sep=';', encoding='latin-1', low_memory=False) # 加载数据 reader = surprise.Reader(rating_scale=(1, 10)) data = surprise.Dataset.load_from_df(ratings_df[['user_id', 'isbn', 'rating']], reader) kf = KFold(n_splits=5) # data = Dataset.load_builtin('jester') # Kfold algo1 = SVD() algo2 = surprise.BaselineOnly() algo3 = surprise.KNNBasic() algo4 = surprise.CoClustering() for trainset, testset in kf.split(data): # SVD algo1.fit(trainset) pSVD = algo1.test(testset) # 计算并打印RMSE print("SVD-") accuracy.rmse(pSVD, verbose=True) #Baseline algo2.fit(trainset) pBase = algo2.test(testset) print("BaseLine-") accuracy.rmse(pBase) #Baseline algo3.fit(trainset) pknn = algo3.test(testset)
# identifier_df_test = X_test[['user_id', 'business_id']] # A reader is needed but only the rating_scale param is requiered. reader = Reader(rating_scale=(1, 5)) data = Dataset.load_from_df(train_df, reader) sim_options = {'user_based': [False]} results = [] # Iterate over all algorithms for algorithm in [ SVD(), surprise.NMF(), surprise.SlopeOne(), surprise.CoClustering(), surprise.KNNBasic(sim_options=sim_options), surprise.KNNWithMeans(sim_options=sim_options), surprise.KNNWithZScore(sim_options=sim_options), surprise.KNNBaseline(sim_options=sim_options), surprise.NormalPredictor(), surprise.BaselineOnly() ]: # Get string of algname for naming a pickle file a useful name alg_name = str(algorithm) alg_name = alg_name[alg_name.find('.') + 1:] alg_name = alg_name[alg_name.find('.') + 1:] alg_name = alg_name[alg_name.find('.') + 1:] alg_name = alg_name[:alg_name.find('object') - 1]
# defining the number of folds = 5 print("Performing splits...") kf = sp.model_selection.KFold(n_splits=5, random_state=0) print("Done.") ### ### PART 1.1 ### ''' application of all algorithms for recommendation made available by “Surprise” libraries, according to their default configuration. ''' algorithms = [sp.NormalPredictor(), sp.BaselineOnly(), sp.KNNBasic(),\ sp.KNNWithMeans(), sp.KNNWithZScore(), sp.KNNBaseline(),\ sp.SVD(), sp.SVDpp(), sp.NMF(), sp.SlopeOne(), sp.CoClustering()] for elem in algorithms: start_time = time.time() algo = elem sp.model_selection.cross_validate(algo, data, measures=['RMSE'], \ cv=kf, n_jobs = 2, verbose=True) print("--- %s seconds ---" % (time.time() - start_time)) print() ### ### PART 1.2 ### ''' Improvement of the quality of both KNNBaseline and SVD methods, by performing hyper-parameters tuning over 5-folds Random-Search-Cross-Validation - KNN
def SurpriseBased(table, relation_name, parameters, verbose=False): """ """ report = {} # Initial checks param_keys = [k for k, v in parameters.items()] if ('max_scale' not in param_keys) or ('min_scale' not in param_keys): raise ValueError( 'max_scale and min_scale must be specified in parameters for explicit RS.' ) if 'model_size' not in param_keys: raise ValueError( 'model_size must be specified in parameters for SURPRISE-based RS.' ) if 'topK_predictions' not in param_keys: raise ValueError( 'A size (K) must be given for the recommended list size (topK).') # Retrieving names start_group = table.start_group.iloc[0] end_group = table.end_group.iloc[0] timestamp = pd.Timestamp('') # Retrieving the table of the bipartite graph in SURPRISE format table = table[['start_object', 'end_object', 'value']] reader = surprise.Reader(rating_scale=(parameters['min_scale'], parameters['max_scale'])) data = surprise.Dataset.load_from_df(table, reader) # Selecting the method from the SURPRISE module if parameters['method'] == 'UBCF': method = surprise.KNNBasic(k=parameters['model_size'], verbose=verbose) elif parameters['method'] == 'Z-UBCF': method = surprise.KNNWithZScore(k=parameters['model_size']) elif parameters['method'] == 'IBCF': method = surprise.KNNBasic(k=parameters['model_size'], sim_options={'user_based': False}) elif parameters['method'] == 'SVD': method = surprise.SVD(n_factors=parameters['model_size']) elif parameters['method'] == 'NMF': method = surprise.NMF(n_factors=parameters['model_size']) elif parameters['method'] == 'CClustering': method = surprise.CoClustering(n_cltr_u=parameters['model_size'], n_cltr_i=parameters['model_size']) else: raise ValueError('Unrecognized SURPRISE-based RS method named %s' % parameters['method']) # Computing utility metrics if so specified if 'RMSE' in param_keys: if parameters['RMSE']: results = surprise.model_selection.validation.cross_validate( method, data, measures=['rmse'], cv=5, verbose=verbose) rmse = results['test_rmse'].mean() report['RMSE'] = rmse # Training the prediction method trainset = data.build_full_trainset() del data method.fit(trainset) # Retrieving unobserved pairs t = TCounter() VerboseMessage(verbose, 'Producing unobserved links...') unobserved_links = trainset.build_anti_testset() VerboseMessage( verbose, 'Unobserved links produced in %s.' % (ETSec2ETTime(TCounter() - t))) # Making the predictions t = TCounter() VerboseMessage(verbose, 'Making predictions for unobserved links...') predictions = method.test(unobserved_links) VerboseMessage( verbose, 'Predictions for Unobserved links produced in %s.' % (ETSec2ETTime(TCounter() - t))) # Prefiltering predictions with lower scores if 'prefilter_score' in param_keys: t = TCounter() VerboseMessage( verbose, 'Prefiltering %d predictions scores lower than %0.1f...' % (len(predictions), parameters['prefilter_threshold'])) predictions = [ p for p in predictions if p[3] > parameters['prefilter_threshold'] ] VerboseMessage( verbose, 'Predictions prefiltered in %s, %d remaining.' % (ETSec2ETTime(TCounter() - t), len(predictions))) # Selecting only top K predictions t = TCounter() VerboseMessage( verbose, 'Selecting top %d predictions...' % (parameters['topK_predictions'])) top_recs = defaultdict(list) for uid, iid, true_r, est, _ in predictions: top_recs[uid].append((iid, est)) for uid, user_ratings in top_recs.items(): user_ratings.sort(key=lambda x: x[1], reverse=True) top_recs[uid] = user_ratings[:parameters['topK_predictions']] VerboseMessage( verbose, 'Predictions selected in %s.' % (ETSec2ETTime(TCounter() - t))) # Putting the predictions in a DataFrame predictions_table = pd.DataFrame(columns=[ 'relation', 'start_group', 'start_object', 'end_group', 'end_object', 'value', 'timestamp' ]) counter = 0 t = TCounter() VerboseMessage(verbose, 'Arranging predictions into a DataFrame table...') for k, v in top_recs.items(): for r in v: predictions_table.loc[counter] = [ relation_name, start_group, k, end_group, r[0], r[1], timestamp ] counter += 1 VerboseMessage( verbose, 'Predictions arranged into a table in %s.' % (ETSec2ETTime(TCounter() - t))) return predictions_table, report
elif args.model == 'KNNBasic': model = surprise.KNNBasic() elif args.model == 'KNNWithMeans': model = surprise.KNNWithMeans() elif args.model == 'KNNWithZScore': model = surprise.KNNWithZScore() elif args.model == 'KNNBaseline': model = surprise.KNNBaseline() elif args.model == 'SVD': model = surprise.SVD() elif args.model == 'SVDpp': model = surprise.SVDpp(verbose=True) elif args.model == 'NMF': model = surprise.NMF() elif args.model == 'SlopeOne': model = surprise.SlopeOne() elif args.model == 'CoClustering': model = surprise.CoClustering() # cross_validate(model, trainset, cv=5, verbose=True) model.fit(trainset) lines = [] test_path = path + '/Data/test_format.txt' for line in tqdm(open(test_path, 'r').readlines()): user_id, item_id, timestamp, *tags = line.strip().split(',') rating = model.predict(user_id, item_id).est lines.append("{:.5}\n".format(float(rating))) open(path + '/Data/submit.txt', 'w').writelines(lines)