forked from NeuroTechSC/Data-Preprocessing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cosine_similarity.py
42 lines (31 loc) · 1010 Bytes
/
cosine_similarity.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from scipy import spatial
import numpy as np
# from sklearn.metrics.pairwise import cosine_similarity
from scipy import sparse
a = [1, 2, 3]
b = [1, 2, 4]
c = [4, 5, 6]
ipa_dict = {'b' : b , 'c' : c}
similarities = {}
''' WAY 1 with dictionaries '''
for key, vector in ipa_dict.items():
cos_sim = 1 - spatial.distance.cosine(a, vector)
similarities[key] = (cos_sim)
# way 1 (just lists)
cos_sim_ab = 1 - spatial.distance.cosine(a, b)
# similarities['b'] = (cos_sim_ab)
cos_sim_ac = 1 - spatial.distance.cosine(a, c)
# similarities['c'] = (cos_sim_ac)
print("first way values:")
print(cos_sim_ab)
print(cos_sim_ac)
best = max(similarities, key = similarities.get)
print("the most similar vector to a is " + best + "\n")
''' WAY 2 '''
cos_sim_b = np.dot(a,b)/(np.linalg.norm(a)*np.linalg.norm(b))
cos_sim_c = np.dot(a,c)/(np.linalg.norm(a)*np.linalg.norm(c))
print("second way values:")
print(cos_sim_b)
print(cos_sim_c)
''' WAY 3 (not working for me) '''
# cos_sim = cosine_similarity(a, b)