计算两个数组之间得余弦值
def cosine_similarity(vector1, vector2):
dot_product = 0.0
normA = 0.0
normB = 0.0
for a, b in zip(vector1, vector2):
dot_product += a * b
normA += a ** 2
normB += b ** 2
if normA == 0.0 or normB == 0.0:
return 0
else:
return round(dot_product / ((normA**0.5)*(normB**0.5)) * 100, 2)
print('测试相似性')
a = [1,4,5]
b = [3,4,9]
print(cosine_similarity(np.array(a),np.array(b)))
print(cosine_similarity(a,b))
测试相似性
95.92
95.92
网友评论