我有一个点集,我已将其坐标存储在三个不同的数组(xa、ya、za)中。现在,我想计算这个点集(xa[0]、ya[0]、za[0] 等)的每个点与另一个点集(xb、yb、zb)的所有点之间的欧氏距离) 并且每次都将最小距离存储在一个新数组中。
假设 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。形状 = (11,) 数组。
你认为这可以用 numpy 实现吗?
请您参考如下方法:
另一种解决方案是使用 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]))
