R 中 2 个 DTM 的余弦相似度 [英] Cosine similarity of 2 DTMs in R

查看:22
本文介绍了R 中 2 个 DTM 的余弦相似度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个文档术语矩阵:

I have 2 Document term matrices:

  1. DTM 1 有 1000 个向量(1000 个文档)和
  2. DTM2 有 20 个向量(20 个文档)

所以基本上我想将 DTM1 的每个文档与 DTM2 进行比较,并希望使用余弦函数查看哪些 DTM1 文档与哪些 DTM2 文档最接近.任何指针都会有所帮助!

So basically I want to compare each document of DTM1 against DTM2 and would want to see which DTM1 docs are closest to which DTM2 docs using the cosine function. Any pointers would help!

我使用slam"包创建了一个余弦矩阵.

I have created a cosine matrix using the "slam" package.

Docs   –glyma –ie   –initi –stafford ‘bureaucratic’ ‘empti ‘holi ‘incontrovert
  1  0.000000   0 0.000000  0.000000       0.000000      0     0             0
  2  0.000000   0 0.000000  0.000000       0.000000      0     0             0
  3  0.000000   0 0.000000  0.000000       0.000000      0     0             0
  4  0.000000   0 0.000000  0.000000       0.000000      0     0             0
  5  0.000000   0 0.000000  0.000000       0.000000      0     0             0
  6  0.000000   0 0.000000  0.000000       4.906891      0     0             0
  7  0.000000   0 0.000000  4.906891       0.000000      0     0             0
  8  0.000000   0 0.000000  0.000000       0.000000      0     0             0
  9  0.000000   0 4.906891  0.000000       0.000000      0     0             0
  10 4.906891   0 0.000000  0.000000       0.000000      0     0             0

余弦函数结果为:

然而,这个矩阵将 DTM1 的文档相互比较.我希望将这些向量与 DTM2 的向量进行比较,然后为给定的 DTM1 文档找到最接近的 DTM2 文档.

However, this matrix compares the docs of DTM1 with one another. I want these vectors to be compared with the vectors of DTM2 and then find the closest DTM2 document for a given DTM1 document.

推荐答案

这里是一种计算两个矩阵之间余弦距离的方法.tm 的使用仅用于数据目的...

Here is a way to calculate the cosine distance between two matrices. The use of tm is just for data purposes...

library(slam)
library(tm)
data("acq")
data("crude")

dtm <- DocumentTermMatrix(c(acq, crude))

index <- sample(1:70, size = 10)

dtm1 <- dtm[index, ]
dtm2 <- dtm[-index, ]

cosine_sim <- tcrossprod_simple_triplet_matrix(dtm1, dtm2)/sqrt(row_sums(dtm1^2) %*% t(row_sums(dtm2^2)))

余弦函数改编自这篇 SO 帖子:R:使用 tm 和代理计算术语文档矩阵的余弦距离

The cosine function was adapted from this SO post: R: Calculate cosine distance from a term-document matrix with tm and proxy

这篇关于R 中 2 个 DTM 的余弦相似度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