help="comma-separated item IDs for loved items") parser.add_argument('--like', type=csv, help="comma-separated item IDs for liked items") parser.add_argument('--neutral', type=csv, help="comma-separated item IDs for neutral items") parser.add_argument('--dislike', type=csv, help="comma-separated item IDs for disliked items") parser.add_argument('--hate', type=csv, help="comma-separated item IDs for hated items") args = parser.parse_args() model = LatentFactorsModel(args.model_path) tastes = {} if args.love is not None: for item in args.love: tastes[item] = 1.0 if args.like is not None: for item in args.like: tastes[item] = 0.75 if args.neutral is not None: for item in args.neutral: tastes[item] = 0.5 if args.dislike is not None: for item in args.dislike: tastes[item] = 0.25 if args.hate is not None:
#!/usr/bin/python # -*- coding: utf8 -*- # # recommend.py # José Devezas ([email protected]) # 2013-06-28 import os import sys import argparse from jld import LatentFactorsModel if __name__ == "__main__": parser = argparse.ArgumentParser( description='Recommend new items to an existing user ordered by predicted rating.') parser.add_argument('user', help='user identification string, as defined in the training set') parser.add_argument('model_path', help='HDF5 file with the trained model containing the factorized matrices') args = parser.parse_args() model = LatentFactorsModel(args.model_path) rank = 1 for item_id, rating in model.recommend(args.user, limit=20): print "%d\t%.10f\t%s" % (rank, rating, item_id) rank += 1
from jld import LatentFactorsModel if __name__ == "__main__": parser = argparse.ArgumentParser( description='Predict a normalized rating for any unrated item of an existing user.') parser.add_argument('user', help='user identification string, as defined in the training set') parser.add_argument('item', help='item identification string, as defined in the training set') parser.add_argument('model_path', help='HDF5 file with the trained model containing the factorized matrices') parser.add_argument('-f', '--force-predict', action='store_true', help='returns the predicted value even when the original rating is available') args = parser.parse_args() model = LatentFactorsModel(args.model_path) if not args.force_predict: # If rating exists, return real rating instead of prediction. rating = model.get_rating(args.user, args.item) if rating is not None: print("User %s rated item %s with %f." % (args.user, args.item, rating)) sys.exit(0) predicted_rating = model.predict(args.user, args.item) if predicted_rating is None: print("Could not predict rating for user %s and item %s." % (args.user, args.item)) sys.exit(1) print("Predicted rating for user %s and item %s is of %f." % (args.user, args.item, predicted_rating))
parser.add_argument('--precompute', action='store_true', help="precompute and store prediction values in HDF5 (DEFAULT=False)") args = parser.parse_args() if not os.access(args.ratings_path, os.R_OK): print("'%s' is not readable." % args.ratings_path) sys.exit(0) if os.path.exists(args.model_path): ans = None while ans != 'y' and ans != 'n': ans = raw_input("===> HDF5 model already exists at %s. Overwrite? [yn] " % args.model_path) if ans != 'y': sys.exit(0) os.remove(args.model_path) model = LatentFactorsModel(args.model_path) if args.delimiter is not None: model.set_training_csv_delimiter(args.delimiter) if args.rank is not None: model.set_training_rank(args.rank) if args.size is not None: model.set_training_sample_size(args.size) model.train(args.ratings_path) if args.precompute: model.precompute_predictions()
"HDF5 file with the trained model containing the factorized matrices") parser.add_argument( 'preferences_csv_path', help= "HDF5 file with the trained model containing the factorized matrices") parser.add_argument( '-c', '--group-combine', choices=['mean', 'usage-wmean'], default='mean', help= 'selects the method to combine the user profiles into a single group profile (DEFAULT: mean)' ) args = parser.parse_args() model = LatentFactorsModel(args.model_path) tastes = {} with open(args.preferences_csv_path, 'rb') as f: for user_id, item_id, normalized_rating in csv.reader(f): if not user_id in tastes: tastes[user_id] = {} tastes[user_id][item_id] = float(normalized_rating) group_combine = jld.group_mean if args.group_combine == 'usage-wmean': group_combine = jld.group_weighted_mean_by_user_activity rank = 1 for item_id, rating in model.recommend_to_group_by_query( tastes, group_combine=group_combine)[0:20]:
) parser.add_argument( "-m", "--max-features", type=int, help="""the maximum number of features to use in cross-validation (ignored if sampling size is not defined)""", ) parser.add_argument("-o", "--output", type=str, help="output CSV filename, to store validation scores (MAE)") args = parser.parse_args() if not os.access(args.ratings_path, os.R_OK): print("'%s' is not readable." % args.ratings_path) sys.exit(0) model = LatentFactorsModel() if args.delimiter is not None: model.set_training_csv_delimiter(args.delimiter) if args.rank is not None: model.set_training_rank(args.rank) k = 10 if args.folds is not None: k = args.folds feature_sampling = None if args.feature_sampling_interval is not None: feature_sampling_interval = args.feature_sampling_interval
'item', help='item identification string, as defined in the training set') parser.add_argument( 'model_path', help= 'HDF5 file with the trained model containing the factorized matrices') parser.add_argument( '-f', '--force-predict', action='store_true', help= 'returns the predicted value even when the original rating is available' ) args = parser.parse_args() model = LatentFactorsModel(args.model_path) if not args.force_predict: # If rating exists, return real rating instead of prediction. rating = model.get_rating(args.user, args.item) if rating is not None: print("User %s rated item %s with %f." % (args.user, args.item, rating)) sys.exit(0) predicted_rating = model.predict(args.user, args.item) if predicted_rating is None: print("Could not predict rating for user %s and item %s." % (args.user, args.item)) sys.exit(1)
args = parser.parse_args() if not os.access(args.ratings_path, os.R_OK): print("'%s' is not readable." % args.ratings_path) sys.exit(0) if os.path.exists(args.model_path): ans = None while ans != 'y' and ans != 'n': ans = raw_input( "===> HDF5 model already exists at %s. Overwrite? [yn] " % args.model_path) if ans != 'y': sys.exit(0) os.remove(args.model_path) model = LatentFactorsModel(args.model_path) if args.delimiter is not None: model.set_training_csv_delimiter(args.delimiter) if args.rank is not None: model.set_training_rank(args.rank) if args.size is not None: model.set_training_sample_size(args.size) model.train(args.ratings_path) if args.precompute: model.precompute_predictions()
import argparse import jld from jld import LatentFactorsModel if __name__ == "__main__": parser = argparse.ArgumentParser( description="Use group preferences to recommend new items.") parser.add_argument('model_path', help="HDF5 file with the trained model containing the factorized matrices") parser.add_argument('preferences_csv_path', help="HDF5 file with the trained model containing the factorized matrices") parser.add_argument('-c', '--group-combine', choices=['mean', 'usage-wmean'], default='mean', help='selects the method to combine the user profiles into a single group profile (DEFAULT: mean)') args = parser.parse_args() model = LatentFactorsModel(args.model_path) tastes = {} with open(args.preferences_csv_path, 'rb') as f: for user_id, item_id, normalized_rating in csv.reader(f): if not user_id in tastes: tastes[user_id] = {} tastes[user_id][item_id] = float(normalized_rating) group_combine = jld.group_mean if args.group_combine == 'usage-wmean': group_combine = jld.group_weighted_mean_by_user_activity rank = 1 for item_id, rating in model.recommend_to_group_by_query(tastes, group_combine=group_combine)[0:20]: print "%d\t%.10f\t%s" % (rank, rating, item_id)
# -*- coding: utf8 -*- # # recommend.py # José Devezas ([email protected]) # 2013-06-28 import os import sys import argparse from jld import LatentFactorsModel if __name__ == "__main__": parser = argparse.ArgumentParser( description= 'Recommend new items to an existing user ordered by predicted rating.') parser.add_argument( 'user', help='user identification string, as defined in the training set') parser.add_argument( 'model_path', help= 'HDF5 file with the trained model containing the factorized matrices') args = parser.parse_args() model = LatentFactorsModel(args.model_path) rank = 1 for item_id, rating in model.recommend(args.user, limit=20): print "%d\t%.10f\t%s" % (rank, rating, item_id) rank += 1
description="Discover nearest neighbor in trained model based on qualitative user input.") parser.add_argument('model_path', help="HDF5 file with the trained model containing the factorized matrices") parser.add_argument('--love', type=csv, help="comma-separated item IDs for loved items") parser.add_argument('--like', type=csv, help="comma-separated item IDs for liked items") parser.add_argument('--neutral', type=csv, help="comma-separated item IDs for neutral items") parser.add_argument('--dislike', type=csv, help="comma-separated item IDs for disliked items") parser.add_argument('--hate', type=csv, help="comma-separated item IDs for hated items") args = parser.parse_args() model = LatentFactorsModel(args.model_path) tastes = {} if args.love is not None: for item in args.love: tastes[item] = 1.0 if args.like is not None: for item in args.like: tastes[item] = 0.75 if args.neutral is not None: for item in args.neutral: tastes[item] = 0.5 if args.dislike is not None: for item in args.dislike: tastes[item] = 0.25 if args.hate is not None:
description="Use qualitative users preferences to recommend new items.") parser.add_argument('model_path', help="HDF5 file with the trained model containing the factorized matrices") parser.add_argument('--love', type=csv, help="comma-separated item IDs for loved items") parser.add_argument('--like', type=csv, help="comma-separated item IDs for liked items") parser.add_argument('--neutral', type=csv, help="comma-separated item IDs for neutral items") parser.add_argument('--dislike', type=csv, help="comma-separated item IDs for disliked items") parser.add_argument('--hate', type=csv, help="comma-separated item IDs for hated items") args = parser.parse_args() model = LatentFactorsModel(args.model_path) tastes = {} if args.love is not None: for item in args.love: tastes[item] = 1.0 if args.like is not None: for item in args.like: tastes[item] = 0.75 if args.neutral is not None: for item in args.neutral: tastes[item] = 0.5 if args.dislike is not None: for item in args.dislike: tastes[item] = 0.25 if args.hate is not None:
'--max-features', type=int, help="""the maximum number of features to use in cross-validation (ignored if sampling size is not defined)""") parser.add_argument( '-o', '--output', type=str, help="output CSV filename, to store validation scores (MAE)") args = parser.parse_args() if not os.access(args.ratings_path, os.R_OK): print("'%s' is not readable." % args.ratings_path) sys.exit(0) model = LatentFactorsModel() if args.delimiter is not None: model.set_training_csv_delimiter(args.delimiter) if args.rank is not None: model.set_training_rank(args.rank) k = 10 if args.folds is not None: k = args.folds feature_sampling = None if args.feature_sampling_interval is not None: feature_sampling_interval = args.feature_sampling_interval