码迷,mamicode.com
首页 > 其他好文 > 详细

Cosine Similarity

时间:2017-07-22 23:47:16      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:sklearn   cos   ace   class   use   tor   transform   pre   highlight   

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

标签:sklearn   cos   ace   class   use   tor   transform   pre   highlight   

原文地址:http://www.cnblogs.com/gwnbu/p/7222942.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!