Skip to content

Instantly share code, notes, and snippets.

@emmagrimaldi
Created October 1, 2018 00:09
Show Gist options
  • Select an option

  • Save emmagrimaldi/4e33c0091d2294b04c063b552925fe5f to your computer and use it in GitHub Desktop.

Select an option

Save emmagrimaldi/4e33c0091d2294b04c063b552925fe5f to your computer and use it in GitHub Desktop.
# 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