文档的余弦相似度

时间:2018-10-09 11:38:49

标签: r text-mining cosine-similarity

数据格式CSV

文档总数500。字段数10。

数据视图compiler explorer

我想计算每个“文档”与所有500个文档的平行余弦相似度,

预期投入

enter image description here

1 个答案:

答案 0 :(得分:2)

这是您想要的吗?要计算所有(500 * 499)/ 2个组合的相似度,您可以执行以下操作:

# Create some mock data
df <-replicate(10, rnorm(500))
rownames(df) <- paste0("doc", seq_len(nrow(df)))
colnames(df) <- paste0("field", seq_len(ncol(df)))


# Vector lengths
vl <- sqrt(rowSums(df*df))

# Matrix of all combinations
comb <- t(combn(1:nrow(df), 2))

# Compute cosine similarity for all combinations
csim <- apply(comb, 1, FUN = function(i) sum(apply(df[i, ], 2, prod))/prod(vl[i]))

# Create a data.frame of the results
res <- data.frame(docA = rownames(df)[comb[,1]],
                  docB = rownames(df)[comb[,2]],
                  csim = csim)
head(res)
#  docA docB       csim
#1 doc1 doc2 -0.6431972
#2 doc1 doc3 -0.2560444
#3 doc1 doc4 -0.4911942
#4 doc1 doc5 -0.2207487
#5 doc1 doc6  0.4764924
#6 doc1 doc7  0.5867607

tail(res)
#         docA   docB      csim
#124745 doc497 doc498 1.0714338
#124746 doc497 doc499 0.8439304
#124747 doc497 doc500 1.1806366
#124748 doc498 doc499 0.9326781
#124749 doc498 doc500 1.4783254
#124750 doc499 doc500 1.3626494

请注意,在此输出表中具有字段的原始矢量值真的没有意义。每个数字都是数据中两行的比较和计算。

修改

如果您不希望它具有矩阵形式,则可以通过以下方式直接进行计算:

res_mat <- tcrossprod(df)/tcrossprod(vl)
print(res_mat[1:5, 1:5])
#           doc1       doc2       doc3       doc4       doc5
#doc1  1.0000000 -0.6431972 -0.2560444 -0.4911942 -0.2207487
#doc2 -0.6431972  1.0000000  0.3996618  0.3365490 -0.1434239
#doc3 -0.2560444  0.3996618  1.0000000  0.2856842  0.2781019
#doc4 -0.4911942  0.3365490  0.2856842  1.0000000  0.2287057
#doc5 -0.2207487 -0.1434239  0.2781019  0.2287057  1.0000000