[英]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.argpartition
对cdist
结果的平坦距离 -
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.