Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a NumPy array of values. I want to count how many of these values are in a specific range say x<100 and x>25. I have read about the counter, but it seems to only be valid for specif values not ranges of values. I have searched, but have not found anything regarding my specific problem. If someone could point me towards the proper documentation I would appreciate it. Thank you

I have tried this

   X = array(X)    for X in range(25, 100):        print(X) 

But it just gives me the numbers in between 25 and 99.

EDIT The data I am using was created by another program. I then used a script to read the data and store it as a list. I then took the list and turned it in to an array using array(r).

Edit

The result of running

 >>> a[0:10]  array(['29.63827346', '40.61488812', '25.48300065', '26.22910525',    '42.41172923', '20.15013315', '34.95323355', '13.03604098',    '29.71097606', '9.53222141'],    dtype='<U11') 
like image 971
Stripers247 Avatar asked Mar 05 '12 00:03

Stripers247


People also ask

Is there a NumPy count function?

NumPy: count() functioncount() function returns an array with the number of non-overlapping occurrences of substring sub in the range [start, end]. Input an array_like of string or unicode.


1 Answers

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.

like image 187
Sven Marnach Avatar answered Sep 30 '22 08:09

Sven Marnach