[英]Counting number of elements greater than a certain value in a numy.ndarray
I want to calculate the count of number of elements in a numpy.ndarry which is greater than a certain value.我想计算 numpy.ndarry 中大于某个值的元素数。 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
.是数组,我想要大于
0.19214945092486838
的元素数。 Here the value will be 21. How to calculate it?这里的值为 21。如何计算?
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
输出为:
21
ar[ar>0.19214945092486838]
will provide you list of elements which are higher than the current values. ar[ar>0.19214945092486838]
将为您提供高于当前值的元素列表。 You can take len
to get the length你可以拿
len
来得到长度
>>> 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
Here's one way这是一种方法
my_array = ... the target array ...
result = sum(0.19214945092486838 < x for x in my_array)
With numpy you can try:使用 numpy,您可以尝试:
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)
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.现在用您的值替换
data
,并使用(data > 0.19214945092486838)
代替。
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)))
Cleanest way (IMHO):最干净的方式(恕我直言):
x > 1
will transform your array x
into a boolean one, where elements larger than 1 are True. x > 1
会将数组x
转换为布尔数组,其中大于 1 的元素为 True。 Then you can count the True values by np.count_nonzero()
然后你可以通过
np.count_nonzero()
计算 True 值
Thus, np.count_nonzero(x > 1)
因此,
np.count_nonzero(x > 1)
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]
you can use len to count results like this example:您可以使用 len 来计算这样的结果:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.