使用Spark LDA可视化主题

5
我正在使用pySpark ML LDA库对来自sklearn的20个新闻组数据集进行主题建模。我正在对训练语料库进行标准分词、停用词去除和tf-idf转换。最后,我可以获取主题并打印单词索引及其权重:
topics = model.describeTopics()
topics.show()
+-----+--------------------+--------------------+
|topic|         termIndices|         termWeights|
+-----+--------------------+--------------------+
|    0|[5456, 6894, 7878...|[0.03716766297248...|
|    1|[5179, 3810, 1545...|[0.12236370744240...|
|    2|[5653, 4248, 3655...|[1.90742686393836...|
...

然而,如何将术语索引映射到实际单词以可视化主题呢? 我使用HashingTF应用于标记化的字符串列表来推导术语索引。如何生成一个字典(从索引到单词的映射)以便可视化主题?
2个回答

6
一个替代 HashingTF 的方法是使用 CountVectorizer 生成词汇表:
count_vec = CountVectorizer(inputCol="tokens_filtered", outputCol="tf_features", vocabSize=num_features, minDF=2.0)
count_vec_model = count_vec.fit(newsgroups)  
newsgroups = count_vec_model.transform(newsgroups)
vocab = count_vec_model.vocabulary

给定一个单词列表作为词汇表,我们可以索引进去以可视化主题:

topics = model.describeTopics()   
topics_rdd = topics.rdd

topics_words = topics_rdd\
       .map(lambda row: row['termIndices'])\
       .map(lambda idx_list: [vocab[idx] for idx in idx_list])\
       .collect()

for idx, topic in enumerate(topics_words):
    print "topic: ", idx
    print "----------"
    for word in topic:
       print word
    print "----------"

下一步可以将主题列表、文档、count_vect_model和词汇表提供给Gensim的相干性模型,以获取相干性分数。 - Tolga

1

HashingTF是不可逆的,也就是说,你无法从单词的输出索引中获取输入单词。多个单词可能映射到同一个输出索引。你可以使用CountVectorizer,这是一个类似但可逆的过程。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接