Cosine similarity

Python में NLP के लिए Feature Engineering

Rounak Banik

Data Scientist

Cosine similarity

1 Image courtesy techninpink.com
Python में NLP के लिए Feature Engineering

डॉट प्रोडक्ट

दो वेक्टर मानिए,

$$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}$$

$$$$

Python में NLP के लिए Feature Engineering

वेक्टर का मैग्निट्यूड

किसी भी वेक्टर के लिए,

$$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}$$

$$

Python में NLP के लिए Feature Engineering

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$$

Python में NLP के लिए Feature Engineering

Cosine स्कोर: ध्यान रखने योग्य बातें

  • मान -1 से 1 के बीच.
  • NLP में, मान 0 से 1 के बीच.
  • डॉक्यूमेंट की लंबाई के प्रति रॉबस्ट.
Python में NLP के लिए Feature Engineering

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]])
Python में NLP के लिए Feature Engineering

अभ्यास करते हैं!

Python में NLP के लिए Feature Engineering

Preparing Video For Download...