简体   繁体   English

如何在它们之间距离最大的n个点中获得m对点

[英]How to get m pair of points among n points that have the largest distance between them

Say I have the following points defined in a one dimensional space:假设我在一维空间中定义了以下几点:

x = np.array([[0.70710678],
             [0.70710678],
             [0.        ],
             [1.41421356]])

I want to get m pair of points among these n points that have the longest euclidean distance between them (if m is 1 in this case will be 1.4142 and 0 )我想在这 n 个点中得到 m 对,它们之间的欧几里得距离最长(如果 m 是 1 在这种情况下将是 1.4142 和 0 )

I tried getting the pairwise distance with:我尝试通过以下方式获得成对距离:

from scipy.spatial.distance import pdist, cdist

cdist(x,x, 'seuclidean')

from this part I'm not sure how to do the rest however.但是,从这部分开始,我不确定如何执行 rest。

We could make use of np.argpartition on flattened distances off cdist result -我们可以利用np.argpartitioncdist结果的平坦距离 -

dists = np.triu(cdist(x,x, 'seuclidean'),1)
s = dists.shape
idx = np.vstack(np.unravel_index(np.argpartition(dists.ravel(),-m)[-m:],s)).T

idx would be m pairs of indexes that are farthest, ie each row of idx would represent indexes of one pair from x . idx将是最远的m对索引,即idx的每一行将代表来自x的一对索引。

Sample run -样品运行 -

# with m = 1
In [144]: idx
Out[144]: array([[2, 3]])

# with m = 2    
In [147]: idx
Out[147]: 
array([[1, 2],
       [2, 3]])

# with m = 3        
In [150]: idx
Out[150]: 
array([[0, 3],
       [1, 2],
       [2, 3]])

Sample run on 2D array -2D数组上运行示例 -

In [44]: x
Out[44]: 
array([[1.25, 1.25],
       [1.25, 1.25],
       [1.87, 1.87],
       [0.62, 0.62],
       [0.62, 0.62],
       [1.25, 1.25],
       [0.  , 0.  ],
       [0.62, 0.62]])

In [45]: m = 2

In [46]: dists
Out[46]: 
array([[0.  , 0.  , 1.58, 1.58, 1.58, 0.  , 3.16, 1.58],
       [0.  , 0.  , 1.58, 1.58, 1.58, 0.  , 3.16, 1.58],
       [0.  , 0.  , 0.  , 3.16, 3.16, 1.58, 4.74, 3.16],
       [0.  , 0.  , 0.  , 0.  , 0.  , 1.58, 1.58, 0.  ],
       [0.  , 0.  , 0.  , 0.  , 0.  , 1.58, 1.58, 0.  ],
       [0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 3.16, 1.58],
       [0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 1.58],
       [0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  ]])

In [47]: idx
Out[47]: 
array([[0, 6],
       [2, 6]])

Note that because of the way argpartition works, idx might not have the indices in their sorted order of distances.请注意,由于argpartition的工作方式, idx可能没有按距离排序的索引。 To force it that way, we could do -要以这种方式强制它,我们可以这样做 -

idx[dists[tuple(idx.T)].argsort()]

To pair each point with it's furthest counterpart you can use:要将每个点与其最远的对应点配对,您可以使用:

np.dstack((x, x[cdist(x,x, 'seuclidean').argmax(axis=-1)]))

#array([[[0.70710678, 0.        ]],
#
#       [[0.70710678, 0.        ]],
#
#       [[0.        , 1.41421356]],
#
#       [[1.41421356, 0.        ]]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 查找所有点对之间的欧几里得距离 - Finding euclidean distance between all pair of points 如何添加 2 个点和它们之间的距离 (SRID = 32636)? - How to add 2 points with distance between them (SRID = 32636)? GeoDjango:我怎样才能得到两点之间的距离? - GeoDjango: How can I get the distance between two points? 如何在python中的开始和结束坐标之间获得'n'个随机点? - How to get 'n' random points between start and end coordinates in python? 生成 50 个点,它们之间的最小距离为 0.1 - generate 50 points with minimum distance between them is 0.1 在N点之间的距离计算中,令人失望的结果是pyCUDA基准 - Disappointing results in pyCUDA benchmark for distance computing between N points N 点与 numpy/scipy 中的参考点之间的有效距离计算 - Efficient distance calculation between N points and a reference in numpy/scipy NumPy:每m点选择n个点 - NumPy: Selecting n points every m points 从 3D 空间采样 N 个点的有效方法,约束条件是 Python 中两点之间的最小距离 - Efficient way to sample N points from a 3D space with the constraint of a minimum distance between two points in Python 如何使用欧几里得距离计算两点之间的距离? - How to calculate the distance between two points using Euclidean distance?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM