I have a very large array, similar to the format height data:
triplets = ((x0, y0, z0),
(x1, y1, z1),
... ,
(xn, yn, zn))
where x, y, z are all floats in meters. You can create suitable test data that matches this format with
x = np.arange(20, 40, dtype=np.float64)
y = np.arange(30, 50, dtype=np.float64)
z = np.random.random(20) * 25.0
triplets = np.hstack((x, y, z)).reshape((len(x),3))
I want to be able to efficiently find the corresponding z value for a given pair (x, y). So far, my research leads to more questions. Here is what I have:
Iterate through all triplets:
query = (a, b)
for i in triplets:
if i[0] == query[0] and i[1] == query[1]:
result = i[2]
Disadvantages: slow; a, bmust exist, which is a problem when comparing floats.
Use scipy.spatial.cKDTreeto find the closest:
points = triplets[:,0:2]
tree = cKDTree(points)
idx = tree.query((a, b))[1]
query = tree.data[idx]
result = triplets[idx, 2]
Disadvantages: returns the closest point, rather than interpolating.
Using interp2daccording to the comment:
f = interp2d(x, y, z)
result = f(a, b)
: . OverflowError: Too many data points to interpolate . ( 11 .)
, : - , ? ?