剧情推荐引擎
在本练习中,您将构建一个基于剧情相似度的电影推荐引擎。我们已经为您提供了一个 get_recommendations() 函数,它接收电影标题、相似度矩阵和一个 indices 序列作为参数,并输出最相似的电影列表。indices 已为您准备好。
您还获得了一个包含多部电影剧情简介的 movie_plots Series。您的任务是为这些剧情的 tf-idf 向量生成一个余弦相似度矩阵。
最后,我们将通过为我最喜欢的电影之一 The Dark Knight Rises 生成推荐来检验引擎的效果。
本练习是课程的一部分
Python 中的 NLP 特征工程
练习说明
- 使用英文
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))