Skip to content

Instantly share code, notes, and snippets.

@AmolMavuduru
Created January 7, 2021 22:43
Show Gist options
  • Save AmolMavuduru/67a1e67171a88055832716f8fcc37e67 to your computer and use it in GitHub Desktop.
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".
"""
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