如何在 R 中不通过 Corpus/VCorpus 将稀疏或 simple_triplet_matrix 转换为 tm-package 文档术语矩阵?
How to convert a sparse or simple_triplet_matrix into a tm-package Document Term Matrix without going through Corpus/VCorpus, in R?
我有一个 sparseMatrix(库矩阵)或一个 simple_triplet_matrix(库 slam)文档 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 包手册明确的地方创建语料库表示文档术语矩阵是稀疏矩阵。
关于如何将已经稀疏的矩阵转换为 tm 的文档术语矩阵有什么建议吗?
谢谢。
这里的文档确实有点棘手。您可以在 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"
我有一个 sparseMatrix(库矩阵)或一个 simple_triplet_matrix(库 slam)文档 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 包手册明确的地方创建语料库表示文档术语矩阵是稀疏矩阵。
关于如何将已经稀疏的矩阵转换为 tm 的文档术语矩阵有什么建议吗?
谢谢。
这里的文档确实有点棘手。您可以在 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"