预测新数据的LDA主题

Dav*_*vid 17 r lda topic-modeling

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

无论如何,我是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"?
Run Code Online (Sandbox Code Playgroud)

Dav*_*vid 25

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

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
Run Code Online (Sandbox Code Playgroud)

  • 干得好!`test.topics [[2]]`是主题为cols的矩阵,新文档为行,单元格值为后验概率. (3认同)