Question

I am trying to sort values in an numpy array so that I can store all of the values that are in a certain range (That could probably be phrased better). Anyway ill give an example of what I am trying to do. I have an array called bins that looks like this:

bins = array([11,11.5,12,12.5,13,13.5,14])

I also have another array called avgs:

avgs = array([11.02, 13.67, 11.78, 12.34, 13.24, 12.98, 11.3, 12.56, 13.95, 13.56,
              11.64, 12.45, 13.23, 13.64, 12.46, 11.01, 11.87, 12.34, 13,87, 13.04,
              12.49, 12.5])

What I am trying to do is to find the index values of the avgs array that are in the ranges between the values of the bins array. For example I was trying to make a while loop that would create new variables for each bin. The first bin would be everything that is between bins[0] and bins[1] and would look like:

bin1 = array([0, 6, 15])

Those index values would correspond to the values 11.02, 11.3, and 11.01 in the avgs and would be the values of avgs that were between index values 0 and 1 in bins. I also need the other bins so another example would be:

bin2 = array([2, 10, 16])

However the challenging part of this for me was that the size of bins and avgs changes based on other parameters so I was trying to build something that would be able to be expanded to larger or smaller bins and avgs arrays.

Was it helpful?

Solution

Numpy has some pretty powerful bin counting functions.

>>> binplace = np.digitize(avgs, bins) #Returns which bin an average belongs
>>> binplace
array([1, 6, 2, 3, 5, 4, 1, 4, 6, 6, 2, 3, 5, 6, 3, 1, 2, 3, 5, 7, 5, 3, 4])

>>> np.where(binplace == 1)
(array([ 0,  6, 15]),)
>>> np.where(binplace == 2)
(array([ 2, 10, 16]),)

>>> avgs[np.where(binplace == 1)]
array([ 11.02,  11.3 ,  11.01])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top