2012-07-21 162 views
1
dtm <- DocumentTermMatrix(reuters, control=list(wordLengths=c(1,Inf))) 

我想转成DTM期限长期矩阵的,有什么下面是不正确的:如何将doc-term矩阵转换为term-term矩阵?

dtm <- dtm %*% t(dtm) 

怎么可能做到呢?

+0

你检查出这个问题的答案[问题](http://stackoverflow.com/q/7380133/1086688)? – nograpes 2012-07-21 12:23:14

回答

2

如果我正确理解文档项矩阵的结构,它是t(dtm) %*% dtm。见this answer

0

我认为这种方式如下将工作(注意:您要创建布尔或许和邻接矩阵):

t(as.matrix(dtm)) %*% as.matrix(dtm) 

对于大DTM你会反弹到使用as.matrix的r限制。 Matrix包可以提供帮助。注我切换ij在第一个矩阵中进行转置。

data("acq") 
dtm <- DocumentTermMatrix(acq, control=list(wordLengths=c(1,Inf))) 
tdm <- t(dtm) 

library(Matrix) 
Xt <- sparseMatrix(j=dtm$i, i=dtm$j, x=dtm$v) 
X <- sparseMatrix(j=tdm$i, i=tdm$j, x=tdm$v) 

Xt %*% X 

# For easier viewing 
(Xt %*% X) [1:20, 1:20] 
0
TDM <- TermDocumentMatrix(x) # Form a Term document matrix 

termDocMatrix <- as.matrix(TDM) # convert your TDM into a matrix 

termDocMatrix[termDocMatrix>=1] <- 1 # change the TDM into Boolean matrix 

# term adjacency matrix 
termMatrix <- termDocMatrix %*% t(termDocMatrix) 


termMatrix[1:10,1:10] # inspect terms numbered 1 to 10 
+0

嗨,欢迎来到stackoverflow。请更多地描述答案。当您在答案中有链接时,页面可能会被删除,您的答案将来对其他人无用 – 2016-08-27 17:38:27