python - 计算两个numpy数组之间的距离

标签 python numpy scipy

我对计算两个 numpy 数组(x 和 y)之间的各种空间距离很感兴趣。

http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.spatial.distance.cdist.html

import numpy as np
from scipy.spatial.distance import cdist

x = 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]]])
i,j,k = x.shape

xx = x.reshape(i,j*k).T

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

yy = y.reshape(i,j*k).T

results =  cdist(xx,yy,'euclidean')
print results

但是,上述结果会产生太多不需要的结果。我怎样才能限制它只用于我需要的结果。

我想计算 [1,11] 和 [31,41] 之间的距离; [2,22] 和 [32,42],...等等。

最佳答案

如果您只想要每对点之间的距离,则不需要计算完整的距离矩阵。

而是直接计算:

import numpy as np

x = 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]])

因此,当我们减去 xxyy 时,我们将得到一个 2xN 数组:

In [3]: xx - yy
Out[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]])

然后我们可以将其解压到 dxdy 组件中:

In [4]: dx, dy = xx - yy

In [5]: dx
Out[5]: 
array([-30, -30, -30, -30,   0, -30, -30, -30, -30,   0,   0,   0,   0,
         0,   0])

In [6]: dy
Out[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]: dist
Out[10]: array([60, 50, 50, 50,  0, 50, 50, 50, 50,  0,  0,  0,  0,  0,  0])

关于python - 计算两个numpy数组之间的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27678583/

相关文章:

python - 将 SymPy 表达式转换为 NumPy longdouble 的精度问题

python - 复数数组上的 scipy cdist 或 pdist

python - 在 Python 中使用 xmltodict 删除命名空间

python - 使用 python pandas 进行大文件分析的延迟

pandas - 箱线图错误: 1 ndim Categorical are not supported at this time

python - 哪些 Scipy 模块实际上调用了 Numpy 模块?

python - 如何可视化 python numpy 数组中的维数

python - 使用特定格式以级别顺序打印 BFS(二叉树)

python - 查明列表中是否没有项是字典中的键

python:在字典中创建一个列表