当前位置:网站首页>Distance measure - cosine distance
Distance measure - cosine distance
2022-06-23 04:40:00 【Fanyi】
Python Learning Series : Catalog

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 .

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(θ)=∣a∣∣b∣a⋅b
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(θ)=∣a∣∣b∣a⋅b=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(θ)=∣a∣∣b∣a⋅b=∑k=1nx1k2∑k=1nx2k2∑k=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.

边栏推荐
- 2020:VL-BERT: Pre-training of generic visual-linguistic representation
- TS进阶之infer
- Pta:6-30 time addition
- Prince language on insect date class
- 【二叉树】二叉树的完全性检验
- PTA: Simulation Implementation of 7-87 set (class template)
- Analysis on the current situation of the Internet of things in 2022
- 8位全加器原理
- Svg+js smart home monitoring grid layout
- Volatile and threads
猜你喜欢

② cocoapods原理及 PodSpec 文件上传操作

What is metadata

It supports running in kubernetes, adds multiple connectors, and seatunnel version 2.1.2 is officially released!

语料库数据处理个案实例(分词和分句、词频统计、排序)

Latest programming language rankings

If you want to understand PostgreSQL, you must first brush the architecture

应急响应-hw复习

Sessions and Daemons

Xiaojinwei, chairman of Chenglian Technology: implement the national strategy of data economy and lead the development of new consumption in the digital era!

Online text filter less than specified length tool
随机推荐
[multimode] unimo
PTA:7-67 友元很简单2016final
[learn FPGA programming from scratch -40]: Advanced - Design - competition and risk
Software development in 2022: five realities CIOs should know
Pta:6-71 clock simulation
Does the network disk also roll inside?
Redis启动有问题
云函数实现模糊搜索功能
Online JSON to CSharp (c) class tool
Particle animation background login page particles js
自动化测试常见的面试题
How MySQL deletes a row of data in a table
Banner 标语 旗帜
3D数学基础[十六] 匀加速直线运动的公式
What are the characteristics of SRM supplier management system developed by manufacturing enterprises
12 excellent practices of wireless network security
[deep learning] deep learning reasoning framework tensorrt MNN openvino onnxruntime
PTA:7-69 数据的间距问题
OpenJudge NOI 1.13 50:数根
PTA: spacing of 7-69 data