开始使用免费开始使用

创建 TF-IDF DataFrame

现在您已经生成了 TF-IDF 特征,需要将其整理成可用于生成推荐的格式。 您将再次借助 pandas,把数组包装成一个 DataFrame。 由于您将使用电影标题来过滤数据,可以把标题赋给 DataFrame 的索引。

已为您加载 df_plots DataFrame。它在 Title 列中包含电影名称,在 Plot 列中包含剧情简介。

本练习是课程的一部分

用 Python 构建推荐引擎

查看课程

练习说明

  • 像上一个练习那样创建一个 TfidfVectorizer,并对数据进行拟合和转换。
  • 将生成的 vectorized_data 包装为一个 DataFrame。使用在拟合与转换阶段生成的特征名称作为其列名,并将新 DataFrame 赋值为 tfidf_df
  • 将原始电影标题赋给新建 tfidf_df DataFrame 的索引。

交互式实操练习

通过完成这段示例代码来试试这个练习。

from sklearn.feature_extraction.text import TfidfVectorizer

# Instantiate the vectorizer object and transform the plot column
vectorizer = ____(max_df=0.7, min_df=2)
vectorized_data = vectorizer.____(df_plots['Plot']) 

# Create Dataframe from TF-IDFarray
tfidf_df = pd.____(____.toarray(), columns=vectorizer.____())

# Assign the movie titles to the index and inspect
tfidf_df.____ = ____['Title']
print(tfidf_df.head())
编辑并运行代码