Some distance vector calculation method

4 herein shows the distance calculated: Euclidean distance, Manhattan distance, Chebyshev distance, cosine distance.
Furthermore: own vector may be constructed as required, the more messy herein, is calculated from the specific requirements come
example: A = [0.1,0.2,0.3,0.4,0.5], B = [0.005,0.006,0.007,0.008, 0.009]

1. Obtain a random number uniformly distributed points, as vector 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. The configuration vector B (inside this calculation method, can be ignored)
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. Some distance calculation method
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. The distance normalization, and then draw out
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()

Complete code:

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()

The results showed that:
Here Insert Picture Description
Here Insert Picture Description
Since the random value vector is acquired, each run so obtained is not the same in FIG.

Guess you like

Origin blog.csdn.net/gm_Ergou/article/details/92841702
Recommended