【问题标题】:Topicmodels transposes the term document matrixTopicmodels 转置术语文档矩阵
【发布时间】:2012-10-20 19:16:52
【问题描述】:

我正在尝试使用 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)

任何帮助将不胜感激。

【问题讨论】:

    标签: r


    【解决方案1】:

    一方面你有一个“TermDocumentMatrix”类的对象,另一方面你有一个“DocumentTermMatrix”。

    您可能只需要这样做:

    art_tdm<-DocumentTermMatrix(art_cor)
    

    【讨论】:

    • 是的,就是这样。谢谢!我还发现矩阵转置函数t() 也适用于这个类,并且达到了相同的结果。
    • 我想知道使用t()。但是,我无法在 package::tm 中找到“AssociatedPress”。
    猜你喜欢
    • 2018-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-27
    • 2015-05-19
    • 2018-04-29
    • 1970-01-01
    相关资源
    最近更新 更多