我正在使用Java中的 Mallet 计算 LDA 的模型估计值,我正在寻找 term-topic-matrix 。< / p>
计算模型并获得主题文档矩阵很顺利:
ParallelTopicModel model = ...; //... estimating the model
int numTopics = model.getNumTopics();
int numDocs = model.getData().size();
// Getting the topic-probabilities
double[][] tmDist = new double[numDocs][];
for (int i = 0; i < numTopics; i++) {
tmDist[i] = model.getTopicProbabilities(i);
}
现在我只能得到顶级的 n 字样:
Object[][] topWords = model.getTopWords(5);
for(int i = 0; i < topWords.length; i++){
for(int j = 0; j < topWords[i].length; j++){
System.out.print(topWords[i][j] + " ");
}
System.out.println();
}
关于该问题的唯一答案我只发现了这个问题的问题/答案是关于Mallet的命令行版本。
答案 0 :(得分:-1)
这段代码将为您提供特定文档所有单词的主题分配。
for (int topic = 0; topic < numTopics; topic++) {
Iterator<IDSorter> iterator = topicSortedWords.get(topic).iterator();
out = new Formatter(new StringBuilder(), Locale.US);
out.format("%d\t%.3f\t", topic, model.getTopicProbabilities(docID)[topic]);
int rank = 0;
while (iterator.hasNext() && rank < 5) {
IDSorter idCountPair = iterator.next();
out.format("%s (%.3f) ", dataAlphabet.lookupObject(idCountPair.getID()), idCountPair.getWeight());
rank++;
}
System.out.println(out);
}
System.out.println("\n");