我试图用稀疏矩阵和称为quanteda的包创建一个矩阵乘法,它使用data.table包,与这个线程here相关。所以
require(quanteda)
mytext <- c("Let the big dogs hunt", "No holds barred", "My child is an honor student")
myMatrix <-dfm(mytext, ignoredFeatures = stopwords("english"), stem = TRUE) #a data.table
as.matrix(myMatrix) %*% transpose(as.matrix(myMatrix))如何使矩阵乘法在这里与量子包和稀疏矩阵一起工作?
发布于 2017-01-09 19:00:51
这样做很好:
mytext <- c("Let the big dogs hunt",
"No holds barred",
"My child is an honor student")
myMatrix <- dfm(mytext)
myMatrix %*% t(myMatrix)
## 3 x 3 sparse Matrix of class "dgCMatrix"
## text1 text2 text3
## text1 5 . .
## text2 . 3 .
## text3 . . 6无需使用as.matrix()强制进入稠密矩阵。请注意,它不再是"dfmSparse“对象,因为它不再是按特性排列的文档矩阵。
发布于 2017-01-09 15:41:12
使用t命令而不是transpose命令进行矩阵乘法,以便
as.matrix(myMatrix) %*% t(as.matrix(myMatrix))另外,正如所评论的,as.matrix是非稀疏的,而矩阵::矩阵是稀疏的,但是这里没有必要,所以更好。
myMatrix %*% t(myMatrix)而且有可能更好
crossprod(myMatrix)
tcrossprod(myMatrix) 但它需要数值/复矩阵/向量参数,而不是使用问题中的示例:
require(quanteda)
mytext <- c("Let the big dogs hunt", "No holds barred", "My child is an honor student")
myMatrix <-dfm(mytext, ignoredFeatures = stopwords("english"), stem = TRUE)
crossprod(myMatrix)
tcrossprod(myMatrix)https://stackoverflow.com/questions/41551216
复制相似问题