首页 > 其他 > 详细

(原)欧氏距离与余弦距离

时间:2019-12-09 23:33:14      阅读:190      评论:0      收藏:0      [点我收藏+]

转载请注明出处:

 https://www.cnblogs.com/darkknightzh/p/12013741.html

网上参考资料一大堆,自己也总结一下吧。

 

两向量$\mathbf{A}=[{{a}_{1}},\cdots ,{{a}_{n}}]$,$\mathbf{B}=[{{b}_{1}},\cdots ,{{b}_{n}}]$,这两个向量之间的欧式距离为:

$Euc\_dist={{\left\| \mathbf{A}-\mathbf{B} \right\|}_{2}}=\sqrt{\sum\limits_{i=1}^{n}{{{({{a}_{i}}-{{b}_{i}})}^{2}}}}=\sqrt{\sum\limits_{i=1}^{n}{(a_{i}^{2}-2\centerdot {{a}_{i}}\centerdot {{b}_{i}}+b_{i}^{2})}}=\sqrt{\sum\limits_{i=1}^{n}{a_{i}^{2}}+\sum\limits_{i=1}^{n}{b_{i}^{2}}-2\centerdot \sum\limits_{i=1}^{n}{{{a}_{i}}\centerdot {{b}_{i}}}}$

这两个向量之间的余弦相似度Cos_sim为:

$Cos\_sim\text{=}\frac{\mathbf{A}\centerdot {{\mathbf{B}}^{T}}}{{{\left\| \mathbf{A} \right\|}_{2}}\centerdot {{\left\| \mathbf{B} \right\|}_{2}}}=\frac{\sum\limits_{i=1}^{n}{{{a}_{i}}\centerdot {{b}_{i}}}}{\sqrt{\sum\limits_{i=1}^{n}{a_{i}^{2}}}\centerdot \sqrt{\sum\limits_{i=1}^{n}{b_{i}^{2}}}}$

余弦距离为:

$Cos\_dis\text{=}1-Cos\_sim\text{=}1\text{-}\frac{\sum\limits_{i=1}^{n}{{{a}_{i}}\centerdot {{b}_{i}}}}{\sqrt{\sum\limits_{i=1}^{n}{a_{i}^{2}}}\centerdot \sqrt{\sum\limits_{i=1}^{n}{b_{i}^{2}}}}$

 

若这两个向量均已归一化,即${{\left\| \mathbf{A} \right\|}_{2}}=\sqrt{\sum\limits_{i=1}^{n}{a_{i}^{2}}}\text{=}1$,${{\left\| \mathbf{B} \right\|}_{2}}=\sqrt{\sum\limits_{i=1}^{n}{b_{i}^{2}}}\text{=}1$,则:

$Euc\_dist\text{=}\sqrt{1+1-2\centerdot \sum\limits_{i=1}^{n}{{{a}_{i}}\centerdot {{b}_{i}}}}\text{=}\sqrt{2\centerdot 1-\sum\limits_{i=1}^{n}{{{a}_{i}}\centerdot {{b}_{i}}}}$

$Cos\_dis\text{=}1\text{-}\sum\limits_{i=1}^{n}{{{a}_{i}}\centerdot {{b}_{i}}}$

进而:

$Euc\_dis{{t}^{2}}\text{=}2\centerdot Cos\_dis\text{=}2\centerdot (1-Cos\_sim)$

$Cos\_sim=1-\frac{1}{2}Euc\_dis{{t}^{2}}$

(原)欧氏距离与余弦距离

原文:https://www.cnblogs.com/darkknightzh/p/12013741.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!