Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 将文档术语矩阵转换为包含大量数据的矩阵会导致溢出_R_Memory Management_Text_Matrix_Mining - Fatal编程技术网

R 将文档术语矩阵转换为包含大量数据的矩阵会导致溢出

R 将文档术语矩阵转换为包含大量数据的矩阵会导致溢出,r,memory-management,text,matrix,mining,R,Memory Management,Text,Matrix,Mining,让我们做一些文本挖掘 这里我站在一个文档术语矩阵(来自tm包) 我看到它是一个“列表”,其结构如下所示 Docs Terms 1 2 ... lorem 0 0 ... ipsum 0 0 ... ... ....... 所以我试着 wordMatrix = as.data.frame( t(as.matrix( dtm )) ) 这适用于1000个文档 但当我试着用40000的时候,它就不再有用了 我得到这个错误: Fehle

让我们做一些文本挖掘

这里我站在一个文档术语矩阵(来自
tm
包)

我看到它是一个“列表”,其结构如下所示

Docs
Terms        1 2 ...
  lorem      0 0 ...
  ipsum      0 0 ...
  ...        .......
所以我试着

wordMatrix = as.data.frame( t(as.matrix(  dtm )) ) 
这适用于1000个文档

但当我试着用40000的时候,它就不再有用了

我得到这个错误:

Fehler in vector(typeof(x$v), nr * nc) : Vektorgröße kann nicht NA sein
Zusätzlich: Warnmeldung:
In nr * nc : NAs durch Ganzzahlüberlauf erzeugt
向量中的错误…:向量不可能是NA 其他: 在由整数溢出创建的nr*nc NAs中

我看了一下as.matrix,结果发现函数以某种方式将它转换成一个向量,而不是一个矩阵。 到向量的转换有效,但从向量到矩阵的转换无效

你有什么建议吗?有什么问题吗


谢谢,Captain

整数溢出准确地告诉您问题所在:有40000个文档,您的数据太多了。在转换为矩阵的过程中,问题开始了,顺便说一句,如果您查看底层函数的代码,可以看出这一点:

class(dtm)
[1] "TermDocumentMatrix"    "simple_triplet_matrix"

getAnywhere(as.matrix.simple_triplet_matrix)

A single object matching ‘as.matrix.simple_triplet_matrix’ was found
...
function (x, ...) 
{
    nr <- x$nrow
    nc <- x$ncol
    y <- matrix(vector(typeof(x$v), nr * nc), nr, nc)
   ...
}
函数
vector()
接受一个长度为的参数,在本例中,如果长度大于appx,则取
nr*nc
。2e9(
.Machine$integer.max
),它将被NA替换。此NA作为
vector()
的参数无效

底线:你正在接近R的极限。就目前而言,在64位上工作对你没有帮助。你得采取不同的方法。一种可能是继续使用您拥有的列表(dtm是一个列表),使用列表操作选择您需要的数据并从中开始

PS:我通过

require(tm)
data("crude")
dtm <- TermDocumentMatrix(crude,
                          control = list(weighting = weightTfIdf,
                                         stopwords = TRUE))
require(tm)
数据(“原油”)

dtm这是我最近发现的一个非常简单的解决方案

DTM=t(TDM)#taking the transpose of Term-Document Matrix though not necessary but I prefer DTM over TDM
M=as.big.matrix(x=as.matrix(DTM))#convert the DTM into a bigmemory object using the bigmemory package 
M=as.matrix(M)#convert the bigmemory object again to a regular matrix
M=t(M)#take the transpose again to get TDM
请注意,转置TDM以获得DTM是绝对可选的,我个人喜欢以这种方式使用矩阵


4年前,p.S.无法回答这个问题,因为我只是大学里的一名新生

根据Joris Meys的回答,我找到了解决办法。关于“长度”参数的“vector()”文档

。。。 对于长向量,即length>.Machine$integer.max,它必须是“double”类型

所以我们可以对as.matrix()进行一个小的修正:


as.big.matrix感谢您的澄清。我将尝试稀疏dtm,并希望我能够执行转换。使dtm在内存限制下的一种简单方法可能是使用
tm::removeSparseTerms
功能删除稀疏项。一种避免包含非常罕见或单独出现的项的简单方法是使用
DocumentTermMatrix(…,控件)(…bounds=list(global=c(N,Inf)))
并将N设置为例如2,3,4…直到大小足够小。
as.integer(40000 * 60000) # 40000 documents is 40000 rows in the resulting frame
[1] NA
Warning message:
NAs introduced by coercion 
require(tm)
data("crude")
dtm <- TermDocumentMatrix(crude,
                          control = list(weighting = weightTfIdf,
                                         stopwords = TRUE))
DTM=t(TDM)#taking the transpose of Term-Document Matrix though not necessary but I prefer DTM over TDM
M=as.big.matrix(x=as.matrix(DTM))#convert the DTM into a bigmemory object using the bigmemory package 
M=as.matrix(M)#convert the bigmemory object again to a regular matrix
M=t(M)#take the transpose again to get TDM
as.big.matrix <- function(x) {
  nr <- x$nrow
  nc <- x$ncol
  # nr and nc are integers. 1 is double. Double * integer -> double
  y <- matrix(vector(typeof(x$v), 1 * nr * nc), nr, nc)
  y[cbind(x$i, x$j)] <- x$v
  dimnames(y) <- x$dimnames
  y
}