Skip to content

Instantly share code, notes, and snippets.

@AmolMavuduru
Created January 7, 2021 22:56
Show Gist options
  • Save AmolMavuduru/966b985252bde7cfcda9cf903909b45d to your computer and use it in GitHub Desktop.
Save AmolMavuduru/966b985252bde7cfcda9cf903909b45d 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 sequence models.
"""
import difflib
def get_movie_id(movie_title, metadata):
"""
Gets the movie id for a movie title
"""
existing_titles = list(metadata['original_title'].values)
closest_titles = difflib.get_close_matches(movie_title, existing_titles)
movie_id = metadata[metadata['original_title'] == closest_titles[0]]['movieId'].values[0]
return movie_id
def recommend_next_movies(movies, metadata, model, n_movies=5):
"""
Recommends the top n next movies that a user is likely to watch
based on a list of previously watched movies.
"""
movie_ids = [get_movie_id(movie, metadata) for movie in movies]
pred = model.predict(sequences=np.array(movie_ids))
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