余弦相似度和欧氏距离

Photo by Markus Winkler on Unsplash
Markus Winkler在Unsplash上拍摄的照片

This is a quick and straight to the point introduction to Euclidean distance and cosine similarity with a focus on NLP.

这是对欧氏距离和余弦相似度的快速而直接的介绍,重点是NLP。

欧氏距离 (Euclidean Distance)

The Euclidean distance metric allows you to identify how far two points or two vectors are apart from each other.

欧几里德距离度量标准可让您确定两个点或两个向量彼此相距多远。

Now suppose you are a high school student and you have three classes. A math class, a philosophy class, and a psychology class. You want to check the similarity between these classes based on the words your professors use in class. For the sake of simplicity, let’s consider these two words: “theory” and “harmony”. You could then create a table like this to record the occurrence of these words in each class:

现在假设您是一名高中生,您有3个班级。 数学课,哲学课和心理学课。 您想根据您的教授在课堂上使用的单词来检查这些课程之间的相似性。 为了简单起见,让我们考虑以下两个词:“理论”和“和谐”。 然后,您可以创建一个像这样的表来记录每个类中这些单词的出现情况:

In this table, the word “theory” is repeated 60 times in math class, 20 times in philosophy class, and 25 times in psychology class whereas the word harmony is repeated 10, 40, and 70 times in math, philosophy, and psychology classes respectively. Let’s translate this data into a 2D plane.

在此表中,“理论”一词在数学课中重复了60次,在哲学课中重复了20次,在心理学课中重复了25次,而在数学,哲学和心理学课中,“和谐”一词重复了10、40和70次分别。 让我们将此数据转换为2D平面。

The Euclidean distance is simply the distance between the points. In the graph below.

欧几里得距离就是点之间的距离。 在下图中。

You can see clearly that d1 which is the distance between psychology and philosophy is smaller than d2 which is the distance between philosophy and math. But how do you calculate d1 and d2?

您可以清楚地看到,心理学与哲学之间的距离d1小于哲学与数学之间的距离d2。 但是,如何计算d1和d2?

The generic formula is the following.

通用公式如下。

In our case, for d1, d(v, w) = d(philosophy, psychology)`, which is:

在我们的情况下,对于d1, d(v, w) = d(philosophy, psychology) `,即:

And d2

和d2

As expected d2 > d1.

如预期的那样,d2> d1。

How to do this in python?

如何在python中做到这一点?

import numpy as np# define the vectorsmath = np.array([60, 10])philosophy = np.array([20, 40])psychology = np.array([25, 70])# calculate d1d1 = np.linalg.norm(philosophy - psychology)# calculate d2d2 = np.linalg.norm(philosophy - math)

余弦相似度 (Cosine Similarity)

Suppose you only have 2 hours of psychology class per week and 5 hours of both math class and philosophy class. Because you attend more of these two classes, the occurrence of the words “theory” and “harmony” will be greater than for the psychology class. Thus the updated table:

假设您每周只有2个小时的心理学课,而数学课和哲学课则只有5个小时。 由于您参加这两个课程中的更多课程,因此“理论”和“和谐”一词的出现将比心理学课程中的要大。 因此,更新后的表:

And the updated 2D graph:

以及更新后的2D图形:

Using the formula we’ve given earlier for Euclidean distance, we will find that, in this case, d1 is greater than d2. But we know psychology is closer to philosophy than it is to math. The frequency of the courses, trick the Euclidean distance metric. Cosine similarity is here to solve this problem.

使用我们先前给出的欧几里得距离公式,我们会发现,在这种情况下,d1大于d2。 但是我们知道心理学比数学更接近于哲学。 课程的频率欺骗欧几里德距离度量标准。 余弦相似度在这里解决了这个问题。

Instead of calculating the straight line distance between the points, cosine similarity cares about the angle between the vectors.

余弦相似度关心的是矢量之间的角度,而不是计算点之间的直线距离。

Zooming in on the graph, we can see that the angle α, is smaller than the angle β. That’s all cosine similarity wants to know. In other words, the smaller the angle, the closer the vectors are to each other.

放大该图,我们可以看到角度α小于角度β。 这就是所有余弦相似度想要知道的。 换句话说,角度越小,向量彼此越接近。

The generic formula goes as follows

通用公式如下

β is the angle between the vectors philosophy (represented by v) and math (represented by w).

β是向量原理(用v表示)和数学(用w表示)之间的夹角。

