如果只需要每对点之间的距离,则无需计算完整的距离矩阵。
而是直接计算:
import numpy as npx = np.array([[[1,2,3,4,5], [5,6,7,8,5], [5,6,7,8,5]], [[11,22,23,24,5], [25,26,27,28,5], [5,6,7,8,5]]])y = np.array([[[31,32,33,34,5], [35,36,37,38,5], [5,6,7,8,5]], [[41,42,43,44,5], [45,46,47,48,5], [5,6,7,8,5]]])xx = x.reshape(2, -1)yy = y.reshape(2, -1)dist = np.hypot(*(xx - yy))print dist
为了进一步说明正在发生的事情,首先,我们对数组进行整形,使其具有2xN的形状(这
-1是一个占位符,告诉numpy自动沿该轴计算正确的大小):
In [2]: x.reshape(2, -1)Out[2]: array([[ 1, 2, 3, 4, 5, 5, 6, 7, 8, 5, 5, 6, 7, 8, 5], [11, 22, 23, 24, 5, 25, 26, 27, 28, 5, 5, 6, 7, 8, 5]])
因此,当我们减去
xx和时
yy,我们将得到一个2xN的数组:
In [3]: xx - yyOut[3]: array([[-30, -30, -30, -30, 0, -30, -30, -30, -30, 0, 0, 0, 0, 0, 0], [-30, -20, -20, -20, 0, -20, -20, -20, -20, 0, 0, 0, 0, 0, 0]])
然后,我们可以将其解压缩到
dx和
dy组件中:
In [4]: dx, dy = xx - yyIn [5]: dxOut[5]: array([-30, -30, -30, -30, 0, -30, -30, -30, -30, 0, 0, 0, 0, 0, 0])In [6]: dyOut[6]: array([-30, -20, -20, -20, 0, -20, -20, -20, -20, 0, 0, 0, 0, 0, 0])
并计算距离(
np.hypot等于
np.sqrt(dx**2 + dy**2)):
In [7]: np.hypot(dx, dy)Out[7]: array([ 42.42640687, 36.05551275, 36.05551275, 36.05551275, 0. , 36.05551275, 36.05551275, 36.05551275, 36.05551275, 0. , 0. , 0. , 0. , 0. , 0. ])
或者,我们可以自动完成拆箱并一步一步完成:
In [8]: np.hypot(*(xx - yy))Out[8]: array([ 42.42640687, 36.05551275, 36.05551275, 36.05551275, 0. , 36.05551275, 36.05551275, 36.05551275, 36.05551275, 0. , 0. , 0. , 0. , 0. , 0. ])
如果要计算其他类型的距离,只需更改
np.hypot为要使用的函数即可。例如,对于曼哈顿/城市街区距离:
In [9]: dist = np.sum(np.abs(xx - yy), axis=0)In [10]: distOut[10]: array([60, 50, 50, 50, 0, 50, 50, 50, 50, 0, 0, 0, 0, 0, 0])



