R: LDA Topicmodels -术语的分布在哪里



主题是TermDocumentMatrix的术语(=words)上的多项分布。使用k=5作为主题数的标准数据集…

library(topicmodels)
data("AssociatedPress", package = "topicmodels")
k <- 5 
lda <- LDA(AssociatedPress[1:20,], control = list(alpha = 0.1), k)

str(lda)给出以下输出

Formal class 'LDA_VEM' [package "topicmodels"] with 14 slots
..@ alpha          : num 0.0184
..@ call           : language LDA(x = AssociatedPress[1:20, ], k = k, control = list(alpha = 0.1))
..@ Dim            : int [1:2] 20 10473
..@ control        :Formal class 'LDA_VEMcontrol' [package "topicmodels"] with 13 slots
.. .. ..@ estimate.alpha: logi TRUE
.. .. ..@ alpha         : num 0.1
.. .. ..@ seed          : int 1437208609
.. .. ..@ verbose       : int 0
.. .. ..@ prefix        : chr
.. .. ..@ save          : int 0
.. .. ..@ nstart        : int 1
.. .. ..@ best          : logi TRUE
.. .. ..@ keep          : int 0
.. .. ..@ estimate.beta : logi TRUE
.. .. ..@ var           :Formal class 'OPTcontrol' [package "topicmodels"] with 2 slots
.. .. .. .. ..@ iter.max: int 500
.. .. .. .. ..@ tol     : num 1e-06
.. .. ..@ em            :Formal class 'OPTcontrol' [package "topicmodels"] with 2 slots
.. .. .. .. ..@ iter.max: int 1000
.. .. .. .. ..@ tol     : num 1e-04
.. .. ..@ initialize    : chr "random"
..@ k              : int 5
..@ terms          : chr [1:10473] "aaron" "abandon" "abandoned" "abandoning" ...
..@ documents      : NULL
..@ beta           : num [1:5, 1:10473] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 ...
..@ gamma          : num [1:20, 1:5] 7.00e-05 6.79e-05 7.22e-05 8.89e-05 2.79e-04 ...
..@ wordassignments:List of 5
.. ..$ i   : int [1:2533] 1 1 1 1 1 1 1 1 1 1 ...
.. ..$ j   : int [1:2533] 116 153 218 272 299 302 447 455 548 597 ...
.. ..$ v   : num [1:2533] 5 5 5 5 5 5 5 5 5 5 ...
.. ..$ nrow: int 20
.. ..$ ncol: int 10473
.. ..- attr(*, "class")= chr "simple_triplet_matrix"
..@ loglikelihood  : num [1:20] -1512 -1584 -1400 -1324 -418 ...
..@ iter           : int 12
..@ logLiks        : num(0) 
..@ n              : int 3636

lda中没有对象似乎存储了我需要的数据。我知道gamma给出了文档上主题的分布,但是我如何访问术语上主题的分布呢?

您可以使用posterior(lda)$terms查看主题在术语上的后验分布。

library(topicmodels)
data("AssociatedPress", package = "topicmodels")
lda <- LDA(AssociatedPress[1:20,], control = list(alpha = 0.1), k = 2)
terms <- as.data.frame(t(posterior(lda)$terms))
head(terms)
                      1            2
aaron      3.720076e-44 3.720076e-44
abandon    3.720076e-44 3.720076e-44
abandoned  3.720076e-44 3.720076e-44
abandoning 3.720076e-44 3.720076e-44
abbott     3.720076e-44 3.720076e-44
abboud     3.720076e-44 3.720076e-44

看起来beta是作为一个k * n矩阵在fit对象中称为beta的槽中返回的。因此,您可以使用以下命令检查调用lda()的那个:

lda@beta

这些术语也在那里作为lda@terms,所以你可以用它们制作一个数据帧来查找特定的术语:

betas <- data.frame(t(lda@beta))
betas$term <- lda@terms
names(betas) <- c(paste("topic", seq(k), sep="."), "term")
head(betas)

下面是我运行的结果:

  topic.1 topic.2 topic.3 topic.4 topic.5       term
1    -100    -100    -100    -100    -100      aaron
2    -100    -100    -100    -100    -100    abandon
3    -100    -100    -100    -100    -100  abandoned
4    -100    -100    -100    -100    -100 abandoning
5    -100    -100    -100    -100    -100     abbott
6    -100    -100    -100    -100    -100     abboud

如果您扫描更多的表,您将看到哪些项的beta值不是-100。例如:

> betas[19,]
     topic.1   topic.2   topic.3   topic.4   topic.5 term
19 -181.7717 -176.7156 -6.919684 -196.3646 -6.398595 able

最新更新