Whereas cos(alpha) = 0.99 which is higher than cos(beta) meaning philosophy is closer to psychology than it is to math.

cos(alpha) = 0.99 (高于cos(beta)意味着哲学比数学更接近心理学。

Recall that

回想起那个

and

This implies that the smaller the angle, the greater your cosine similarity will be and the greater your cosine similarity, the more similar your vectors are.

这意味着角度越小,您的余弦相似度就越大,并且您的余弦相似度越大,向量就越相似。

Python implementation

Python实现

import numpy as npmath = np.array([80, 45])philosophy = np.array([50, 60])psychology = np.array([15, 20])cos_beta = np.dot(philosophy, math) / (np.linalg.norm(philosophy) * np.linalg.norm(math))print(cos_beta)

带走 (Takeaway)

I bet you should know by now how Euclidean distance and cosine similarity works. The former considers the straight line distance between two points whereas the latter cares about the angle between the two vectors in question.

我敢打赌,您现在应该知道欧几里得距离和余弦相似度是如何工作的。 前者考虑了两个点之间的直线距离,而后者则考虑了所讨论的两个向量之间的角度。

Euclidean distance is more straightforward and is guaranteed to work whenever your features distribution is balanced. But most of the time, we deal with unbalanced data. In such cases, it’s better to use cosine similarity.

欧几里得距离更简单明了,并且可以保证只要要素分布平衡就可以使用。 但是大多数时候,我们处理不平衡的数据。 在这种情况下,最好使用余弦相似度。

翻译自: https://medium.com/@josmyfaure/euclidean-distance-and-cosine-similarity-which-one-to-use-and-when-28c97a18fe68

余弦相似度和欧氏距离


http://www.taodudu.cc/news/show-995368.html

相关文章:

  • 机器学习 客户流失_通过机器学习预测流失
  • 预测股票价格 模型_建立有马模型来预测股票价格
  • 柠檬工会_工会经营者
  • 大数据ab 测试_在真实数据上进行AB测试应用程序
  • 如何更好的掌握一个知识点_如何成为一个更好的讲故事的人3个关键点
  • 什么事数据科学_如果您想进入数据科学,则必须知道的7件事
  • 季节性时间序列数据分析_如何指导时间序列数据的探索性数据分析
  • 美团骑手检测出虚假定位_在虚假信息活动中检测协调
  • 回归分析假设_回归分析假设的最简单指南
  • 为什么随机性是信息
  • 大数据相关从业_如何在组织中以数据从业者的身份闪耀
  • 汉诺塔递归算法进阶_进阶python 1递归
  • 普里姆从不同顶点出发_来自三个不同聚类分析的三个不同教训数据科学的顶点...
  • 荷兰牛栏 荷兰售价_荷兰的公路货运是如何发展的
  • 如何成为数据科学家_成为数据科学家需要了解什么
  • 个人项目api接口_5个免费有趣的API,可用于学习个人项目等
  • 如何评价强gis与弱gis_什么是gis的简化解释
  • 自我接纳_接纳预测因子
  • python中knn_如何在python中从头开始构建knn
  • tb计算机存储单位_如何节省数TB的云存储
  • 数据可视化机器学习工具在线_为什么您不能跳过学习数据可视化
  • python中nlp的库_用于nlp的python中的网站数据清理
  • 怎么看另一个电脑端口是否通_谁一个人睡觉另一个看看夫妻的睡眠习惯
  • tableau 自定义省份_在Tableau中使用自定义图像映射
  • 熊猫烧香分析报告_熊猫分析进行最佳探索性数据分析
  • 白裤子变粉裤子怎么办_使用裤子构建构建数据科学的monorepo
  • 青年报告_了解青年的情绪
  • map(平均平均精度_客户的平均平均精度
  • 鲜活数据数据可视化指南_数据可视化实用指南
  • 图像特征 可视化_使用卫星图像可视化建筑区域

余弦相似度和欧氏距离_欧氏距离和余弦相似度相关推荐

  1. 相似度计算——欧氏距离、汉明距离、余弦相似度

    计算图像间的相似性可以使用欧氏距离.余弦相似度/作为度量,前者强调点的思想,后者注重线的思想. 欧氏距离 欧式距离/Euclidean Distance即n维空间中两个点之间的实际距离.已知两个点A= ...

  2. python余弦相似度_推荐系统01--余弦相似度

    今天,我们来聊聊协同过滤中的相似度计算方法有哪些. 相似度的本质 推荐系统中,推荐算法分为两个门派,一个是机器学习派,另一个就是相似度门派.机器学习派是后起之秀,而相似度派则是泰山北斗,以致撑起来推荐 ...

  3. 求两个点的欧氏距离_数据智能系列(五)| 距离的秘密

    回忆下前四篇的内容,有没有发现一个共同点?没错,无论是用于统计聚类的K-means算法,还是拟合直线的最小二乘法,或是简单高效的KNN分类算法都提到了距离的计算.可见"距离"在机器 ...

  4. 相似度计算(欧式距离和余弦距离)

    相似度:即计算个体间的相似程度,相似度度量的值越小,说明个体间相似度越小,相似度的值越大说明个体差异越大. 对于多个不同的文本或者短文本对话消息要来计算他们之间的相似度如何,一个好的做法就是将这些文本 ...

  5. python实现词语相似度计算分析_相似度计算的方法及Python实现

    现实生活中,我们经常提到距离这个词,本文谈的相似度就是基于距离定义的,当两个向量之间的距离特别小时,就说这俩个向量相似度高,反之相似度不高.所以,衡量相似度的指标就是距离度量. 经常使用的相似度计算公 ...

  6. matlab 多通道余弦调制镜像滤波器组_滤波器基础:抗混叠

    电子万花筒平台核心服务  中国最活跃的射频微波天线雷达微信技术群 电子猎头:帮助电子工程师实现人生价值!  电子元器件:价格比您现有供应商最少降低10% 射频微波天线新产品新技术发布平台:让更多优秀的 ...

  7. python余弦相似度文本分类_Jaccard与cosine文本相似度的异同

    工作过程中,常常其他业务的同学问到:某两个词的相似度是多少?某两个句子的相似度是多少?某两个文档之间的相似度是多少?在本文中,我们讨论一下jaccard与cosine在文本相似度上的差异,以及他们适用 ...

  8. idea删除后能还原吗_热水管能过100度热水吗

    热水管是否能过100度热水需根据水管的材质来决定,如果家中使用的是联塑水管,此类材质的水管具有较好的耐候性,软化点在131.5℃左右,安装后能过100度热水.PPR管的耐候性比较差,可承受的温度为70 ...

  9. fastreport 旋转90度_水冷必不可少之90度弯头

    点击蓝字关注我们 设计于Bitspower而言 是生活,专注当下,发现需求迎合需求 是呼吸,将持续在道上,创新永不止步 BP所研发的水冷接头一直以来是众多玩家认可的逸品,兼具质感与特色,赋予水冷无限可 ...

最新文章

  1. [转]在Eclipse中使用JUnit4进行单元测试(中级篇)
  2. ajax异步上传到又拍云的实例教程
  3. 操作系统-命令解释程序(实验一)
  4. oracle对象之存储函数
  5. jsp设置背景图片并使得图片扩大到整个屏幕
  6. 【CodeForces - 706D】Vasiliy's Multiset(01字典树)
  7. [转]C++中sizeof(struct)怎么计算?
  8. C ++中的std :: binary_search()
  9. 关于一些java命令作用
  10. AdminLTE与php,如何使用Vue整合AdminLTE模板
  11. Ubuntu20.04配置NTP服务器
  12. 7个用于开源网络情报渗透测试工作的热门OSINT工具
  13. 算法入门——数组,链表,选择排序
  14. 媒体播控系统、信息发布系统 使用说明
  15. 测试管理工具之禅道的使用
  16. 服务器安装jkd1.8运行jar以及一系列的操作
  17. 【数据库技术】2PL(两阶段锁)下的死锁与饥饿处理手段
  18. 如何使用MongoDB Go驱动程序在MongoDB中使用Go
  19. 如何成功软件开发外包
  20. 【渗透测试】ew代理三层靶机穿透+proxifier配置规则

热门文章

  1. 解决读写分离过期读的几个方案
  2. springboot集成redis使用redis作为session报错ClassNotFoundException类RememberMeServices
  3. Java概述、环境变量、注释、关键字、标识符、常量
  4. 枚举转中文,通过反射方法与描述的方式获取
  5. 一个数据仓库转型者眼中的数据挖掘
  6. 第三周作业(一)单元测试
  7. 传奇服务端各文件用途说明
  8. [编程题] 按照左右半区的方式重新组合单链表
  9. How to remove replication in SyteLine V2
  10. C++类分号(;)问题