当前位置:网站首页>【机器学习Q&A】余弦相似度、余弦距离、欧式距离以及机器学习中距离的含义
【机器学习Q&A】余弦相似度、余弦距离、欧式距离以及机器学习中距离的含义
2022-06-30 01:24:00 【镰刀韭菜】
余弦相似度、余弦距离、欧式距离以及机器学习中距离的含义
机器学习问题中,通常将特征表示为向量的形式,所以在分析两个特征向量之间的相似性时,常使用
余弦相似度来表示。 余弦相似度的取值范围为[ -1, 1 ],相同的两个向量之间的相似度为1,将1减去余弦相似度即为
余弦距离。因此,余弦距离的取值范围为[0, 2],相同的两个向量余弦距离为0。 问题1:为什么在一些场景中要使用余弦相似度而不是欧氏距离?
对于两个向量A和B, 其余弦相似度定义为 c o s ( A , B ) = A ⋅ B ∣ ∣ A ∣ ∣ 2 ∣ ∣ B ∣ ∣ 2 cos(A, B)=\frac{A\cdot B}{||A||_2||B||_2} cos(A,B)=∣∣A∣∣2∣∣B∣∣2A⋅B,即两个向量夹角的余弦,关注的是向量之间的角度关系,并不关心它们的绝对大小,其取值范围是[-1, 1]。
当一对文本相似度的长度差距很大,但内容相近时,如果使用词频或者词向量作为特征,它们在特征空间中的欧式距离通常很大;而如果使用余弦相似度的话,它们之间的夹角可能很小,因而相似度高。
此外,在文本、图像、视频等领域,研究的对象的特征维度往往很高,余弦相似度在高维情况下依然保持“相同时为1,正交时为0,相反时为-1”的性质,而欧氏距离的数值则受维度的影响,范围不固定,并且含义比较模糊。
在一些场景中,如Word2Vec中,其向量的模长是经过归一化的,此时欧式距离与余弦距离有着单调的关系,即:
∣ ∣ A − B ∣ ∣ 2 = 2 ( 1 − c o s ( A , B ) ) ||A-B||_2=\sqrt{2(1-cos(A,B))} ∣∣A−B∣∣2=2(1−cos(A,B))
其中, ∣ ∣ A − B ∣ ∣ 2 ||A-B||_2 ∣∣A−B∣∣2表示欧式距离, c o s ( A , B ) cos(A,B) cos(A,B)表示余弦相似度, ( 1 − c o s ( A , B ) ) (1-cos(A,B)) (1−cos(A,B))表示余弦距离。在此场景下,选择距离最小(相似度最大)的近邻,那么使用余弦相似度和欧式距离的结果是相同的。
总体来说,欧式距离体现数值上的绝对差异,而余弦距离体现方向上的相对差异。
问题2:余弦距离是否是一个严格定义的距离?
注意:余弦距离不是严格定义上的距离!
距离的定义:在一个集合中,如果每一对元素均可唯一确定一个实数,使得三条距离公理(正定性、对称性、三角不等式)成立,则该实数可称为这对元素之间的距离。
(1)正定性
根据余弦距离的定义,有
d i s t ( A , B ) = 1 − c o s θ = ∣ ∣ A ∣ ∣ 2 ∣ ∣ B ∣ ∣ 2 − A B ∣ ∣ A ∣ ∣ 2 ∣ ∣ B ∣ ∣ 2 dist(A, B)=1-cos \theta = \frac{||A||_2||B||_2-AB}{||A||_2||B||_2} dist(A,B)=1−cosθ=∣∣A∣∣2∣∣B∣∣2∣∣A∣∣2∣∣B∣∣2−AB
考虑到 ∣ ∣ A ∣ ∣ 2 ∣ ∣ B ∣ ∣ 2 − A B ≥ 0 ||A||_2||B||_2-AB\ge 0 ∣∣A∣∣2∣∣B∣∣2−AB≥0,因此有 d i s t ( A , B ) ≥ 0 dist(A, B)\ge 0 dist(A,B)≥0恒成立。
(2)对称性
根据余弦距离的定义,有
d i s t ( A , B ) = ∣ ∣ A ∣ ∣ 2 ∣ ∣ B ∣ ∣ 2 − A B ∣ ∣ A ∣ ∣ 2 ∣ ∣ B ∣ ∣ 2 = ∣ ∣ B ∣ ∣ 2 ∣ ∣ A ∣ ∣ 2 − A B ∣ ∣ B ∣ ∣ 2 ∣ ∣ A ∣ ∣ 2 dist(A, B)=\frac{||A||_2||B||_2-AB}{||A||_2||B||_2}=\frac{||B||_2||A||_2-AB}{||B||_2||A||_2} dist(A,B)=∣∣A∣∣2∣∣B∣∣2∣∣A∣∣2∣∣B∣∣2−AB=∣∣B∣∣2∣∣A∣∣2∣∣B∣∣2∣∣A∣∣2−AB
满足对称性
(3)三角不等式
该性质并不成立,反例。给定A=(1,0),B=(1,1),C=(0,1),则有 d i s t ( A , B ) = 1 − 2 2 dist(A,B)=1-\frac{\sqrt{2}}{2} dist(A,B)=1−22, d i s t ( B , C ) = 1 − 2 2 dist(B,C)=1-\frac{\sqrt{2}}{2} dist(B,C)=1−22, d i s t ( A , C ) = 1 dist(A,C)=1 dist(A,C)=1
因此有 d i s t ( A , B ) + d i s t ( B , C ) = 2 − 2 < 1 = d i s t ( A , C ) dist(A,B)+dist(B,C)=2-\sqrt{2}<1=dist(A,C) dist(A,B)+dist(B,C)=2−2<1=dist(A,C)
余弦距离满足正定性和对称性,不满足三角不等式。
另外,我们知道单位圆上欧式距离和余弦距离满足
∣ ∣ A − B ∣ ∣ = 2 ( 1 − c o s ( A , B ) ) = 2 d i s t ( A , B ) ||A-B||=\sqrt{2(1-cos(A,B))}=\sqrt{2dist(A,B)} ∣∣A−B∣∣=2(1−cos(A,B))=2dist(A,B)
即有如下关系:
d i s t ( A , B ) = 1 2 ∣ ∣ A − B ∣ ∣ 2 dist(A, B)=\frac{1}{2}||A-B||^2 dist(A,B)=21∣∣A−B∣∣2
显然在单位圆上,余弦距离和欧式距离的范围都是[0,2],已知欧式距离是一个合法的距离,而余弦距离与欧式距离有二次关系,自然不满足三角不等式。
在机器学习领域,被俗称为距离,但却不满足三条距离公理的不仅仅有余弦距离,还有KL距离(Kullback-Leibler Divergence),也叫做相对熵,它常用于计算两个分布之间的差异,但不满足对称性和三角不等式。
在机器学习领域中,A/B测试是验证模型最终效果的主要手段。
问题1: 在对模型进行充分的离线评估后,为什么还要进行在线A/B测试?
(1)离线评估无法消除模型过拟合的影响;因此,得出的离线评估效果无法完全替代线上评估结果;
(2)离线评估无法完全还原线上的工程环境。一般,离线评估往往不会考虑线上环境的延迟、数据丢失、标签数据缺失等情况。
(3)线上系统的某些商业指标在离线评估中无法计算。比如,上线了新的推荐算法,离线往往关心ROC曲线、P-R曲线等;而线上评估可以全面了解该推荐算法带来的用户点击率、留存时长、PV访问量等的变化。
问题2:如何进行线上A/B测试?
(1) 进行A/B测试的主要手段是进行用户分桶,即将用户分成实验组和对照组,对实验组用户施以新模型,对对照组用户施以旧模型;
(2)在分桶的过程中,要注意样本的独立性和采样方式的无偏性,确保同一个用户每次只能分到同一个桶中;
问题3:如何划分实验组和对照组?
某公司的算法工程师对美国用户,研发了一套全新的视频推荐模型A;而正在使用的针对全体用户的推荐模型是B。
正确的做法: 将所有的 美国用户根据user_id个位数奇偶划分为实验组和对照组,分别施以模型A和B,才能够验证模型A的效果;
参考资料
[1] 《百面机器学习》第二章:模型评估
[2] 熵 Entropy – 香农熵、相对熵、交叉熵、条件熵
边栏推荐
- Ansible ad-hoc 临时命令
- I, 33 years old, ByteDance test development, unveiled the real income of Beijing "test post"
- R language linear regression model fitting diagnosis outliers analysis of domestic gas consumption and calorie examples with self-test questions
- The first unlucky person watching eth 2.0
- Quick Pow: 如何快速求幂
- HC32M0+ GPIO
- The listing of Symantec electronic sprint technology innovation board: it plans to raise 623million yuan, with a total of 64 patent applications
- Ctfshow competition original title 680-695
- cookie加密11
- Ansible ad-hoc temporary command
猜你喜欢

