Cosine Similarity

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Cosine Similarity相关的知识,希望对你有一定的参考价值。

http://blog.christianperone.com/2013/09/machine-learning-cosine-similarity-for-vector-space-models-part-iii/

 

documents = (
"The sky is blue",
"The sun is bright",
"The sun in the sky is bright",
"We can see the shining sun, the bright sun"
)

from sklearn.feature_extraction.text import TfidfVectorizer
tfidf_vectorizer = TfidfVectorizer()
tfidf_matrix = tfidf_vectorizer.fit_transform(documents)
# print tfidf_matrix

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

import math
# This was already calculated on the previous step, so we just use the value
cos_sim = 0.52305744
angle_in_radians = math.acos(cos_sim)
print math.degrees(angle_in_radians)

  

以上是关于Cosine Similarity的主要内容,如果未能解决你的问题,请参考以下文章

scikit cosine_similarity vs pairwise_distances

在 Dask 数组-python 上使用 scikit-learn cosine_similarity

torch.nn.functional.cosine_similarity使用详解

torch.nn.functional.cosine_similarity使用详解

皮尔逊相关系数与余弦相似度(Pearson Correlation Coefficient & Cosine Similarity)

R语言使用lsa包计算余弦相似度(Cosine Similarity)实战:两个向量的余弦相似度矩阵的余弦相度