劇情推薦引擎
在這個練習中,我們要建立一個根據劇情相似度來推薦電影的引擎。你已經拿到一個 get_recommendations() 函式,它會接收電影標題、相似度矩陣,以及 indices Series 作為引數,並輸出最相似電影的清單。indices 已經提供給你。
另外,你也拿到一個 movie_plots Series,裡面包含多部電影的劇情摘要。你的任務是為這些劇情的 tf-idf 向量產生一個餘弦相似度矩陣。
最後,我們會用我最喜歡的電影之一「The Dark Knight Rises」來產生推薦,檢視這個引擎的效果。
本練習屬於課程
Python 中文本特徵工程
練習說明
- 以 English
stop_words初始化TfidfVectorizer,命名為tfidf。 - 使用
fit_transform()擬合並轉換電影劇情資料,建立tfidf_matrix。 - 使用
tfidf_matrix產生餘弦相似度矩陣cosine_sim。不要使用cosine_similarity()! - 使用
get_recommendations()為'The Dark Knight Rises'產生推薦。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Initialize the TfidfVectorizer
tfidf = ____(____='english')
# Construct the TF-IDF matrix
tfidf_matrix = tfidf.____(movie_plots)
# Generate the cosine similarity matrix
cosine_sim = ____(tfidf_matrix, tfidf_matrix)
# Generate recommendations
print(get_recommendations(____, cosine_sim, indices))