R “从类转化”;简单的“U三元组”矩阵;“去课堂”;“矩阵”;
我正在尝试转换以下使用R “从类转化”;简单的“U三元组”矩阵;“去课堂”;“矩阵”;,r,matrix,tm,R,Matrix,Tm,我正在尝试转换以下使用tm包的TermDocumentMatrix()创建的简单三元组矩阵 A term-document matrix (317443 terms, 86960 documents) Non-/sparse entries: 18472230/27586371050 Sparsity : 100% Maximal term length: 653 Weighting : term frequency (tf) 一流的 [1] "Ter
tm
包的TermDocumentMatrix()
创建的简单三元组矩阵
A term-document matrix (317443 terms, 86960 documents)
Non-/sparse entries: 18472230/27586371050
Sparsity : 100%
Maximal term length: 653
Weighting : term frequency (tf)
一流的
[1] "TermDocumentMatrix" "simple_triplet_matrix"
到密集矩阵
但是
我不能真正理解错误和警告信息。正在尝试在具有的小数据集上复制错误
library(tm)
data("crude")
tdm <- TermDocumentMatrix(crude)
as.matrix(tdm)
library(tm)
数据(“原油”)
tdm您会得到一个错误,因为正如所评论的,您达到了整数限制的限制,这是正常的,因为您有大量的文档。。这将再现错误:
as.integer(.Machine$integer.max+1)
[1] NA
Warning message:
NAs introduced by coercion
函数vector
将整数作为参数,但由于第二个参数为NA,因此失败
一种解决方案是将重新定义为.matrix.simple\u triplet\u matrix
,而不调用vector
。例如:
as.matrix.simple_triplet_matrix <-
function (x, ...)
{
nr <- x$nrow
nc <- x$ncol
## old line: y <- matrix(vector(typeof(x$v), nr * nc), nr, nc)
y <- matrix(0, nr, nc) ##
y[cbind(x$i, x$j)] <- x$v
dimnames(y) <- x$dimnames
y
}
我只是有一个类似的问题。我不确定我的问题是否相同,但当将稀疏矩阵与密集矩阵组合时,我得到了一条类似的错误消息,即整数溢出产生的NAs。我能够通过使用as.single
将密集矩阵转换为单精度来修复它。我认为“溢出整数”是由sparseMatrix
包中的操作造成的,这些操作以某种方式截断了双精度值,留下了剩余的数字。行数乘以列数,nr*nc
,对于可用存储空间来说太大了。嗯,我需要一个密集矩阵来对其执行一些矩阵操作(特别是乘法)…您是否建议直接对三元组矩阵进行运算更好?是的!它只是一个长格式dtm[i,j,value]的列表。试着看看你是否能用这种格式进行矩阵运算。我在我的tdm上试过这个函数:错误:无法分配大小为205.7 Gb的向量
我想奥德修斯在经过大力神的柱子时也会有类似的感觉……你的编辑帮助了我。我能够将矩阵相乘。
as.integer(.Machine$integer.max+1)
[1] NA
Warning message:
NAs introduced by coercion
as.matrix.simple_triplet_matrix <-
function (x, ...)
{
nr <- x$nrow
nc <- x$ncol
## old line: y <- matrix(vector(typeof(x$v), nr * nc), nr, nc)
y <- matrix(0, nr, nc) ##
y[cbind(x$i, x$j)] <- x$v
dimnames(y) <- x$dimnames
y
}
require(tm)
data("crude")
dtm <- TermDocumentMatrix(crude,
control = list(weighting = weightTfIdf,
stopwords = TRUE))
library(Matrix)
Dense <- sparseMatrix(dtm$i,dtm$j,x=dtm$v)
dense <- as.matrix(dtm)
## check sizes
floor(as.numeric(object.size(dense)/object.size(Dense)))
## addistion and multiplication are supported
Dense+Dense
Dense*Dense