Answer the question
In order to leave comments, you need to log in
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
Seems to have found a solution. Use scipy.spatial.distance.cdist() https://docs.scipy.org/doc/scipy/reference/generat...
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question