预测新数据的LDA主题

时间:2013-04-20 00:01:58

标签: r lda topic-modeling

看起来此问题之前可能已被问过几次(here and here),但尚未得到回答。我希望这是由于之前提出的问题含糊不清,正如评论所表明的那样。如果我通过再次提出一个类似问题来破坏协议,我道歉,我只是假设这些问题不会有任何新的答案。

无论如何,我是Latent Dirichlet Allocation的新手,正在探索将其用作文本数据降维的方法。最后,我想从一大堆单词中提取一小组主题,并使用这些主题作为模型中的一些变量来构建分类模型。我已经成功地在训练集上运行LDA,但我遇到的问题是能够预测哪些相同的主题出现在其他一些测试数据集中。我现在正在使用R的topicmodels包,但是如果还有另一种使用其他包的方法,我也会对此持开放态度。

这是我想要做的一个例子:

library(topicmodels)
data(AssociatedPress)

train <- AssociatedPress[1:100]
test <- AssociatedPress[101:150]

train.lda <- LDA(train,5)
topics(train.lda)

#how can I predict the most likely topic(s) from "train.lda" for each document in "test"?

1 个答案:

答案 0 :(得分:24)

在Ben的优秀文档阅读技巧的帮助下,我相信这可以使用后验()函数。

library(topicmodels)
data(AssociatedPress)

train <- AssociatedPress[1:100]
test <- AssociatedPress[101:150]

train.lda <- LDA(train,5)
(train.topics <- topics(train.lda))
#  [1] 4 5 5 1 2 3 1 2 1 2 1 3 2 3 3 2 2 5 3 4 5 3 1 2 3 1 4 4 2 5 3 2 4 5 1 5 4 3 1 3 4 3 2 1 4 2 4 3 1 2 4 3 1 1 4 4 5
# [58] 3 5 3 3 5 3 2 3 4 4 3 4 5 1 2 3 4 3 5 5 3 1 2 5 5 3 1 4 2 3 1 3 2 5 4 5 5 1 1 1 4 4 3

test.topics <- posterior(train.lda,test)
(test.topics <- apply(test.topics$topics, 1, which.max))
#  [1] 3 5 5 5 2 4 5 4 2 2 3 1 3 3 2 4 3 1 5 3 5 3 1 2 2 3 4 1 2 2 4 4 3 3 5 5 5 2 2 5 2 3 2 3 3 5 5 1 2 2