gpt4 book ai didi

python - scikit cosine_similarity 与 pairwise_distances

转载 作者:太空狗 更新时间:2023-10-29 22:06:32 25 4
gpt4 key购买 nike

Scikit-learn 的 sklearn.metrics.pairwise.cosine_similarity 和 sklearn.metrics.pairwise.pairwise_distances(.. metric="cosine") 有什么区别?

from sklearn.feature_extraction.text import TfidfVectorizer

documents = (
"Macbook Pro 15' Silver Gray with Nvidia GPU",
"Macbook GPU"
)

tfidf_vectorizer = TfidfVectorizer()
tfidf_matrix = tfidf_vectorizer.fit_transform(documents)

from sklearn.metrics.pairwise import cosine_similarity
print(cosine_similarity(tfidf_matrix[0:1], tfidf_matrix)[0,1])

0.37997836

from sklearn.metrics.pairwise import pairwise_distances
print(pairwise_distances(tfidf_matrix[0:1], tfidf_matrix, metric='cosine')[0,1])

0.62002164

为什么这些不同?

最佳答案

来自源代码 documentation :

Cosine distance is defined as 1.0 minus the cosine similarity.

所以你的结果是有道理的。

关于python - scikit cosine_similarity 与 pairwise_distances,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35281691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com