Search code examples
pythonpython-3.xcosine-similarity

How to store Cosine Similarity values individually?


I know this is a very basic question, but please forgive me. I have a python script which is calculating cosine similarity of sentences. The result the script is returning is like this: [[0.72894156 0.96235985 0.61194754]]. I want to store these three values into an array or list individually, so I can find the minimum and maximum values. When I store them in an array, it stores them altogether in a single value. Here is the script:

sentence_embeddings = model.encode(sentences)
sentence_embeddings.shape
result = (cosine_similarity(
        [sentence_embeddings[0]],
        sentence_embeddings[1:]
))
print(result)

Your help is much appreciated!


Solution

  • To clarify, OP is asking for the result [[0.72894156, 0.96235985, 0.61194754]] of shape (1, 3) to become [[0.72894156], [0.96235985], [0.61194754]] of shape (3, 1).

    As mentioned in the comments, we can either use .reshape or a more generalizable way of transposing .T.

    result.reshape(-1, 1) and result.T