How to count values in a certain range in a Numpy array?

If your array is called a, the number of elements fulfilling 25 < x < 100 is

((25 < a) & (a < 100)).sum()

The expression (25 < a) & (a < 100) results in a Boolean array with the same shape as a with the value True for all elements that satisfy the condition. Summing over this Boolean array treats True values as 1 and False values as 0.

Leave a Comment