我正在尝试使用 Java 中的 Apache Spark ML 创建 LDA 模型。输入文档为字符串格式。我得到数字格式的主题,但不是文字格式。
发现了一个类似的问题,但遗憾的是解决方案是在 R- LDA with topicmodels, how can I see which topics different documents belong to?但我正在寻找使用 Java 中的 Spark ML 库的解决方案。
任何帮助将不胜感激。谢谢!
最佳答案
如果您在管道中使用 CountVectorizer 转换器,您可以通过以下方式恢复索引词汇表:
String[] vocabulary= countVectorizerModel.vocabulary();
然后,您对从该文本>(术语计数)转换获得的 SparseVector 运行 LDA。
查看 LDA 结果时,
Tuple2<int[], double[]>[] topicsDescribed = ldaModel.describeTopics();
int idxTopic = 0;
for (Tuple2<int[], double[]> element : topicsDescribed) {
idxTopic++;
int[] termIndices = element._1;
double[] termScores = element._2;
System.out.println("Topic >> " + idxTopic);
for (int i = 0; i < termIndices.length; i++) {
System.out.println("termIndex --> " + termIndices[i] + + "word="+ vocabulary[termIndices[i]] + + ",score= " + termScores[i]);
}
}
}
这是有效的,因为您可以在整个管道中保持术语词汇的一致性,例如
ldaModel.vocabSize() == vocabulary.length
关于java - 如何使用java中的spark以word格式查看LDA主题建模中的主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39229425/