R语言线性回归模型拟合诊断异常值分析家庭燃气消耗量和卡路里实例带自测题
![[Thesis Writing] English thesis writing guide](/img/59/88d34814a88a2da19ed6a236825649.png)
[Thesis Writing] English thesis writing guide

Sfdp 超级表单开发平台 V6.0.4 正式发布

Seata et les trois plateformes travaillent main dans la main pour programmer Summer, un million de bonus vous attend

TP-LINK configure WiFi authentication method for wireless Internet SMS

STC89C52 single chip microcomputer simple calculator design and code demonstration

Understand the module function of MES management system

The 8th "Internet +" competition - cloud native track invites you to challenge

Online text digit recognition list summation tool

Seata and the three platforms are working together in the summer of programming. Millions of bonuses are waiting for you
随机推荐
Cub school learning: manual query and ADC in-depth use
C language selection, loop overview
Cookie加密13
Seata 與三大平臺攜手編程之夏,百萬獎金等你來拿
[deep learning compilation] operator compilation IR conversion
Using tsne to visualize the similarity of different sentences
Visual Studio 2017 无法打开包括文件: “QOpenGLFunctions_3_3_Core”: No such file or directory
MySQL function
MySql函数
cookie加密11
Transaction summary on June 25, 2022
Questions about database: database attachment
Text classification using huggingface
阅读,是最廉价的高贵
快手伸手“供给侧”,找到直播电商的“源头活水”?
Cantilever beam calculation [matlab code]
Stimulus reports reporting tool, stimulus creates and builds reports
Varnish 基础概览4
Seata and the three platforms are working together in the summer of programming. Millions of bonuses are waiting for you
Three text to speech artifacts, each of which is very practical