I've got a function below which takes in an array of floats as well as an array of discrete integers. For all of the floats, I want them to be rounded to the closest integer in the list.
The below function works perfectly, where sHatV is an array of 10,000 floats and possible_locations is an array of 5 integers:
binnedV = [min(possible_locations, key=lambda x:abs(x-bv)) for bv in sHatV]
As this function is going to be called thousands of time I'm trying to use the @numba.njit decorator to minimize computation time.
I thought about using np.digitize in my 'numbafied' function but it rounds values out of bounds to zeros. I want everything to be binned to one of the values in possible locations.
Overall, I need to write a numba compatible function which takes every value in the first array of length N, finds the closest value to it in array 2, and return that closest value, culminating in an array of length N with the binned values.
Any help is appreciated!