余弦相似度和歐氏距離_歐氏距離和余弦相似度

余弦相似度和歐氏距離

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.pswp.cn/news/389936.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/389936.shtml
英文地址,請注明出處:http://en.pswp.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…