当前位置:网站首页>Distance measure - cosine distance

Distance measure - cosine distance

2022-06-23 04:40:00 Fanyi

Python Learning Series Catalog

 Insert picture description here

One 、 summary

Trigonometric functions , I believe everyone has learned in junior high school , And what we're talking about here Cosine distance (Cosine Distance) The calculation formula is similar to that learned in high school .

In Geometry , The cosine of the included angle can be used to measure two directions ( vector ) The difference of ; So it can be extended to machine learning , To measure the difference between sample vectors .

therefore , Our formula has to be changed slightly , So that it can be expressed as a vector .

 Insert picture description here

Two 、 Calculation formula

① Cosine distance in two-dimensional plane

hypothesis Two dimensional plane There are two vectors inside : A ( x 1 , y 1 ) A(x_{1},y_{1}) A(x1,y1) And B ( x 2 , y 2 ) B(x_{2},y_{2}) B(x2,y2)

Then the A A A B B B The cosine distance formula of two vectors is :

c o s ( θ ) = a ⋅ b ∣ a ∣ ∣ b ∣ cos(\theta)=\frac{a\cdot b}{|a| |b|} cos(θ)=abab

c o s ( θ ) = a ⋅ b ∣ a ∣ ∣ b ∣ = x 1 x 2 + y 1 y 2 x 1 2 + y 1 2 x 2 2 + y 2 2 \begin{aligned} cos(\theta)&=\frac{a\cdot b}{|a| |b|}\\ &=\frac{x_{1}x_{2}+y_{1}y_{2}}{\sqrt{x_{1}^2+y_{1}^2}\sqrt{x_{2}^2+y_{2}^2}} \end{aligned} cos(θ)=abab=x12+y12x22+y22x1x2+y1y2

② n Cosine distance in dimensional space

Generalized to n Two vectors of dimensional space A ( x 11 , x 12 , . . . , x 1 n ) A(x_{11},x_{12},...,x_{1n}) A(x11,x12,...,x1n) And B ( x 21 , x 22 , . . . , x 2 n ) B(x_{21},x_{22},...,x_{2n}) B(x21,x22,...,x2n), Then the cosine distance is :

c o s ( θ ) = a ⋅ b ∣ a ∣ ∣ b ∣ = ∑ k = 1 n x 1 k x 2 k ∑ k = 1 n x 1 k 2 ∑ k = 1 n x 2 k 2 \begin{aligned} cos(\theta)&=\frac{a\cdot b}{|a| |b|}\\ &=\frac{\sum_{k=1}^n x_{1k} x_{2k}}{\sqrt{\sum_{k=1}^nx_{1k}^2}\sqrt{\sum_{k=1}^nx_{2k}^2}} \end{aligned} cos(θ)=abab=k=1nx1k2k=1nx2k2k=1nx1kx2k

③ Be careful

  • The value range of cosine distance is [ − 1 , 1 ] [-1,1] [1,1].
  • The larger the cosine, the smaller the angle between the two vectors , The smaller the cosine, the larger the angle between the two vectors .
  • When the directions of two vectors coincide, the cosine takes the maximum 1 1 1, When two vectors are in opposite directions, the cosine is the minimum − 1 -1 1.

 Insert picture description here

原网站

版权声明
本文为[Fanyi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206222333267868.html