Created
January 7, 2021 22:43
-
-
Save AmolMavuduru/67a1e67171a88055832716f8fcc37e67 to your computer and use it in GitHub Desktop.
Sample code for my Medium article "How to build powerful deep recommender systems using Spotlight".
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Utility functions for generating movie recommendations using matrix factorization models | |
""" | |
def get_metadata(movie_id, metadata): | |
""" | |
Retrieves the metadata for a movie given the movie ID | |
""" | |
movie_data = metadata[metadata['movieId'] == movie_id] | |
return movie_data[['original_title', 'release_date', 'genres']].to_dict(orient='records') | |
def recommend_movies(user_id, metadata, model, n_movies=5): | |
""" | |
Recommends movies for user using a matrix factorization model. | |
""" | |
pred = model.predict(user_ids=user_id) | |
indices = np.argpartition(pred, -n_movies)[-n_movies:] | |
best_movie_ids = indices[np.argsort(pred[indices])] | |
return [get_metadata(movie_id + 1, metadata) for movie_id in best_movie_ids] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment