Skip to content

Instantly share code, notes, and snippets.

@Krishna829
Created June 11, 2020 13:12
Show Gist options
  • Save Krishna829/56f5dde6430ff7a1b7b1d2b3409b7c53 to your computer and use it in GitHub Desktop.
Save Krishna829/56f5dde6430ff7a1b7b1d2b3409b7c53 to your computer and use it in GitHub Desktop.
Recommender System #RecommenderSystem
from sklearn.feature_extraction.text import TfidfVectorizer
tf = TfidfVectorizer(analyzer='word',ngram_range=(1, 2),min_df=0, stop_words='english')
tfidf_matrix = tf.fit_transform(movies['genres'])
from sklearn.metrics.pairwise import linear_kernel
cosine_sim = linear_kernel(tfidf_matrix, tfidf_matrix)
# Build a 1-dimensional array with movie titles
titles = movies['title']
indices = pd.Series(movies.index, index=movies['title'])
# Function that get movie recommendations based on the cosine similarity score of movie genres
def genre_recommendations(title):
idx = indices[title]
sim_scores = list(enumerate(cosine_sim[idx]))
sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)
sim_scores = sim_scores[1:21]
movie_indices = [i[0] for i in sim_scores]
return titles.iloc[movie_indices]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment