# Calculating cosine similarity between 3D arrays using Python

I have two matrices with multiple columns and three rows each. I calculated the cosine similarity (sklearn) but it gives the result as a matrix. How can I obtain one single value? The matrices are the embeddings of two words each, obtained from BERT.

– nag
Jun 18 '19 at 12:23
• Already checked, thanks anyway. Jun 20 '19 at 4:14

Your input matrices (with 3 rows and multiple columns) are saying that there are 3 samples, with multiple attributes. So the output you will get will be a 3x3 matrix, where each value is the similarity to one other sample (there are 3 x 3 = 9 such combinations)

If you were to print out the pairwise similarities in sparse format, then it might look closer to what you are after.

I have created two example matrices of random numbers that fits your description:

from sklearn.metrics.pairwise import cosine_similarity
from scipy import sparse

a = np.random.random((3, 10))
b = np.random.random((3, 10))

# Create sparse matrices, which compute faster and give more understandable output
a_sparse, b_sparse = sparse.csr_matrix(a), sparse.csr_matrix(b)

sim_sparse = cosine_similarity(a_sparse, b_sparse, dense_output=False)
print(sim_sparse)


Output:

  (0, 2)    0.7938732813430508
(0, 1)    0.7575978172453429
(0, 0)    0.7897664361147338
(1, 2)    0.740418315571796
(1, 1)    0.833981672896221
(1, 0)    0.7184526671218405
(2, 2)    0.8746293481677073
(2, 1)    0.6456666045233884
(2, 0)    0.7925289217609924


Hopefully this output makes it clearer, what you are actually getting as output.

Have a look here for a few more details on performance aspects, and the documentation on sparse matrices is here.

• I'd like to explain the problem in a more precise manner. I am extracting embeddings from a pre-trained BERT model. These are sentence embeddings. But my requirement is to find the similarity between two words using this model. I passed two words - e.g. hi and hello, but I am getting a matrix as the cosine similarity output, whereas what is expected is a single value. Am I going wrong anywhere? Jun 20 '19 at 4:15