主题模型将词项-文档矩阵转置。

4
我将使用R中的topicmodels包来运行LDA。手册中提供的示例使用美联社的数据,效果很好。然而,当我尝试在自己的数据上运行时,得到的主题是文档名称。我已经追踪到问题出在我的词项-文档矩阵是应该的倒置(行->列)。
示例TDM如下:
str(AssociatedPress)
List of 6
$ i       : int [1:302031] 1 1 1 1 1 1 1 1 1 1 ...

$ j       : int [1:302031] 116 153 218 272 299 302 447 455 548 597 ...
$ v       : int [1:302031] 1 2 1 1 1 1 2 1 1 1 ...
$ nrow    : int 2246
$ ncol    : int 10473
$ dimnames:List of 2
..$ Docs : NULL
..$ Terms: chr [1:10473] "aaron" "abandon" "abandoned" "abandoning" ...
- attr(*, "Weighting")= chr [1:2] "term frequency" "tf"
- attr(*, "class")= chr [1:2] "DocumentTermMatrix" "simple_triplet_matrix"

我的TDM中,术语作为行,文档作为列:

List of 6
$ i       : int [1:10489] 1 3 4 13 20 24 25 26 27 28 ...
$ j       : int [1:10489] 1 1 1 1 1 1 1 1 1 1 ...
$ v       : num [1:10489] 1 1 1 1 2 1 67 1 44 3 ...
$ nrow    : int 5903
$ ncol    : int 9
$ dimnames:List of 2
..$ Terms: chr [1:5903] "\u2439aa" "aars" "\u2439ab" "\u242dab" ...
..$ Docs : chr [1:9] "art111130.txt" "art111131.txt" "art111132.txt" "art111133.txt" ...
- attr(*, "class")= chr [1:2] "TermDocumentMatrix" "simple_triplet_matrix"
- attr(*, "Weighting")= chr [1:2] "term frequency" "tf"

是什么导致LDA(art_tdm,3)基于文档名称而不是文档内的术语构建主题。这是否是tm软件包代码库中的更改?我无法想象我会在我的代码中引起这种转置:

art_cor<-Corpus(DirSource(directory = "tmptxts"))
art_tdm<-TermDocumentMatrix(art_cor)

非常感谢您的帮助。

1个回答

3
一方面,您有一个“TermDocumentMatrix”类的对象,另一方面,则有一个“DocumentTermMatrix”类的对象。
您可能只需要执行以下操作:
art_tdm<-DocumentTermMatrix(art_cor)

1
是的,就是这样。谢谢!我还发现矩阵转置函数t()也适用于这个类,并且可以实现相同的结果。 - cjbayesian
我在考虑使用t()。但是我在package::tm中找不到“AssociatedPress”。 - IRTFM

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