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
为什么这些不同?
来自源代码文档:
Cosine distance is defined as 1.0 minus the cosine similarity.
所以你的结果是有意义的。
成对距离提供两个 array.so 之间的距离越远意味着相似性越小。而余弦相似性是 1-pairwise_distance,因此余弦相似性越大意味着两个数组之间的相似性越大。