zoukankan      html  css  js  c++  java
  • (原)欧氏距离与余弦距离

    转载请注明出处:

     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} ight|}_{2}}=sqrt{sumlimits_{i=1}^{n}{{{({{a}_{i}}-{{b}_{i}})}^{2}}}}=sqrt{sumlimits_{i=1}^{n}{(a_{i}^{2}-2centerdot {{a}_{i}}centerdot {{b}_{i}}+b_{i}^{2})}}=sqrt{sumlimits_{i=1}^{n}{a_{i}^{2}}+sumlimits_{i=1}^{n}{b_{i}^{2}}-2centerdot sumlimits_{i=1}^{n}{{{a}_{i}}centerdot {{b}_{i}}}}$

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

    $Cos\_sim ext{=}frac{mathbf{A}centerdot {{mathbf{B}}^{T}}}{{{left| mathbf{A} ight|}_{2}}centerdot {{left| mathbf{B} ight|}_{2}}}=frac{sumlimits_{i=1}^{n}{{{a}_{i}}centerdot {{b}_{i}}}}{sqrt{sumlimits_{i=1}^{n}{a_{i}^{2}}}centerdot sqrt{sumlimits_{i=1}^{n}{b_{i}^{2}}}}$

    余弦距离为:

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

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

    $Euc\_dist ext{=}sqrt{1+1-2centerdot sumlimits_{i=1}^{n}{{{a}_{i}}centerdot {{b}_{i}}}} ext{=}sqrt{2centerdot 1-sumlimits_{i=1}^{n}{{{a}_{i}}centerdot {{b}_{i}}}}$

    $Cos\_dis ext{=}1 ext{-}sumlimits_{i=1}^{n}{{{a}_{i}}centerdot {{b}_{i}}}$

    进而:

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

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

    另外:

    欧式距离越小(越接近0),两向量越相似。

    余弦距离越小(越接近0),两向量越相似。

    余弦相似度越大(越接近1),两向量越相似。

  • 相关阅读:
    vi常用操作
    Python练习题
    Jmeter也能IP欺骗!
    mysql主从配置
    性能测试之mysql监控、优化
    Git 命令
    Chrome——F12 谷歌开发者工具详解
    Appscan
    微信群发红包抢红包设计测试用例
    MySQL基础篇(1)SQL基础
  • 原文地址:https://www.cnblogs.com/darkknightzh/p/12013741.html
Copyright © 2011-2022 走看看