【问题标题】:How to convert a sparse or simple_triplet_matrix into a tm-package Document Term Matrix without going through Corpus/VCorpus, in R?如何在 R 中将稀疏或 simple_triplet_matrix 转换为 tm 包文档术语矩阵而不通过语料库/VCorpus?
【发布时间】:2015-01-18 11:54:39
【问题描述】:

我有一个 sparseMatrix(库矩阵)或一个 simple_triplet_matrix(库 slam)的 docs x 术语,例如:

library(Matrix)
mat <- sparseMatrix(i = c(1,2,4,5,3), j = c(2,3,4,1,5), x = c(3,2,3,4,1))
rownames(mat) <- paste0("doc", 1:5)
colnames(mat) <- paste0("word", 1:5)

5 x 5 sparse Matrix of class "dgCMatrix"
     word1 word2 word3 word4 word5
doc1     .     3     .     .     .
doc2     .     .     2     .     .
doc3     .     .     .     .     1
doc4     .     .     .     3     .
doc5     4     .     .     .     .

或:

library(slam)
mat2 <- simple_triplet_matrix(c(1,2,4,5,3), j = c(2,3,4,1,5), v = c(3,2,3,4,1),
                          dimnames = list(paste0("doc", 1:5), paste0("word", 1:5)))

我希望将这些矩阵中的任何一个转换为 tm::Document-Term-Matrix,而不需要创建 Corpus/VCorpus。

这仅适用于小矩阵: In R tm package, build corpus FROM Document-Term-Matrix

我的矩阵很大,~16K x ~53K,所以列表建议对于合理的 RAM 来说太大了,此外我不明白为什么我应该通过 tm 包手册明确说明的语料库创建Document Term Matrix 是一个稀疏矩阵。

关于如何将已经稀疏的矩阵转换为 tm 的 Document Term Matrix 有什么建议吗?

谢谢。

【问题讨论】:

    标签: r sparse-matrix text-mining tm


    【解决方案1】:

    这里的文档确实有点棘手。您可以在simple_triplet_matrix 上使用强制函数as.DocumentTermMatrix,但不能使用直接构造函数DocumentTermMatrix

    library(slam)
    library(Matrix)
    mat2 = simple_triplet_matrix(c(1,2,4,5,3), j = c(2,3,4,1,5), v = c(3,2,3,4,1),
                                  dimnames = list(paste0("doc", 1:5), paste0("word", 1:5)))
    mat2 = as.DocumentTermMatrix(mat2, weighting = weightTfIdf)
    

    您可以查看:

    > class(mat2)
    [1] "DocumentTermMatrix"    "simple_triplet_matrix"
    

    【讨论】:

      猜你喜欢
      • 2021-07-27
      • 1970-01-01
      • 1970-01-01
      • 2014-03-22
      • 1970-01-01
      • 2015-05-05
      • 1970-01-01
      • 2017-03-12
      • 1970-01-01
      相关资源
      最近更新 更多