利用numpy可以很方便的计算两个二维数组之间的距离。二维数组之间的距离定义为:X的维度为(a,c),Y的维度为(b,c),Z为X到Y的距离数组,维度为(a,b)。且Z[0,0]是X[0]到Y[0]的距离。Z(m,n)为X[m]到Y[n]的距离。如下图所示。
代码如下:
#computer the distance between text point x and train point x_train
import numpy as np
X = np.random.random((3,2))
X_train = np.random.random((5,2))
print('X:')
print(X)
print('X_train:')
print(X_train)
dist = np.zeros((X.shape[0],X_train.shape[0]))
print('--------------------')
#way 1:use two loops
for i in range(X.shape[0]):
for j in range(X_train.shape[0]):
dist[i,j] = np.sum((X[i,:]-X_train[j,:])**2)
print('way 1 result:')
print(dist)
#way 2:use one loops
for i in range(X.shape[0]):
dist[i,:] = np.sum((X_train-X[i,:])**2,axis=1)
print('--------------------')
print('way 2 result:')
print(dist)
#way 3:use no loops
dist = np.reshape(np.sum(X**2,axis=1),(X.shape[0],1))+ np.sum(X_train**2,axis=1)-2*X.dot(X_train.T)
print('--------------------')
print('way 3 result:')
print(dist)
结果:
X:
[[ 0.2892627 0.46569586]
[ 0.75739842 0.33398985]
[ 0.95221813 0.11192751]]
X_train:
[[ 0.10710814 0.27200357]
[ 0.09082801 0.68378859]
[ 0.62707459 0.81271073]
[ 0.29597022 0.56699045]
[ 0.42406439 0.93302285]]
--------------------
way 1 result:
[[ 0.07069699 0.08694076 0.23453619 0.01030558 0.23656601]
[ 0.42671974 0.56667526 0.24615799 0.26720526 0.46995212]
[ 0.73983525 1.06901804 0.59681545 0.6377436 0.95314395]]
--------------------
way 2 result:
[[ 0.07069699 0.08694076 0.23453619 0.01030558 0.23656601]
[ 0.42671974 0.56667526 0.24615799 0.26720526 0.46995212]
[ 0.73983525 1.06901804 0.59681545 0.6377436 0.95314395]]
--------------------
way 3 result:
[[ 0.07069699 0.08694076 0.23453619 0.01030558 0.23656601]
[ 0.42671974 0.56667526 0.24615799 0.26720526 0.46995212]
[ 0.73983525 1.06901804 0.59681545 0.6377436 0.95314395]]