0

Given an array such that:

arr = np.array([0,0,1,1,1,2,2,3,3,3,3,3,4,4,5,5,5,5,5,6])

I want to know the frequency of values >=0 & <1, >=1 & <2, >=2 & <3...etc

so I would get an new array like:

freq =[2,3,2,5,2,5,1].

I can use np.where((arr>=0)&(arr<1)) but this changes the shape of arr. Is there a pythonic way of getting the frequency of values over an interval of 1?

GeoMonkey
  • 1,615
  • 7
  • 28
  • 56
  • Do you actually have integers and single integer intervals? Because `np.bincount` is likely all you need in that case. If not, `np.histogram`. – miradulo Jul 13 '18 at 00:23
  • the interval range will be integer as in the question but the values in arr may be either floats or integers – GeoMonkey Jul 13 '18 at 00:26
  • Then `np.histogram(arr, bins=7, range=(0, 7))` for instance. – miradulo Jul 13 '18 at 00:29
  • so for clarity, this is inclusive of the lower limit but not the upper limit, correct? so for example >=0 & < 1 – GeoMonkey Jul 13 '18 at 00:32
  • Yep, as noted in the [docs](https://docs.scipy.org/doc/numpy/reference/generated/numpy.histogram.html). – miradulo Jul 13 '18 at 00:34

0 Answers0