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.
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)
(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.