有效地计数numpy数组中的零个元素? [英] Efficiently count zero elements in numpy array?
问题描述
我需要计算numpy
数组中零个元素的数量.我知道 numpy.count_nonzero 函数,但似乎没有任何模拟可以计数零个元素.
I need to count the number of zero elements in numpy
arrays. I'm aware of the numpy.count_nonzero function, but there appears to be no analog for counting zero elements.
我的数组不是很大(通常少于1E5个元素),但是该操作执行了数百万次.
My arrays are not very large (typically less than 1E5 elements) but the operation is performed several millions of times.
我当然可以使用len(arr) - np.count_nonzero(arr)
,但是我想知道是否有更有效的方法.
Of course I could use len(arr) - np.count_nonzero(arr)
, but I wonder if there's a more efficient way to do it.
这是我目前的操作方式:
Here's a MWE of how I do it currently:
import numpy as np
import timeit
arrs = []
for _ in range(1000):
arrs.append(np.random.randint(-5, 5, 10000))
def func1():
for arr in arrs:
zero_els = len(arr) - np.count_nonzero(arr)
print(timeit.timeit(func1, number=10))
推荐答案
A 2x faster approach would be to just use np.count_nonzero()
but with the condition as needed.
In [3]: arr
Out[3]:
array([[1, 2, 0, 3],
[3, 9, 0, 4]])
In [4]: np.count_nonzero(arr==0)
Out[4]: 2
In [5]:def func_cnt():
for arr in arrs:
zero_els = np.count_nonzero(arr==0)
# here, it counts the frequency of zeroes actually
您还可以使用 np.where()
> ,但比 np.count_nonzero()
You can also use np.where()
but it's slower than np.count_nonzero()
In [6]: np.where( arr == 0)
Out[6]: (array([0, 1]), array([2, 2]))
In [7]: len(np.where( arr == 0))
Out[7]: 2
效率:(降序排列)
Efficiency: (in descending order)
In [8]: %timeit func_cnt()
10 loops, best of 3: 29.2 ms per loop
In [9]: %timeit func1()
10 loops, best of 3: 46.5 ms per loop
In [10]: %timeit func_where()
10 loops, best of 3: 61.2 ms per loop
这篇关于有效地计数numpy数组中的零个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!