在Python中用许多点找到两个最远点的点

Ren*_*lts 5 python geometry numpy matplotlib

我需要找到距离彼此最远的两个点.正如屏幕截图所示,我有一个包含其他两个数组的数组.一个用于X,一个用于Y坐标.确定数据中最长线的最佳方法是什么?通过这样说,我需要选择情节中最远的两个点.希望你们能帮忙.下面是一些帮助解释问题的截图.

数据点可视化

numpy数组中的数据点

hil*_*lem 11

您可以通过观察相距最远的两个点将作为凸包中的顶点出现来避免计算成对距离.然后,您可以计算较少点之间的成对距离.

例如,在单位平方中均匀分布100,000个点,在我的实例中凸包中只有22个点.

import numpy as np
from scipy import spatial

# test points
pts = np.random.rand(100_000, 2)

# two points which are fruthest apart will occur as vertices of the convex hull
candidates = pts[spatial.ConvexHull(pts).vertices]

# get distances between each pair of candidate points
dist_mat = spatial.distance_matrix(candidates, candidates)

# get indices of candidates that are furthest apart
i, j = np.unravel_index(dist_mat.argmax(), dist_mat.shape)

print(candidates[i], candidates[j])
# e.g. [  1.11251218e-03   5.49583204e-05] [ 0.99989971  0.99924638]
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


如果数据是二维的,您可以在时间上计算凸包,O(N*log(N))其中N是点数.不幸的是,随着维度数量的增加,性能提升消失了.