-2

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.

1 Answers1

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