[译]Cosine similarity

本文介绍了余弦相似度的概念及应用,特别是在文本挖掘中的作用。余弦相似度通过计算两个向量之间的夹角余弦值来衡量它们的相似度,适用于文档比较。文章还讨论了该方法在信息检索领域的特点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Cosine similarity [1][2] is a measure of similarity between two vectors of n dimensions by finding the cosine of the angle between them, often used to compare documents in text mining. Given two vectors of attributes, A and B, the cosine similarity, θ, is represented using a dot product and magnitude as

余弦相似度是利用两个n维向量的夹角余弦值来计算它们相似度的方法,经常用于在文本挖掘中比较文档.给定两个向量的属性(维度)A和B,它们的夹角θ,余弦相似度以点积和向量长度表示为

\text{similarity} = \cos(\theta) = {A \cdot B \over \|A\| \|B\|}.

 

For text matching, the attribute vectors A and B are usually the term frequency vectors of the documents. The cosine similarity can be seen as a method of normalizing document length during comparison.

在文本匹配中,属性(维度)向量通常是文档的词频向量.余弦相似度可以看作一个在比较中规范化文档长度的方法.

 

The resulting similarity ranges from −1 meaning exactly opposite, to 1 meaning exactly the same, with 0 indicating independence, and in-between values indicating intermediate similarity or dissimilarity.

相似度的结果的范围从-1(表示完全相反)到1(表示完全相同),0表示互相独立,其余的值表示文档的相似度和相反度.

 

In the case of information retrieval, the cosine similarity of two documents will range from 0 to 1, since the term frequencies (tf-idf weights) cannot be negative. The angle between two term frequency vectors cannot be greater than 90°.

在信息检索的领域,余弦相似度的值的范围是从0到1,因为词频(tf-idf权重)不可能为负数.两个词频向量的夹角不可能大于90度.

 

This cosine similarity metric may be extended such that it yields the Jaccard coefficient in the case of binary attributes. This is the Tanimoto coefficient, T(AB), represented as

余弦相似度的公式在二进制的情况下可以扩展到以Jaccard系数作为除数的值(分母).这是Tanimoto系数(广义Jaccard系数),T(A, B),表示为

T(A,B) = {A \cdot B \over \|A\|^2 +\|B\|^2 - A \cdot B}.

 

原始地址: http://en.wikipedia.org/wiki/Cosine_similarity

转载于:https://www.cnblogs.com/live41/archive/2010/01/03/1638222.html

### 余弦相似度(Cosine Similarity)的含义 余弦相似度是一种用于衡量两个向量在多维空间中夹角的相似性度量方法。其核心思想是通过计算两个向量的余弦值来判断它们的相似程度。余弦值的范围通常在 -1 到 1 之间,其中 1 表示两个向量完全相同(方向一致),0 表示两个向量正交(没有相似性),-1 表示两个向量方向完全相反[^2]。 #### 数学公式 余弦相似度的数学公式如下: $$ \cos(\theta) = \frac{\mathbf{A} \cdot \mathbf{B}}{\|\mathbf{A}\| \|\mathbf{B}\|} $$ 其中: - $\mathbf{A} \cdot \mathbf{B}$ 表示向量 $\mathbf{A}$ 和 $\mathbf{B}$ 的点积。 - $\|\mathbf{A}\|$ 和 $\|\mathbf{B}\|$ 分别表示向量 $\mathbf{A}$ 和 $\mathbf{B}$ 的模(长度)。 #### 应用场景 余弦相似度广泛应用于高维数据的相似性比较,例如: - **自然语言处理**:用于比较文档的语义相似度。 - **推荐系统**:用于衡量用户偏好的相似性。 - **图像识别**:用于比较图像的特征向量。 #### 示例代码 以下是一个使用 PyTorch 计算两个向量余弦相似度的示例代码: ```python import torch import torch.nn as nn # 定义两个向量 A = torch.tensor([1.0, 2.0, 3.0]) B = torch.tensor([4.0, 5.0, 6.0]) # 计算余弦相似度 cosine_similarity = nn.CosineSimilarity(dim=0, eps=1e-8) similarity = cosine_similarity(A, B) # 显示结果 print(f"余弦相似度为:{similarity.item()}") ``` #### 与皮尔逊相关系数的区别 余弦相似度与皮尔逊相关系数的主要区别在于: - **输入不同**:余弦相似度仅需要两个向量,而皮尔逊相关系数需要两个数据集。 - **输出范围不同**:余弦相似度的输出范围是 [-1, 1],而皮尔逊相关系数的输出范围是 [0, 1]。 - **概念不同**:余弦相似度衡量的是两个向量的相似性,而皮尔逊相关系数衡量的是两个变量的动态关系[^3]。 #### 调整余弦相似度 调整余弦相似度(Adjusted Cosine Similarity)是一种改进的余弦相似度方法,它通过减去向量的均值来调整向量之间的相似性计算。这种方法通常用于推荐系统中,以消除用户评分的偏差[^4]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值