I have two arrays, A (size = (20, 200) and B (size = (15, 200)). I want to construct a matrix C (size = (20, 15)) s.t c[i,j] store the cosine similarity between elements A[i] and B[j]? I can do that using a loop, but it takes so long time if A and B are big arrays.
Asked
Active
Viewed 33 times
1 Answers
0
You can use the scipy.spatial.distance.cosine function to compute the cosine similarity between two arrays:
import numpy as np from scipy.spatial.distance import cosine A = np.random.rand(20, 200) B = np.random.rand(15, 200) C = np.zeros((20, 15)) for i in range(20): for j in range(15): C[i, j] = cosine(A[i], B[j])

Mohamed Elgazar
- 778
- 4
- 9
-
I am just asking if there is another solution without using for loop? – External_Happy_77 Oct 14 '22 at 18:08