向量的几种距离计算方法

更新时间:2023-05-27 09:36:18 阅读: 评论:0

向量的⼏种距离计算⽅法
铁皮石斛的功效本⽂展⽰了4中距离计算⽅法:欧⽒距离、曼哈顿距离、切⽐雪夫距离、夹⾓余弦距离。
另外:向量可以⾃⼰根据需要来构造,本⽂的⽐较杂乱,是由特定要求计算得来的
例如:A=[0.1,0.2,0.3,0.4,0.5],B=[0.005,0.006,0.007,0.008,0.009]
1.获取⼀些随机均匀分布的点,作为向量A
a=numpy.random.randint(-20,high=160,size=(1,30))  #随机均匀分布
a=a/1000
A=[]
#只要30个点,并且保留⼩数点后3位
for i in range(30):
for b in a:
c='%.3f'%b[i]
A.append(float(c))
print(A)
2.构造向量B(这个计算⽅法内部使⽤,可忽略)
power=[0.005,0.006,0.007,0.008,0.009]
for n in range(5):
B=[]
for i in A:
random2 = numpy.random.randn()  #随机正态分布
a = i+random2/1000*power[n]
a='%.6f'%a
B.append(float(a))
# print(B)
vector1 =mat(A)
vector2 =mat(B)
3.⼏种距离计算⽅法
print("第"+str(n+1)+"次距离计算")
# 1.欧式距离
开心激情网站
ou = numpy.sqrt(numpy.sum(numpy.square(vector1 - vector2)))
ou1.append(ou)
print('欧⽒距离:', ou)
# 2.曼哈顿距离
manhadun =sum(abs(vector1 - vector2))
man.append(manhadun)
print("曼哈顿距离:", manhadun)
个人免责声明# 3.切⽐雪夫距离
qbxf =abs(vector1 - vector2).max()
qie.append(qbxf)
print("切⽐雪夫距离:", qbxf)
# 4.夹⾓余弦距离
n1 = np.squeeze(np.asarray(vector1))
n2 = np.squeeze(np.asarray(vector2))
cos1 =dot(n1, n2)/((n1)* (n2))
cos.append(cos1)
print("夹⾓余弦距离:", cos1)
print("-----------------------------")
4.把距离归⼀化,然后画出来
g1=ou1/max(ou1)
print("欧⽒距离归⼀化:",g1)
g2=man/max(man)
print("曼哈顿距离归⼀化:",g2)
g3=qie/max(qie)爆炒包菜
print("切⽐雪夫距离归⼀化:",g3)
g4=cos/max(cos)
print("余弦距离归⼀化:",g4)
# 1.欧⽒距离:蓝⾊线
plot(power, g1,'b.')
plot(power[:5], g1[:5],color='b')
# 2.曼哈顿距离:红⾊线
plot(power, g2,'r.')
plot(power[:5],g2[:5],color='r')
# 3.切⽐雪夫距离:黄⾊线
plot(power, g3,'y.')
plot(power[:5], g3[:5],color='y')
# 4.夹⾓余弦距离:⿊⾊线
plot(power, g4,'k.')
plot(power[:5], g4[:5],color='k')
title('Vector distance')
plt.show()
完整代码:
import numpy as np
import numpy
from numpy import*
from PIL import Image
from pylab import*
from sklearn import preprocessing
from scipy.spatial.distance import pdist
from itertools import chain
a=numpy.random.randint(-20,high=160,size=(1,30))  #随机均匀分布a=a/1000
A=[]
for i in range(30):
for b in a:
c='%.3f'%b[i]
A.append(float(c))
print(A)
ou1=[]
man =[]
engageqie=[]
cos=[]
power=[0.005,0.006,0.007,0.008,0.009]
for n in range(5):
B=[]
for i in A:
random2 = numpy.random.randn()  #随机正态分布
a = i+random2/1000*power[n]
a='%.6f'%a
B.append(float(a))
# print(B)
vector1 =mat(A)
vector2 =mat(B)
银行卡英文print("第"+str(n+1)+"次距离计算")
# 1.欧式距离
ou = numpy.sqrt(numpy.sum(numpy.square(vector1 - vector2)))    ou1.append(ou)
print('欧⽒距离:', ou)
# 2.曼哈顿距离
manhadun =sum(abs(vector1 - vector2))
man.append(manhadun)
print("曼哈顿距离:", manhadun)
# 3.切⽐雪夫距离
qbxf =abs(vector1 - vector2).max()
qie.append(qbxf)
print("切⽐雪夫距离:", qbxf)
# 4.夹⾓余弦距离
n1 = np.squeeze(np.asarray(vector1))
n2 = np.squeeze(np.asarray(vector2))
cos1 =dot(n1, n2)/((n1)* (n2))
cos.append(cos1)
print("夹⾓余弦距离:", cos1)
print("-----------------------------")
g1=ou1/max(ou1)
print("欧⽒距离归⼀化:",g1)
g2=man/max(man)
print("曼哈顿距离归⼀化:",g2)
g3=qie/max(qie)
print("切⽐雪夫距离归⼀化:",g3)
甲醇的化学式
g4=cos/max(cos)
print("余弦距离归⼀化:",g4)
般若波罗蜜经# 1.欧⽒距离:蓝⾊线
plot(power, g1,'b.')
plot(power[:5], g1[:5],color='b')
# 2.曼哈顿距离:红⾊线
plot(power, g2,'r.')
plot(power[:5],g2[:5],color='r')
# 3.切⽐雪夫距离:黄⾊线
plot(power, g3,'y.')
plot(power[:5], g3[:5],color='y')
# 4.夹⾓余弦距离:⿊⾊线
plot(power, g4,'k.')
plot(power[:5], g4[:5],color='k')
title('Vector distance')
plt.show()
结果显⽰:
由于获取的向量为随机值,所以每次运⾏得到的图都是不⼀样的

本文发布于:2023-05-27 09:36:18,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/791354.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:距离   计算   向量   曼哈顿   网站   声明
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图