Python怎样求2个向量间的余弦值?
Admin 2021-04-29 群英技术资讯 1507 次浏览
余弦相似度衡量的是2个向量间的夹角大小,通过夹角的余弦值表示结果,因此2个向量的余弦相似度为:

余弦相似度的取值为[-1,1],值越大表示越相似。
向量夹角的余弦公式很简单,不在此赘述,直接上代码:
def cosVector(x,y):
if(len(x)!=len(y)):
print('error input,x and y is not in the same space')
return;
result1=0.0;
result2=0.0;
result3=0.0;
for i in range(len(x)):
result1+=x[i]*y[i] #sum(X*Y)
result2+=x[i]**2 #sum(X*X)
result3+=y[i]**2 #sum(Y*Y)
#print(result1)
#print(result2)
#print(result3)
print("result is "+str(result1/((result2*result3)**0.5))) #结果显示
cosVector([2,1],[1,1])
#求余弦函数
def cosVector(x,y):
if(len(x)!=len(y)):
print('error input,x and y is not in the same space')
return;
result1=0.0;
result2=0.0;
result3=0.0;
for i in range(len(x)):
result1+=x[i]*y[i] #sum(X*Y)
result2+=x[i]**2 #sum(X*X)
result3+=y[i]**2 #sum(Y*Y)
#print("result is "+str(result1/((result2*result3)**0.5))) #结果显示
return result1/((result2*result3)**0.5)
#print("result is ",cosVector([2,1],[1,1]))
#计算query_output(60,20)和db_output(60,20)的余弦值,用60*1的向量存储
cosResult= [[0]*1 for i in range(60)]
for i in range(60):
cosResult[i][0]=cosVector(query_output[i], db_output[i])
print(cosResult)
--------------------------------------------------------------------------------------------
#计算query_output和db_output的余弦值,用60*1的向量存储
rows=query_output.shape[0] #行数
cols=query_output.shape[1] #列数
cosResult= [[0]*1 for i in range(rows)]
for i in range(rows):
cosResult[i][0]=cosVector(query_output[i], db_output[i])
#print(cosResult)
#将结果存入文件中,并且一行一个数字
file=open('cosResult.txt','w')
for i in cosResult:
file.write(str(i).replace('[','').replace(']','')+'\n') #\r\n为换行符
file.close()
补充:python实现余弦近似度
def cos(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 None
else:
return 0.5 + 0.5 * dot_product / ((normA*normB)**0.5) #归一化 <span style="font-family: Arial, Helvetica, sans-serif;">从[-1,1]到[0,1]</span>
num = float(A.T * B) #若为行向量则 A * B.T denom = linalg.norm(A) * linalg.norm(B) cos = num / denom #余弦值 sim = 0.5 + 0.5 * cos #归一化 从[-1,1]到[0,1]
以上就是关于Python求向量的余弦值的介绍,上述代码仅供大家参考,希望对大家有帮助,更多Python相关内容大家可以关注其他文章。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
python3 默认的是UTF-8格式,但在在用dump写入的时候仍然要注意:如下importjsondata1={"TestId":"testcase001","Method":"post","Title":
ROI区域又叫感兴趣区域。在机器视觉、图像处理中,从被处理的图像以方框、圆、椭圆、不规则多边形等方式勾勒出需要处理的区域,称为感兴趣区域,ROI。本文主要为大家介绍如何通过Python+OpenCV提取ROI区域,需要的朋友可以了解一下
本篇文章给大家带来了关于Python的相关知识,KNN分类算法(K-Nearest-Neighbors Classification),又叫K近邻算法,是一个概念极其简单,而分类效果又很优秀的分类算法,下面一起来看一下,希望对大家有帮助。
这个名为PyScript的框架,其核心目标是为开发者提供在标准HTML中嵌入Python代码的能力,使用 Python调用JavaScript函数库,并以此实现利用Python创建Web应用的功能,本文给大家介绍Python HTML运行的案例解析,感兴趣的朋友一起看看吧
这篇文章主要介绍了浅谈Python任务自动化工具Tox,tox 是一个管理测试虚拟环境的命令行工具, 它已存在多年且广被开发者们使用,对Python任务自动化工具Tox基本用法感兴趣的朋友一起看看吧
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008