我有一个点集,我将其坐标存储在三个不同的数组(xa,ya,za)中。现在,我想用另一个点集(xb,yb,zb)的所有点来计算此点集(xa [0],ya [0],za [0]等)的每个点之间的欧氏距离。 )并且每次都在新阵列中存储最小距离。
假设xa.shape =(11,),ya.shape =(11,),za.shape =(11,)。分别为xb.shape =(13,),yb.shape =(13,),zb.shape =(13,)。我想要做的是每次使用一个xa [],ya [],za [],并计算它与xb,yb,zb的所有元素的距离,并在最后将最小值存储到xfinal中。 shape =(11,)数组。
你觉得numpy会有可能吗?
答案 0 :(得分:8)
另一种解决方案是使用scipy中的空间模块,特别是KDTree。
本课程从一组数据中学习,并可在给定新数据集的情况下进行查询:
from scipy.spatial import KDTree
# create some fake data
x = arange(20)
y = rand(20)
z = x**2
# put them togheter, should have a form [n_points, n_dimension]
data = np.vstack([x, y, z]).T
# create the KDTree
kd = KDTree(data)
现在,如果您有一个点,您可以通过以下方式询问壁橱点(或N个最近点)的距离和索引:
kd.query([1, 2, 3])
# (1.8650720813822905, 2)
# your may differs
或者,给定一系列职位:
#bogus position
x2 = rand(20)*20
y2 = rand(20)*20
z2 = rand(20)*20
# join them togheter as the input
data2 = np.vstack([x2, y2, z2]).T
#query them
kd.query(data2)
#(array([ 14.96118553, 9.15924813, 16.08269197, 21.50037074,
# 18.14665096, 13.81840533, 17.464429 , 13.29368755,
# 20.22427196, 9.95286671, 5.326888 , 17.00112683,
# 3.66931946, 20.370496 , 13.4808055 , 11.92078034,
# 5.58668204, 20.20004206, 5.41354322, 4.25145521]),
#array([4, 3, 2, 4, 2, 2, 4, 2, 3, 3, 2, 3, 4, 4, 3, 3, 3, 4, 4, 4]))
答案 1 :(得分:1)
您可以使用np.subtract.outer(xa, xb)
计算每个xa与每个xb之间的差异。到最近的xb的距离由
np.min(np.abs(np.subtract.outer(xa, xb)), axis=1)
要将其扩展为3D,
distances = np.sqrt(np.subtract.outer(xa, xb)**2 + \
np.subtract.outer(ya, yb)**2 + np.subtract.outer(za, zb)**2)
distance_to_nearest = np.min(distances, axis=1)
如果您确实想知道哪个 b点是最接近的,则使用argmin
代替min
。
index_of_nearest = np.argmin(distances, axis=1)
答案 2 :(得分:0)
这样做的方法不止一种。最重要的是,在内存使用和速度之间需要权衡。这是浪费的方法:
s = (1, -1)
d = min((xa.reshape(s)-xb.reshape(s).T)**2
+ (ya.reshape(s)-yb.reshape(s).T)**2
+ (za.reshape(s)-zb.reshape(s).T)**2), axis=0)
另一种方法是迭代b
中的点集,以避免扩展到完整的矩阵。