Cosine similarity

Feature Engineering for NLP in Python

Rounak Banik

Data Scientist

Cosine similarity

1 ภาพจาก techninpink.com
Feature Engineering for NLP in Python

ผลคูณดอต

กำหนดเวกเตอร์สองตัว

$$V = (v_1, v_2, \cdots, v_n), W = (w_1, w_2, \cdots, w_n)$$

ผลคูณดอตของ V และ W คือ

$$V \cdot W = (v_1 \times w_1) + (v_2 \times w_2) + \cdots + (v_n \times w_n) $$

ตัวอย่าง:

$$A = (4, 7, 1) \; , \; B = (5, 2, 3)$$

$$A \cdot B = (4 \times 5) + (7 \times 2) + \cdots (1 \times 3)$$

$$= 20 + 14 + 3 = 37 \color{white}{A \cdot B d}$$

$$$$

Feature Engineering for NLP in Python

ขนาดของเวกเตอร์

สำหรับเวกเตอร์ใดก็ตาม

$$V = (v_1, v_2, \cdots, v_n)$$

ขนาดของเวกเตอร์นิยามไว้ว่า

$$||\mathbf{V}|| = \sqrt{(v_1)^{2} + (v_2)^{2} + ... + (v_n)^{2}} $$

ตัวอย่าง:

$$A = (4, 7, 1) \; , \; B = (5, 2, 3)$$

$$||\mathbf{A}|| = \sqrt{(4)^{2} + (7)^{2} + (1)^{2}} $$

$$ \color{white}{filler} = \sqrt{16 + 49 + 1} = \sqrt{66}$$

$$

Feature Engineering for NLP in Python

คะแนน cosine

มุมระหว่างเวกเตอร์ A และ B

$$A: (4, 7, 1)$$

$$B: (5, 2, 3)$$

คะแนน cosine

$$cos(A,B) = \frac{A \cdot B}{|A| \cdot |B|}$$

$$\color{white}{fillers lorem}= \frac{37}{\sqrt{66} \times \sqrt{38}}$$

$$\color{white}{fillers l}= 0.7388$$

Feature Engineering for NLP in Python

คะแนน Cosine: สิ่งที่ควรจำ

  • ค่าอยู่ระหว่าง -1 ถึง 1
  • ใน NLP ค่าอยู่ระหว่าง 0 ถึง 1
  • ไม่ขึ้นกับความยาวของเอกสาร
Feature Engineering for NLP in Python

การใช้งานด้วย scikit-learn

# Import the cosine_similarity
from sklearn.metrics.pairwise import cosine_similarity
# Define two 3-dimensional vectors A and B
A = (4,7,1)
B = (5,2,3)

# Compute the cosine score of A and B
score = cosine_similarity([A], [B])

# Print the cosine score
print(score)
array([[ 0.73881883]])
Feature Engineering for NLP in Python

มาฝึกกันเถอะ!

Feature Engineering for NLP in Python

Preparing Video For Download...