Python 有效地计算numpy数组中的零元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42916330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:15:48  来源:igfitidea点击:

Efficiently count zero elements in numpy array?

pythonarraysperformancenumpymultidimensional-array

提问by Gabriel

I need to count the number of zero elements in numpyarrays. I'm aware of the numpy.count_nonzerofunction, but there appears to be no analog for counting zero elements.

我需要计算numpy数组中零元素的数量。我知道numpy.count_nonzero函数,但似乎没有用于计算零元素的模拟。

My arrays are not very large (typically less than 1E5 elements) but the operation is performed several millions of times.

我的数组不是很大(通常少于 1E5 个元素),但该操作执行了数百万次。

Of course I could use len(arr) - np.count_nonzero(arr), but I wonder if there's a more efficient way to do it.

当然我可以使用len(arr) - np.count_nonzero(arr),但我想知道是否有更有效的方法来做到这一点。

Here's a MWE of how I do it currently:

这是我目前如何做的 MWE:

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))

回答by kmario23

A 2xfaster approach would be to just use np.count_nonzero()but with the conditionas needed.

一种快2倍的方法是仅使用np.count_nonzero()但根据需要使用条件

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

You can also use np.where()but it's slower than np.count_nonzero()

您也可以使用,np.where()但它比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