R :: tm - 创建术语关联频率的 table/matrix 并将值添加到树状图
R :: tm - Create a table/matrix of term association frequencies and add values to dendrogram
我得到的语料库基本上是一个短句向量 (n > 50),例如:
corpus <- c("looking for help in R","check whether my milk is sour or not",
"random sentence with dubious meaning")
我可以打印树状图
fit <- hclust(d, method="ward")
plot(fit, hang=-1)
groups <- cutree(fit, k=nc) # "k=" defines the number of clusters you are using
rect.hclust(fit, k=nc, border="red") # draw dendrogram with red borders around the 5 clusters
和相关矩阵
cor_1 <- cor(as.matrix(dtms))
corrplot(cor_1, method = "number")
据我了解 - 如果我错了请在这里纠正我 - findAssocs()
即相关性检查两个术语是否出现在同一文档中?
目标:
现在我不想看到相关性,但两个术语出现在同一文档中的频率不一定彼此相邻(BigramTokenizer 不起作用)。例如:术语 A 和术语 B 在我的语料库中同时出现在 5 个不同的文档中,无论距离如何。
理想情况下,我想创建一个类似于上面的频率矩阵,并在可能的情况下将频率添加到树状图中(类似于 pvclust()
打印其数字的位置)
关于如何实现这一点有什么想法吗?
我想你问的是如何获得术语的共现矩阵,其中单元格是术语与另一个文档一起出现的文档数。在将文档术语频率矩阵转换为指示术语是否出现在文档中的布尔值之后,我们可以使用矩阵与其自身转置的矩阵叉积来实现这一魔法。
(我在这里使用了 quanteda 包而不是 tm 但类似的方法适用于 DocumentTermMatrix
来自 tm 的对象。)
# create some demonstration documents
(txts <- c(paste(letters[c(1, 1:3)], collapse = " "),
paste(letters[c(1, 3, 5)], collapse = " "),
paste(letters[c(5, 6, 7)], collapse = " ")))
## [1] "a a b c" "a c e" "e f g"
# convert to a document-term matrix
require(quanteda)
dtm <- dfm(txts, verbose = FALSE)
dtm
## Document-feature matrix of: 3 documents, 6 features.
## 3 x 6 sparse Matrix of class "dfmSparse"
## features
## docs a b c e f g
## text1 2 1 1 0 0 0
## text2 1 0 1 1 0 0
## text3 0 0 0 1 1 1
# convert to a matrix of co-occcurences rather than counts
(dtm <- tf(dtm, "boolean"))
## Document-feature matrix of: 3 documents, 6 features.
## 3 x 6 sparse Matrix of class "dfmSparse"
## features
## docs a b c e f g
## text1 1 1 1 0 0 0
## text2 1 0 1 1 0 0
## text3 0 0 0 1 1 1
# now get the "feature in document" co-occurrence matrix
t(dtm) %*% dtm
## 6 x 6 sparse Matrix of class "dgCMatrix"
## a b c e f g
## a 2 1 2 1 . .
## b 1 1 1 . . .
## c 2 1 2 1 . .
## e 1 . 1 2 1 1
## f . . . 1 1 1
## g . . . 1 1 1
注意:此设置将在文档中仅与自身一起出现的术语计为 "co-occurring" 一次(例如 b
)。如果要更改它,只需将对角线替换为对角线减一即可。
我得到的语料库基本上是一个短句向量 (n > 50),例如:
corpus <- c("looking for help in R","check whether my milk is sour or not",
"random sentence with dubious meaning")
我可以打印树状图
fit <- hclust(d, method="ward")
plot(fit, hang=-1)
groups <- cutree(fit, k=nc) # "k=" defines the number of clusters you are using
rect.hclust(fit, k=nc, border="red") # draw dendrogram with red borders around the 5 clusters
和相关矩阵
cor_1 <- cor(as.matrix(dtms))
corrplot(cor_1, method = "number")
据我了解 - 如果我错了请在这里纠正我 - findAssocs()
即相关性检查两个术语是否出现在同一文档中?
目标: 现在我不想看到相关性,但两个术语出现在同一文档中的频率不一定彼此相邻(BigramTokenizer 不起作用)。例如:术语 A 和术语 B 在我的语料库中同时出现在 5 个不同的文档中,无论距离如何。
理想情况下,我想创建一个类似于上面的频率矩阵,并在可能的情况下将频率添加到树状图中(类似于 pvclust()
打印其数字的位置)
关于如何实现这一点有什么想法吗?
我想你问的是如何获得术语的共现矩阵,其中单元格是术语与另一个文档一起出现的文档数。在将文档术语频率矩阵转换为指示术语是否出现在文档中的布尔值之后,我们可以使用矩阵与其自身转置的矩阵叉积来实现这一魔法。
(我在这里使用了 quanteda 包而不是 tm 但类似的方法适用于 DocumentTermMatrix
来自 tm 的对象。)
# create some demonstration documents
(txts <- c(paste(letters[c(1, 1:3)], collapse = " "),
paste(letters[c(1, 3, 5)], collapse = " "),
paste(letters[c(5, 6, 7)], collapse = " ")))
## [1] "a a b c" "a c e" "e f g"
# convert to a document-term matrix
require(quanteda)
dtm <- dfm(txts, verbose = FALSE)
dtm
## Document-feature matrix of: 3 documents, 6 features.
## 3 x 6 sparse Matrix of class "dfmSparse"
## features
## docs a b c e f g
## text1 2 1 1 0 0 0
## text2 1 0 1 1 0 0
## text3 0 0 0 1 1 1
# convert to a matrix of co-occcurences rather than counts
(dtm <- tf(dtm, "boolean"))
## Document-feature matrix of: 3 documents, 6 features.
## 3 x 6 sparse Matrix of class "dfmSparse"
## features
## docs a b c e f g
## text1 1 1 1 0 0 0
## text2 1 0 1 1 0 0
## text3 0 0 0 1 1 1
# now get the "feature in document" co-occurrence matrix
t(dtm) %*% dtm
## 6 x 6 sparse Matrix of class "dgCMatrix"
## a b c e f g
## a 2 1 2 1 . .
## b 1 1 1 . . .
## c 2 1 2 1 . .
## e 1 . 1 2 1 1
## f . . . 1 1 1
## g . . . 1 1 1
注意:此设置将在文档中仅与自身一起出现的术语计为 "co-occurring" 一次(例如 b
)。如果要更改它,只需将对角线替换为对角线减一即可。