0

I have two numpy arrays Array1, Array2. Array1 has shape (27000,), while Array2 has shape (501,3). Both of the arrays have np.float32 numeric values inside them. I have to do the following:

  1. First, for every element in Array1, find the (index of) closest matching element in the first column of Array2.
  2. Next, retrieve the values for these 'nearest-matching' indexes from second (or third) column of Array2. Here, I would like to have a choice whether I want to have values from second or from the third column.

I am seeking a fast Numpy solution to this (preferably one-liner), because this operation appears somewhere inside my code and right now my current implementation (where I break it into two parts i.e. first get indexes, then select corresponding values) of this seems to be taking some time. My current implementation is:

indices   =  abs(Array1[:, None] - Array2[:,0][None, :]).argmin(axis=-1)
final_val =  Array2[indices,2]

One might expect that a simple Numpy operation for this should be fast enough, however, I need to run my program thousands of time, so any time saving on this (most time consuming part) would help me greatly.

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
chupa_kabra
  • 465
  • 2
  • 5
  • 15

1 Answers1

0

I think I found an answer here:

NumPy: Vectorize finding closest value in an array for each element in another array

In short, use numpy.searchsorted() for better performance.

chupa_kabra
  • 465
  • 2
  • 5
  • 15