不同的解決方案是使用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]))
換句話說,對於每個'xa/ya/za',你想計算到'xb/yb/zb'中最近點的距離? – NPE 2013-03-15 15:01:26
是的,如果它會更容易... – GiorgosR 2013-03-15 15:04:59