如何单独存储余弦相似度值?

How to store Cosine Similarity values individually?

我知道这是一个非常基本的问题,但请原谅我。我有一个 python 脚本,它正在计算句子的余弦相似度。脚本返回的结果是这样的:[[0.72894156 0.96235985 0.61194754]]。我想将这三个值分别存储到一个数组或列表中,这样我就可以找到最小值和最大值。当我将它们存储在一个数组中时,它会将它们一起存储在一个值中。这是脚本:

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

非常感谢您的帮助!

这可能会有所帮助。我遇到了类似的东西并将输出视为数组。为了根据我比较的文本获得具体分数,我做了以下操作:

cos_text = [TextA, TextB]
cv = CountVectorizer()
count_matrix = cv.fit_transform(cos_text)

#word matrix
doc_term_matrix = count_matrix.todense()
df_matrix = pd.DataFrame(doc_term_matrix,
                         columns=cv.get_feature_names_out())
#individual score
score=cosine_similarity(df_matrix)
cs_scoreA = score[0,1]
cs_scoreB = score[1,0]

为了澄清,OP 要求形状 (1, 3) 的结果 [[0.72894156, 0.96235985, 0.61194754]] 变成形状 (3, 1)[[0.72894156], [0.96235985], [0.61194754]]

如评论中所述,我们可以使用 .reshape 或更通用的转置方式 .T

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