NMF 学习文档主题
在视频中,您了解到当 NMF 应用于文档时,组件对应于文档的主题,而 NMF 特征可以由这些主题重构文档。请用您之前基于维基百科文章训练的 NMF 模型自行验证这一点。之前,您看到第 3 个 NMF 特征在关于演员 Anne Hathaway 和 Denzel Washington 的文章中取值较高。在本练习中,请识别与之对应的 NMF 组件的主题。
您之前构建的 NMF 模型可通过 model 获取,words 是给词频数组各列加标签的单词列表。
完成后,请思考一下:Anne Hathaway 和 Denzel Washington 的文章有何共同主题!
本练习是课程的一部分
Python 中的无监督学习
练习说明
- 导入
pandas并命名为pd。 - 从
model.components_创建一个 DataFramecomponents_df,并设置columns=words,使列以单词命名。 - 打印
components_df.shape,检查 DataFrame 的维度。 - 在 DataFrame
components_df上使用.iloc[]访问器选择第3行。将结果赋值给component。 - 调用
component的.nlargest()方法,并打印结果。这样可以得到该组件中取值最高的 5 个单词。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Import pandas
import pandas as pd
# Create a DataFrame: components_df
components_df = ____
# Print the shape of the DataFrame
print(components_df.shape)
# Select row 3: component
component = ____
# Print result of nlargest
print(component.nlargest())