Python 求向量的余弦值操作

作者:追求卓越583 时间:2022-11-24 22:51:11 

1、余弦相似度

余弦相似度衡量的是2个向量间的夹角大小,通过夹角的余弦值表示结果,因此2个向量的余弦相似度为:

Python 求向量的余弦值操作

余弦相似度的取值为[-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]

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。

来源:https://blog.csdn.net/zhuiqiuzhuoyue583/article/details/80145026

标签:Python,向量,余弦值
0
投稿

猜你喜欢

  • sqlserver 数据库学习笔记

    2024-01-14 21:54:33
  • MySQL如何查询当前正在运行的SQL语句

    2009-02-13 13:40:00
  • js改变文章字体大小的实例代码

    2024-04-19 10:03:21
  • 一文搞懂Python中is和==的区别

    2023-11-15 09:42:27
  • python爬虫豆瓣网的模拟登录实现

    2023-07-08 20:28:08
  • Python如何实现FTP功能

    2021-10-22 15:08:25
  • MySQL内连接和外连接及七种SQL JOINS的实现

    2024-01-21 09:23:16
  • javascript 密码强度验证规则、打分、验证

    2010-05-18 19:58:00
  • JavaScript实现alert弹框效果

    2024-05-10 10:57:40
  • 在PyCharm导航区中打开多个Project的关闭方法

    2022-08-11 01:39:11
  • linux修改mysql数据库文件的路径

    2024-01-19 20:50:42
  • 新手如何快速入门Python(菜鸟必看篇)

    2021-01-27 01:24:35
  • python如何提升爬虫效率

    2021-12-17 22:18:24
  • Python Matplotlib绘制多子图详解

    2021-01-11 11:27:05
  • Go语言操作数据库及其常规操作的示例代码

    2024-01-14 07:05:46
  • 详解python调度框架APScheduler使用

    2021-11-05 22:55:36
  • PHP 用数组降低程序的时间复杂度

    2023-10-08 11:38:03
  • PHP xpath()函数讲解

    2023-06-04 07:03:38
  • Opencv实现眼睛控制鼠标的实践

    2023-07-04 11:21:52
  • 在Django中实现添加user到group并查看

    2021-12-08 21:47:07
  • asp之家 网络编程 m.aspxhome.com