预测新数据的LDA主题

21

看起来这个问题可能已经被问过几次了(这里 和这里),但尚未得到答案。我希望这是由于之前的问题存在歧义(如评论所示)。如果我重复提出类似的问题违反了规定,我很抱歉,我只是认为那些问题不会得到新的答案。

无论如何,我对潜在狄利克雷分配(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"?

7
使用 topicmodels 包中的 newdata 参数会发生什么?看起来很相关.. http://cran.r-project.org/web/packages/topicmodels/topicmodels.pdf - Ben
2
唉,我不知道我怎么在文档中错过了那个。乍一看,似乎 posterior(train.lda,test) 就可以解决问题。 - David
1个回答

32

在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

3
干得好!test.topics[[2]]是一个矩阵,其中主题为列,新文档为行,单元格的值为后验概率。 - Ben
为了让程序正常工作,必须将Lines train <- AssociatedPress[1:100]和test <- AssociatedPress[101:150]的代码更改为train <- AssociatedPress[1:100,]和test <- AssociatedPress[101:150,]。 =) - hdvianna

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