r - 从 TermDocumentMatrix 创建稀疏矩阵

标签 r sparse-matrix tm term-document-matrix

我已经从 R 中的 tm 库创建了一个 TermDocumentMatrix。它看起来像这样:

> inspect(freq.terms)

A document-term matrix (19 documents, 214 terms)

Non-/sparse entries: 256/3810
Sparsity           : 94%
Maximal term length: 19 
Weighting          : term frequency (tf)

Terms
Docs abundant acid active adhesion aeropyrum alternative
  1         0    0      1        0         0           0
  2         0    0      0        0         0           0
  3         0    0      0        1         0           0
  4         0    0      0        0         0           0
  5         0    0      0        0         0           0
  6         0    1      0        0         0           0
  7         0    0      0        0         0           0
  8         0    0      0        0         0           0
  9         0    0      0        0         0           0
  10        0    0      0        0         1           0
  11        0    0      1        0         0           0
  12        0    0      0        0         0           0
  13        0    0      0        0         0           0
  14        0    0      0        0         0           0
  15        1    0      0        0         0           0
  16        0    0      0        0         0           0
  17        0    0      0        0         0           0
  18        0    0      0        0         0           0
  19        0    0      0        0         0           1

这只是矩阵的一小部分样本;我实际上正在处理 214 个术语。在小范围内,这很好。如果我想将我的 TermDocumentMatrix 转换为普通矩阵,我会这样做:

data.matrix <- as.matrix(freq.terms)

但是,我上面显示的数据只是总体数据的一个子集。我的总体数据可能至少有 10,000 个术语。当我尝试从总体数据创建 TDM 时,出现错误:

> Error cannot allocate vector of size n Kb

因此,从这里开始,我正在研究为我的 tdm 找到有效内存分配的替代方法。

我尝试将我的 tdm 转换为 Matrix 库中的稀疏矩阵,但遇到了同样的问题。

此时我有什么选择?我觉得我应该调查以下之一:

  • bigmemory/ff 软件包如所讨论的 here (尽管 bigmemory 软件包目前似乎不适用于 Windows)
  • irlba 包,用于计算我提到的 tdm 的部分 SVD here

我已经尝试了两个库中的函数,但似乎没有得到任何实质性的结果。有谁知道前进的最佳方法是什么?我花了很长时间摆弄这个问题,所以我想在我浪费更多时间走向错误的方向之前,我应该问问那些比我更有处理大型数据集经验的人。

编辑:将 10,00 更改为 10,000。谢谢@nograpes。

最佳答案

qdap 包似乎能够处理这么大的问题。第一部分是重新创建与 OP 问题匹配的数据集,然后是解决方案。截至qdap version 1.1.0与 tm 包兼容:

library(qdapDictionaries)

FUN <- function() {
   paste(sample(DICTIONARY[, 1], sample(seq(100, 10000, by=1000), 1, TRUE)), collapse=" ")
}

library(qdap)
mycorpus <- tm::Corpus(tm::VectorSource(lapply(paste0("doc", 1:15), function(i) FUN())))

这给出了类似的语料库...

现在是 qdap 方法。您必须首先将语料库转换为数据帧 (tm_corpus2df),然后使用 tdm 函数创建 TermDocumentMatrix。

out <- with(tm_corpus2df(mycorpus), tdm(text, docs))
tm::inspect(out)

## A term-document matrix (19914 terms, 15 documents)
## 
## Non-/sparse entries: 80235/218475
## Sparsity           : 73%
## Maximal term length: 19 
## Weighting          : term frequency (tf)

关于r - 从 TermDocumentMatrix 创建稀疏矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21688031/

相关文章:

r - 为什么在执行 map(str_match_all()) 后需要将数据帧索引到 map() ?

r - 计算 R 中稀疏矩阵的特征向量

r - 使 udpipe_annotate() 更快

当 output_file 路径为绝对路径时,rmarkdown::render 会导致错误

r - 如何将 .csv 文件的特定列加载到 R?

r - 从一个区间生成一组随机的唯一整数

python - tensorflow :使用 tf.matmul 将稀疏矩阵与稠密矩阵相乘时出错

python - 在 Python 中对稀疏矩阵执行分解

r - 将术语文档矩阵转换为 R 中的节点/边缘列表

R tm 包 tm.plugin.tags 停止工作