我有一个点集,我将其坐标存储在三个不同的数组(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有关吗?
另一种解决方案是使用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)
Run Code Online (Sandbox Code Playgroud)
现在,如果你有一个点,你可以通过这样做来询问壁橱点(或N个最近点)的距离和索引:
kd.query([1, 2, 3])
# (1.8650720813822905, 2)
# your may differs
Run Code Online (Sandbox Code Playgroud)
或者,给定一系列职位:
#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]))
Run Code Online (Sandbox Code Playgroud)