Skip to content

Commit f8f9aa3

Browse files
Merge pull request #668 from nanto88/master
add Python/cosine_similarity.py
2 parents 34edf7c + f23c48d commit f8f9aa3

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Python/cosine_similarity.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# -*- coding: utf-8 -*-
2+
import numpy as np
3+
4+
def cos_sim(a, b):
5+
"""Takes 2 vectors a, b and returns the cosine similarity according
6+
to the definition of the dot product
7+
"""
8+
dot_product = np.dot(a, b)
9+
norm_a = np.linalg.norm(a)
10+
norm_b = np.linalg.norm(b)
11+
return dot_product / (norm_a * norm_b)
12+
13+
# the counts we computed above
14+
sentence_m = np.array([1, 1, 1, 1, 0, 0, 0, 0, 0])
15+
sentence_h = np.array([0, 0, 1, 1, 1, 1, 0, 0, 0])
16+
sentence_w = np.array([0, 0, 0, 1, 0, 0, 1, 1, 1])
17+
18+
# We should expect sentence_m and sentence_h to be more similar
19+
print(cos_sim(sentence_m, sentence_m)) # 1.0
20+
print(cos_sim(sentence_m, sentence_h)) # 0.5
21+
print(cos_sim(sentence_m, sentence_w)) # 0.25

0 commit comments

Comments
 (0)