def zscoreKNN(train, test, user_bool): """ Run the KNN zscore model from Surprise library. @param train: the training set in the Surprise format. @param test: the test set in the Surprise format. @param user_bool: if True, runs the user based KNN zscore. Otherwise, runs item based KNN zscore. @return: the predictions in a numpy array. """ algo = spr.KNNWithZScore(user_based=user_bool) algo.fit(train) predictions = algo.test(test) return get_predictions(predictions)
def main(args): user_item_based = 'item_based' if args.item_based else 'user_based' filename = '_'.join([ args.exp_name, args.algorithm, args.sim_name, user_item_based, str(args.num_rows) ]) + '.pkl' output_file = Path(filename) if output_file.exists(): print(f'ERROR! Output file {output_file} already exists. Exiting!') sys.exit(1) print(f'Saving scores in {output_file}\n') reader = surprise.Reader(rating_scale=(1, 5)) df = pq.read_table('all_ratings_with_indices.parquet', columns=['user_idx', 'movie_idx', 'rating']).to_pandas() df.user_idx = df.user_idx.astype(np.uint32) df.movie_idx = df.movie_idx.astype(np.uint16) df.rating = df.rating.astype(np.uint8) print(df.dtypes) data = surprise.Dataset.load_from_df(df[:args.num_rows], reader=reader) del df sim_options = { 'name': args.sim_name, 'user_based': False if args.item_based else True } if args.algorithm == 'knn': algo = surprise.KNNBasic(sim_options=sim_options) elif args.algorithm == 'baseline': algo = surprise.BaselineOnly() elif args.algorithm == 'normal': algo = surprise.NormalPredictor() elif args.algorithm == 'knn_zscore': algo = surprise.KNNWithZScore(sim_options=sim_options) elif args.algorithm == 'svd': algo = surprise.SVD() elif args.algorithm == 'nmf': algo = surprise.NMF() else: print(f'Algorithm {args.algorithm} is not a valid choice.') scores = surprise.model_selection.cross_validate(algo, data, cv=args.cv_folds, verbose=True, n_jobs=-1) pickle.dump(scores, open(output_file, 'wb'))
def create_algorithm(): """ See: http://surprise.readthedocs.io/en/stable/prediction_algorithms.html Just change the algorithm and the option set for a different prediction algorithm. """ options = { 'name': 'msd', 'user_based': False } algo = surprise.KNNWithZScore( min_k=1, k=40, sim_options=options) return algo
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
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']]
} mean_ap = [] precision = [] recall = [] fscore = [] normalized_DCG = [] mean_ap_train = [] precision_train = [] recall_train = [] fscore_train = [] normalized_DCG_train = [] for k_val in ks: print(k_val) algo = surprise.KNNWithZScore(k=k_val, sim_options=sim_options) pr = 0 re = 0 fs = 0 ap = 0 nd = 0 pr_train = 0 re_train = 0 fs_train = 0 ap_train = 0 nd_train = 0 for trainset, testset in data.folds(): algo.train(trainset) predictions_on_test = algo.test(testset) precisions_test, recalls_test = precision_recall_at_k(
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
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] # Take a look at cross validation results to compare model types print('\n\nModeling: {}\n'.format(str(alg_name))) result = cross_validate(algorithm,
print("Done.") # 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
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
train_reader = Reader(line_format='user item rating timestamp', sep=',', rating_scale=(0, 5)) trainset = Dataset.load_from_file(train_path, reader=train_reader) trainset = trainset.build_full_trainset() if args.model == 'NormalPredictor': model = surprise.NormalPredictor() elif args.model == 'BaselineOnly': model = surprise.BaselineOnly() 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)