Created
October 1, 2018 00:09
-
-
Save emmagrimaldi/4e33c0091d2294b04c063b552925fe5f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # creating a Series for the movie titles so they are associated to an ordered numerical | |
| # list I will use in the function to match the indexes | |
| indices = pd.Series(df.index) | |
| # defining the function that takes in movie title | |
| # as input and returns the top 10 recommended movies | |
| def recommendations(title, cosine_sim = cosine_sim): | |
| # initializing the empty list of recommended movies | |
| recommended_movies = [] | |
| # gettin the index of the movie that matches the title | |
| idx = indices[indices == title].index[0] | |
| # creating a Series with the similarity scores in descending order | |
| score_series = pd.Series(cosine_sim[idx]).sort_values(ascending = False) | |
| # getting the indexes of the 10 most similar movies | |
| top_10_indexes = list(score_series.iloc[1:11].index) | |
| # populating the list with the titles of the best 10 matching movies | |
| for i in top_10_indexes: | |
| recommended_movies.append(list(df.index)[i]) | |
| return recommended_movies |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment