【问题标题】:Alternative (python) to calculate distance between all points at two different sets替代(python)计算两个不同集合中所有点之间的距离
【发布时间】:2021-12-27 21:39:42
【问题描述】:

我有两个不同的点:set_1set_2。 使用python,我想计算从set_1 点到set_2 点的所有距离。

点是一维数组:

point_1=np.array([x1,y1,z1])

点集是二维数组:

set_1=np.array([[x1,y1,z1],[x2,y2,z2], ...[xn,yn,zn]])

使用scipy.spatial 中的distance 这是我的方法:

np.array([[distance.euclidean(i,j) for i in set_1] for j in set_2])

我可以以某种方式直接将distance.euclidean 申请到set_1set_2 吗?

【问题讨论】:

    标签: python scipy euclidean-distance


    【解决方案1】:

    这正是scipy.spatial.distance.cdist 的设计目的。

    例如,

    In [21]: import numpy as np
    
    In [22]: from scipy.spatial.distance import cdist
    
    In [23]: set_1 = np.array([[0, 0, 0], [1, 1, 2], [1, -1, 3]])
    
    In [24]: set_2 = np.array([[0, 0, 10], [1, 1, 1]])
    
    In [25]: cdist(set_1, set_2)
    Out[25]: 
    array([[10.        ,  1.73205081],
           [ 8.1240384 ,  1.        ],
           [ 7.14142843,  2.82842712]])
    

    【讨论】:

      猜你喜欢
      • 2021-11-21
      • 2010-10-30
      • 2019-03-23
      • 1970-01-01
      • 2012-07-04
      • 2011-04-23
      相关资源
      最近更新 更多