向量的几种距离计算方法

本文展示了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个点,并且保留小数点后3for 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) / (linalg.norm(n1) * linalg.norm(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 = []
qie=[]
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) / (linalg.norm(n1) * linalg.norm(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()

结果显示:
向量的几种距离计算方法_第1张图片
向量的几种距离计算方法_第2张图片
由于获取的向量为随机值,所以每次运行得到的图都是不一样的

你可能感兴趣的:(图像处理)