我正在尝试根据文档术语矩阵计算相似度。
A <- data.frame(name = c(
"X-ray right leg arteries",
"x-ray left shoulder",
"x-ray leg arteries"
), stringsAsFactors = F)
B <- data.frame(name = c(
"X-ray left leg arteries",
"xray right leg",
"X-ray right leg arteries",
"x-ray leg with 20km distance"
), stringsAsFactors = F)
library(quanteda)
corp1 <- corpus(A, text_field = "name")
corp2 <- corpus(B, text_field = "name")
docnames(corp1) <- paste("A", seq_len(ndoc(corp1)), sep = ".")
docnames(corp2) <- paste("B", seq_len(ndoc(corp2)), sep = ".")
dtm3 <- rbind(dfm(corp1, ngrams=1), dfm(corp2, ngrams=1))
有更快的方法进行以下计算吗?我需要在一个非常大的矩阵上进行。
# Similarity
m = matrix(nrow = length(docnames(corp1)), ncol = length(docnames(corp2)))
for (x in 1:length(docnames(corp1))) {
for (y in 1:length(docnames(corp2))) {
m[x,y] = sum(dtm3[x,] * dtm3[y+length(docnames(corp1)),]) / min(sum(dtm3[x,]) , sum(dtm3[y+length(docnames(corp1)),]))
}
}
rownames(m) = docnames(corp1)
colnames(m) = docnames(corp2)
m
以上代码执行这些计算 -
sum(dtm3[1,] * dtm3[4,]) / min(sum(dtm3[1,]) , sum(dtm3[4,]))
sum(dtm3[1,] * dtm3[5,]) / min(sum(dtm3[1,]) , sum(dtm3[5,]))
答案 0 :(得分:0)
使用基础R你可以运行
X <- dtm3[1:length(docnames(corp1)), ]
Y <- dtm3[length(docnames(corp1)) + 1:length(docnames(corp2)), ]
rX <- rowSums(X)
rY <- rowSums(Y)
X %*% t(Y) / outer(rX, rY, FUN = `pmin`)
# 3 x 4 Matrix of class "dgeMatrix"
# B.1 B.2 B.3 B.4
# A.1 0.7500000 0.6666667 1.0000000 0.5000000
# A.2 0.6666667 0.0000000 0.3333333 0.3333333
# A.3 1.0000000 0.3333333 1.0000000 0.6666667