-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent-based_recommender.py
68 lines (51 loc) · 2.28 KB
/
content-based_recommender.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#############################
# Content Based Recommendation
#############################
#############################
# Developing Recommendations Based on Movie Overviews
#############################
# 1. Setting Up TF-IDF Matrix
# 2. Setting Up Cosine Similarity Matrix
# 3. Making Recommendations Based on Similarities
#################################
# 1. Setting Up TF-IDF Matrix
#################################
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
df = pd.read_csv("movies_metadata_small.csv", low_memory=False)
df.head()
df["overview"].head()
#We need to translate textual expressions into measurable, mathematical expressions.
tfidf = TfidfVectorizer(stop_words="english") #we will subtract stop_words because it does not carry any measurement value
df[df["overview"].isnull()]
df["overview"] = df["overview"].fillna('')
# transform the overview by calling the vectorizer.
tfidf_matrix = tfidf.fit_transform(df["overview"])
tfidf_matrix.shape
df["title"].shape
tfidf.get_feature_names()
tfidf_matrix.toarray() #tf/idf scores
#################################
# 2. Setting Up Cosine Similarity Matrix
#################################
cosine_sim = cosine_similarity(tfidf_matrix, tfidf_matrix) #calculates cosine similarity for all pairs of documents we have one by one.
cosine_sim.shape
cosine_sim[1] # The similarity score of the 1st index movie with all the other movies
#################################
# 3. Making Recommendations Based on Similarities
#################################
indices = pd.Series(df.index, index = df["title"]) #film names and indexes according to name
indices["Cinderella"]
indices.index.value_counts() #duplicated titles...
indices = indices[~indices.index.duplicated(keep="last")] #We have deduplicated our indexes.
indices.index.value_counts()
indices["Cinderella"]
# Now let's find the similarities from Sherlock Holmes.
indices["Sherlock Holmes"]
movie_index = indices["Sherlock Holmes"]
cosine_sim[movie_index]
similarity_score = pd.DataFrame(cosine_sim[movie_index], columns=["score"])
# top 10 films..
movie_indices = similarity_score.sort_values("score", ascending=False)[1:11].index #0th film is Sherlock Holmes, pass away it.
df['title'].iloc[movie_indices]