余弦相似度和欧氏距离_欧氏距离和余弦相似度

余弦相似度和欧氏距离

Image for post
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个班级。 数学课,哲学课和心理学课。 您想根据您的教授在课堂上使用的单词来检查这些课程之间的相似性。 为了简单起见,让我们考虑以下两个词:“理论”和“和谐”。 然后,您可以创建一个像这样的表来记录每个类中这些单词的出现情况:

Image for post

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平面。

Word vectors in 2D plane

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

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

Image for post

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.

通用公式如下。

Image for post

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

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

Image for post

And d2

和d2

Image for post

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个小时。 由于您参加这两个课程中的更多课程,因此“理论”和“和谐”一词的出现将比心理学课程中的要大。 因此,更新后的表:

Image for post

And the updated 2D graph:

以及更新后的2D图形:

Image for post

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.

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

Image for post

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

通用公式如下

Image for post

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

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

Image for post

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

回想起那个

Image for post

and

Image for post

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.mzph.cn/news/389936.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

bzoj2152 聪聪可可

题目描述 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃、两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一般情况下石头剪刀布就好了,可是他们…

七、 面向对象(二)

匿名类对象 创建的类的对象是匿名的。当我们只需要一次调用类的对象时,我们就可以考虑使用匿名的方式创建类的对象。特点是创建的匿名类的对象只能够调用一次! package day007;//圆的面积 class circle {double radius;public double getArea() {// TODO…

机器学习 客户流失_通过机器学习预测流失

机器学习 客户流失介绍 (Introduction) This article is part of a project for Udacity “Become a Data Scientist Nano Degree”. The Jupyter Notebook with the code for this project can be downloaded from GitHub.本文是Udacity“成为数据科学家纳米学位”项目的一部分…

2044. 统计按位或能得到最大值的子集数目

2044. 统计按位或能得到最大值的子集数目 给你一个整数数组 nums ,请你找出 nums 子集 按位或 可能得到的 最大值 ,并返回按位或能得到最大值的 不同非空子集的数目 。 如果数组 a 可以由数组 b 删除一些元素(或不删除)得到&…

redis系列:分布式锁

1 介绍 这篇博文讲介绍如何一步步构建一个基于Redis的分布式锁。会从最原始的版本开始,然后根据问题进行调整,最后完成一个较为合理的分布式锁。 本篇文章会将分布式锁的实现分为两部分,一个是单机环境,另一个是集群环境下的Redis…

Qt中的坐标系统

转载:原野追逐 Qt使用统一的坐标系统来定位窗口部件的位置和大小。 以屏幕的左上角为原点即(0, 0)点,从左向右为x轴正向,从上向下为y轴正向,这整个屏幕的坐标系统就用来定位顶层窗口; 此外,窗口内部也有自己…

预测股票价格 模型_建立有马模型来预测股票价格

预测股票价格 模型前言 (Preface) If you are reading this, it’s most likely because you love to solve puzzles. I’m a very competitive person by nature. The Mt. Everest of puzzles, in my opinion, is trying to find excess returns through active trading in th…

Python 模块 timedatetime

time & datetime 模块 在平常的代码中,我们常常需要与时间打交道。在Python中,与时间处理有关的模块就包括:time,datetime,calendar(很少用,不讲),下面分别来介绍。 在开始之前,首先要说明几…

大数模板Java

import java.util.*; import java.math.BigInteger; public class Main{public static void main(String args[]){Scanner cinnew Scanner(System.in);BigInteger a,b;acin.nextBigInteger();bcin.nextBigInteger();System.out.println(a.add(b));//加法System.out.println(a.…

柠檬工会_工会经营者

柠檬工会Hey guys! This week we’ll be going over some ways to work with result sets in MySQL. These result sets are the outputs of your everyday queries, such as:大家好! 本周,我们将介绍一些在MySQL中处理结果集的方法。 这些结果集是您日常…

229. 求众数 II

229. 求众数 II 给定一个大小为 n 的整数数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素。 示例 1:输入:[3,2,3] 输出:[3]示例 2:输入:nums [1] 输出:[1]示例 3:输入:…

写给Java开发者看的JavaScript对象机制

帮助面向对象开发者理解关于JavaScript对象机制 本文是以一个熟悉OO语言的开发者视角,来解释JavaScript中的对象。 对于不了解JavaScript 语言,尤其是习惯了OO语言的开发者来说,由于语法上些许的相似会让人产生心理预期,JavaScrip…

Pythonic---------详细讲解

作者:半载流殇 链接:https://zhuanlan.zhihu.com/p/35219750 来源:知乎 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。Pythonic,简言之就是以Python这门语言独特的方式写出既简洁又优美的代码…

大数据ab 测试_在真实数据上进行AB测试应用程序

大数据ab 测试Hello Everyone!大家好! I am back with another article about Data Science. In this article, I will write about what is A-B testing and how to use it on real life data-set to compare two advertisement methods.我回来了另一篇有关数据科…

492. 构造矩形

492. 构造矩形 作为一位web开发者, 懂得怎样去规划一个页面的尺寸是很重要的。 现给定一个具体的矩形页面面积,你的任务是设计一个长度为 L 和宽度为 W 且满足以下要求的矩形的页面。要求: 你设计的矩形页面必须等于给定的目标面积。 宽度 …

node:爬虫爬取网页图片

前言 周末自己在家闲着没事,刷着微信,玩着手机,发现自己的微信头像该换了,就去网上找了一下头像,看着图片,自己就想着作为一个码农,可以把这些图片都爬取下来做成一个微信小程序,说干…

如何更好的掌握一个知识点_如何成为一个更好的讲故事的人3个关键点

如何更好的掌握一个知识点You’re launching a digital transformation initiative in the middle of the ongoing pandemic. You are pretty excited about this big-ticket investment, which has the potential to solve remote-work challenges that your organization fac…

centos 搭建jenkins+git+maven

gitmavenjenkins持续集成搭建发布人:[李源] 2017-12-08 04:33:37 一、搭建说明 系统:centos 6.5 jdk:1.8.0_144 jenkins:jenkins-2.93-1.1 git:git-2.9.0 maven:Maven 3.3.9 二、部署 2.1、jdk安装 1)下…

638. 大礼包

638. 大礼包 在 LeetCode 商店中, 有 n 件在售的物品。每件物品都有对应的价格。然而,也有一些大礼包,每个大礼包以优惠的价格捆绑销售一组物品。 给你一个整数数组 price 表示物品价格,其中 price[i] 是第 i 件物品的价格。另有…

记录一次spark连接mysql遇到的问题

在使用spark连接mysql的过程中报错了,错误如下 08:51:32.495 [main] ERROR - Error loading factory org.apache.calcite.jdbc.CalciteJdbc41Factory java.lang.NoClassDefFoundError: org/apache/calcite/linq4j/QueryProviderat java.lang.ClassLoader.defineCla…