i have following target embeddings vector u
shape of (768,)
and target embeddings matrix v
shape of (23, 768)
. I need to calculate cosine similarity between target vector and matrix. I know how to do it in cycle:
from sklearn.metrics.pairwise import cosine_similarity
for i in v:
cosine_similarity(u.reshape(-1, 1), i.reshape(-1,1))[0][0]
But how to do it like numpy way ? My target is to have array of cosine similarities like
array[0.4, 0.5, 0.6, ... n]
CodePudding user response:
import numpy as np
np.random.seed(1)
u = np.random.random(768)
v = np.random.random((23,768))
w = np.array([u.dot(v[i])/(np.linalg.norm(u)*np.linalg.norm(v[i])) for i in range(23)])
print(w)