0

I need to bin some data, it takes too much time with my caveman coding, so I need some optimization. I have a time array and a magnitude array, I need to store multiple magnitude values of according to their time data. For example bin the magnitude values between 0-2 sec, then 2-4,4-6 and so on. My code is:

   for i in range(time_values.shape[0]): ----> around 1000, so 1000+ elif statement
      if (time[i] > 0 ) and ( 0.001 > time[i]):
        bins['bin_'+str( 0 )].append(magnitude[i])
      elif (time[i] > 0.001 ) and ( 0.002 > time[i]):
        bins['bin_'+str( 1 )].append(magnitude[i])
      elif (time[i] > 0.002 ) and ( 0.003 > time[i]):
        bins['bin_'+str( 2 )].append(magnitude[i])
             ...
             ...
             ...

As you can see, I used a dictionary to bin the data, thinking it would be easier to create around 1000 empty bin array. But I'm starting to regret this since its taking very long. Is there a faster way of doing this job?

Olca Orakcı
  • 372
  • 3
  • 12
  • Not quite a duplicate, but [this](https://stackoverflow.com/questions/6163334/binning-data-in-python-with-scipy-numpy) will definitely answer the question. `np.histogram` is the way to go. – bnaecker Jun 17 '20 at 20:07
  • You should provide a sample of your data - enough to reproduce your process. Please read [mre]. – wwii Jun 17 '20 at 20:10
  • I think you are looking for https://numpy.org/doc/stable/reference/generated/numpy.bincount.html – Ehsan Jun 17 '20 at 21:58

0 Answers0