V
V
voronin_denis2019-08-23 09:49:08
Python
voronin_denis, 2019-08-23 09:49:08

How to optimize finding closest values ​​in python array?

There is a method that looks for the minimum deviation from the given longitude and latitude in the array and returns the indices of the values ​​closest to the given ones. But since the shape of the array of input parameters is (4800 4800) , and the shape of the required parameters is (1200 1370), the enumeration takes a long time. Tell me how you can optimize the enumeration of values?

@jit(nopython=True, parallel=True)
def get_indexes_argmin(tx, ty, lat_array, lon_array):
    return (np.abs(lat_array - round(ty, 6) + np.abs(lon_array - round(tx, 6)))).argmin()

def get_unravel_coord(tx, ty, lat_array, lon_array):
    return np.unravel_index(get_indexes_argmin(tx, ty, lat_array, lon_array), lat_array.shape, order='F')

for y in range(4795):
     ta = tranform_array[x][y]
     tx = ta[0]
     ty = ta[1]
     coord = get_unravel_coord(ty, tx, lat_array, lon_array

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
voronin_denis, 2019-08-23
@voronin_denis

Seems to have found a solution. Use scipy.spatial.distance.cdist() https://docs.scipy.org/doc/scipy/reference/generat...

R
res2001, 2019-08-23
@res2001

If you sort the array, then the nearest values ​​will be in the vicinity of the point. Search in sorted array by binary search.
Trees can be used instead of sorting and binary search.

D
devalone, 2019-08-23
@devalone

If I understand the task correctly, then some tree will suit you, for example kd tree https://en.wikipedia.org/wiki/K-d_tree

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question