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/