开始使用免费开始使用

创建字典与语料库

要运行 LDA 主题模型,首先需要先定义字典与语料库(dictionary 与 corpus),因为模型需要它们作为输入。您将继续使用之前练习中清洗好的文本数据。这意味着 text_clean 已经为您准备好,接下来将用它来创建字典和语料库。

本练习的运行时间会比平时稍长。

本练习是课程的一部分

Python 中的欺诈检测

查看课程

练习说明

  • 分别导入 gensim 包以及 gensim 中的 corpora。
  • 在清洗后的数据 text_clean 上运行正确的函数以定义字典。
  • text_clean 中的每段文本运行 doc2bow 来定义语料库。
  • 打印结果,查看 dictionarycorpus 的具体样子。

交互式实操练习

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

# Import the packages
import ____
from ____ import ____

# Define the dictionary
dictionary = ____.____(____)

# Define the corpus 
corpus = [dictionary.____(text) for ___ in ____]

# Print corpus and dictionary
print(____)
print(____)
编辑并运行代码