Now that we built all of the different parts of a recommendation engine, we are ready to generate movie recommendations. We will use all the functionality that we built in the previous recipes to build a movie recommendation engine. Let's see how to build it.
import json import numpy as np from euclidean_score import euclidean_score from pearson_score import pearson_score from find_similar_users import find_similar_users
# Generate recommendations for a given user def generate_recommendations(dataset, user): if user not in dataset: raise TypeError('User ' + user + ' not present in the dataset')
total_scores = {} similarity_sums = {} for u in [x for x in dataset if x != user]: similarity_score = pearson_score(dataset, user, u) if similarity_score <= 0: continue
for item in [x for x in dataset[u] if x not in dataset[user] or dataset[user][x] == 0]: total_scores.update({item: dataset[u][item] * similarity_score}) similarity_sums.update({item: similarity_score})
if len(total_scores) == 0: return ['No recommendations possible']
# Create the normalized list movie_ranks = np.array([[total/similarity_sums[item], item] for item, total in total_scores.items()])
# Sort in decreasing order based on the first column movie_ranks = movie_ranks[np.argsort(movie_ranks[:, 0])[::-1]]
# Extract the recommended movies recommendations = [movie for _, movie in movie_ranks] return recommendations
main
function and load the dataset:if __name__=='__main__': data_file = 'movie_ratings.json' with open(data_file, 'r') as f: data = json.loads(f.read())
Michael Henry
:user = 'Michael Henry' print " Recommendations for " + user + ":" movies = generate_recommendations(data, user) for i, movie in enumerate(movies): print str(i+1) + '. ' + movie
John Carson
has watched all the movies. Therefore, if we try to generate recommendations for him, it should display 0 recommendations. Let's see whether this happens:user = 'John Carson' print " Recommendations for " + user + ":" movies = generate_recommendations(data, user) for i, movie in enumerate(movies): print str(i+1) + '. ' + movie
3.133.141.6