Reputation: 33
I want to calculate the count of number of elements in a numpy.ndarry which is greater than a certain value. How do I get the required results?
For example:
[[0.25656927 0.31030828 0.23430803 0.25999823 0.20450112 0.19383106
0.35779405 0.36355627 0.16837767 0.1933686 0.20630316 0.17804974
0.06902786 0.26209944 0.21310201 0.12016498 0.14213449 0.16639964
0.33461425 0.15897344 0.20293266 0.14630634 0.2509769 0.17211646
0.3922994 0.14036047 0.12571093 0.25565785 0.18216616 0.0728473
0.25328827 0.1476636 0.1873344 0.12253726 0.16082433 0.20678088
0.33296013 0.03104548 0.14949016 0.05495472 0.1494042 0.32033417
0.05361898 0.14325878 0.16196126 0.15796155 0.10990247 0.14499696]]
is the array and I want the count of number of elements greater than 0.19214945092486838
.
Here the value will be 21. How to calculate it?
Upvotes: 1
Views: 4792
Reputation: 686
you can use len to count results like this example:
import numpy as np
matrix = np.array([[0.25656927,0.31030828,0.23430803,0.25999823,0.20450112,0.19383106,
0.35779405, 0.36355627, 0.16837767, 0.1933686, 0.20630316, 0.17804974,
0.06902786, 0.26209944, 0.21310201, 0.12016498, 0.14213449, 0.16639964,
0.33461425, 0.15897344, 0.20293266, 0.14630634, 0.2509769, 0.17211646,
0.3922994, 0.14036047, 0.12571093, 0.25565785, 0.18216616, 0.0728473,
0.25328827, 0.1476636, 0.1873344, 0.12253726, 0.16082433, 0.20678088,
0.33296013, 0.03104548, 0.14949016, 0.05495472, 0.1494042, 0.32033417,
0.05361898, 0.14325878, 0.16196126, 0.15796155, 0.10990247, 0.14499696]])
n = len(matrix[matrix > 0.18])
print(n)
Upvotes: 0
Reputation: 271
arr=np.array([0.25656927,0.31030828,0.23430803,0.25999823,0.20450112,0.19383106,
0.35779405, 0.36355627, 0.16837767, 0.1933686, 0.20630316, 0.17804974,
0.06902786, 0.26209944, 0.21310201, 0.12016498, 0.14213449, 0.16639964,
0.33461425, 0.15897344, 0.20293266, 0.14630634 ,0.2509769 , 0.17211646,
0.3922994 , 0.14036047, 0.12571093, 0.25565785, 0.18216616, 0.0728473,
0.25328827, 0.1476636 , 0.1873344 , 0.12253726, 0.16082433, 0.20678088,
0.33296013, 0.03104548, 0.14949016, 0.05495472, 0.1494042 , 0.32033417,
0.05361898, 0.14325878 ,0.16196126, 0.15796155, 0.10990247, 0.14499696])
Count:
arr[np.where(arr>0.19214945092486838)].shape[0]
Upvotes: 0
Reputation: 2408
Cleanest way (IMHO):
x > 1
will transform your array x
into a boolean one, where elements larger than 1 are True. Then you can count the True values by np.count_nonzero()
Thus, np.count_nonzero(x > 1)
Upvotes: 0
Reputation: 339
The following snippet of code will achieve what you desire :)
import numpy as np
arrayToCheck=np.array([0.25656927, 0.31030828, 0.23430803, 0.25999823, 0.20450112, 0.19383106,
0.35779405, 0.36355627, 0.16837767, 0.1933686, 0.20630316, 0.17804974,
0.06902786, 0.26209944, 0.21310201, 0.12016498, 0.14213449, 0.16639964,
0.33461425, 0.15897344, 0.20293266, 0.14630634, 0.2509769, 0.17211646,
0.3922994, 0.14036047, 0.12571093, 0.25565785, 0.18216616, 0.0728473,
0.25328827, 0.1476636, 0.1873344, 0.12253726, 0.16082433, 0.20678088,
0.33296013, 0.03104548, 0.14949016, 0.05495472, 0.1494042, 0.32033417,
0.05361898, 0.14325878, 0.16196126, 0.15796155, 0.10990247, 0.14499696])
print ("The number of float numbers above your threshold is " + str(np.sum(a>0.19214945092486838)))
Upvotes: 0
Reputation: 321
To get an array of which the item is greater than / less than:
>>> import numpy as np
>>> data = np.arange(12)
>>> data > 5
array([False, False, False, False, False, False, True, True, True,
True, True, True])
Then you just have to find the sum of the array:
>>> (data > 5).sum()
6
Now substitude data
with your values, and use (data > 0.19214945092486838)
instead.
Upvotes: 0
Reputation: 2819
With numpy you can try:
Myarray= [ [ your array]]
Value_to_search=0.19214945092486838
Array_greater_than=Myarray>Value_to_search
Nb_Val_greater_than=Array_greater_than.sum()
print(Nb_Val_greater_than)
Upvotes: 0
Reputation: 66
ar[ar>0.19214945092486838]
will provide you list of elements which are higher than the current values. You can take len
to get the length
>>> import numpy as np
>>> ar = np.array([0.25656927,0.31030828,0.23430803,0.25999823,0.20450112,0.19383106,0.35779405,0.36355627,0.16837767,0.1933686,0.20630316,0.17804974 ,0.06902786,0.26209944,0.21310201,0.12016498,0.14213449,0.16639964,0.33461425,0.15897344,0.20293266,0.14630634,0.2509769,0.17211646 ,0.3922994,0.14036047,0.12571093,0.25565785,0.18216616,0.0728473,0.25328827,0.1476636,0.1873344,0.12253726,0.16082433,0.20678088 ,0.33296013,0.03104548,0.14949016,0.05495472,0.1494042,0.32033417,0.05361898,0.14325878,0.16196126,0.15796155,0.10990247,0.14499696])
>>> print(len(ar[ar>0.19214945092486838]))
>>> 21
Upvotes: 1
Reputation: 1594
Here's one way
my_array = ... the target array ...
result = sum(0.19214945092486838 < x for x in my_array)
Upvotes: 0
Reputation: 1279
You can simply do:
import numpy
arr = numpy.asarray([0.25656927, 0.31030828, 0.23430803, 0.25999823, 0.20450112, 0.19383106, 0.35779405, 0.36355627, 0.16837767, 0.1933686, 0.20630316, 0.17804974, 0.06902786, 0.26209944, 0.21310201, 0.12016498, 0.14213449, 0.16639964, 0.33461425, 0.15897344, 0.20293266, 0.14630634, 0.2509769, 0.17211646, 0.3922994, 0.14036047, 0.12571093, 0.25565785, 0.18216616, 0.0728473, 0.25328827, 0.1476636, 0.1873344, 0.12253726, 0.16082433, 0.20678088, 0.33296013, 0.03104548, 0.14949016, 0.05495472, 0.1494042, 0.32033417, 0.05361898, 0.14325878, 0.16196126, 0.15796155, 0.10990247, 0.14499696])
print((arr > 0.19214945092486838).sum())
The output is: 21
Upvotes: 2