GeoMonkey
GeoMonkey

Reputation: 1665

sum one numpy array based on bins of another

I have values that represent the height of a number of data points, such that:

arr = np.array([1.0,1.2,1.6,2.3,2.2,2.6,2.8,2.2,3.7,3.3......7.1,7.7,7.3,7.9,7.5,7.3,8.2,9.9,9.3,9.5,10.0,10.2])

associated with these is another array of their area:

arr2 = np.array([50,30,25,21,36,87,54,34,67,43,21,45,......25,46,78,42])

both arr and arr2 have the same shape.

I can get a histogram from arr using:

np.hist(arr,bins=11,range=(0,11)) 

which gives me the number of counts per bin, but what I would like is the total area per bin (from arr2).

For example for heights >=1 and <2, I would like the total area, which in this case would be:

50+30+25 = 105

is there a function or a pythonic way of getting the total from arr2 that fall within bins created from arr?

Upvotes: 6

Views: 1275

Answers (1)

GeoMonkey
GeoMonkey

Reputation: 1665

I achieved this by using numpy.histogram. Using weights allowed me to sum the values in arr2 based on bins of arr, as in the docs: https://docs.scipy.org/doc/numpy/reference/generated/numpy.histogram.html

so my new code is:

n, bins = numpy.histogram(arr, bins=100, range=(0,100), weights=arr2)

Upvotes: 4

Related Questions