Numpy tips: how to check whether a numpy array is all 0?

Posted by qads on Sat, 16 Oct 2021 08:31:01 +0200

catalogue

0 summary

1 numpy.any()

2 numpy.count_nonzero()

3 numpy.all()  

4. Multidimensional arrays can be judged by axis

0 summary

         This paper briefly introduces several test methods to judge whether numpy array is all zero.

1 numpy.any()

        The numpy.any() function is used to check whether there are any non-0 elements in a numpy number. Therefore, the result of numpy.any() is reversed to obtain the result of "whether all numpy arrays are 0". For example:       

import numpy as np

print('Using numpy.any()...')
a_1D = np.zeros(5)
print('Is a_1D all zeros?: ', not(np.any(a_1D)))
print('Is a_1D all zeros?: ', ~(np.any(a_1D)))
a_1D[2] = -1
print('Is a_1D all zeros?: ', not(np.any(a_1D)))

a_2D = np.zeros((2,3))
print(a_2D)
print('Is a_2D all zeros?: ', not(np.any(a_2D)))
a_2D[1,2] = 0.1
print('Is a_2D all zeros?: ', not(np.any(a_2D)))

Output results:

        Using numpy.any()...
        Is a_1D all zeros?:  True
        Is a_1D all zeros?:  True
        Is a_1D all zeros?:  False
        [[0. 0. 0.]
         [0. 0. 0.]]
        Is a_2D all zeros?:  True
        Is a_2D all zeros?:  False

         Note that logical negation in python can use "~" or "not", but not "!" ("!" also means negation in C and verilog, but it has other functions in python). However, "~" and "not" are different. See section 4 below.

2 numpy.count_nonzero()

       numpy.count_nonzero() is used to count the number of 0 elements in the array, so it can also be used to judge whether all 0 elements are zero. The usage is as follows:

print('Using numpy.nonzero()...')
a = np.array([1,2,3,0,0,1])
print('Number of zeros in a = ',np.count_nonzero(a))
print('Is a all zeros?: ', np.count_nonzero(a)==0)
a[:] = 0 # Force a to all-zeros array
print('Is a all zeros?: ', np.count_nonzero(a)==0)
print('Is a all zeros?: ', not np.count_nonzero(a))

        Using numpy.nonzero()...
        Number of zeros in a =  4
        Is a all zeros?:  False
        Is a all zeros?:  True
        Is a all zeros?:  True 

 

3 numpy.all()  

        You can also do this with numpy.all(). The following example takes advantage of python's internal automatic conversion of 0 -- false and 1 -- true.

print('')
print('Using numpy.all()...')
a = np.zeros(10)
print('Is a all zeros?: ', np.all(a==0))

        Using numpy.all()...
        Is a all zeros?:  True 

4. Multidimensional arrays can be judged by axis

        For multi-dimensional arrays (which is where numpy really exerts its strength), the above functions judge the whole array uniformly by default, but they can also be processed along the specified axis through the axis parameter. As shown in the following example:

print('')
print('Judge according to the specified axis')
a_2D = np.zeros((2,3))
a_2D[1,2] = 0.1
print(a_2D)
print('Is each col of a_2D all zeros?: ', ~(np.any(a_2D, axis=0)))
print('Is each row of a_2D all zeros?: ', ~(np.any(a_2D, axis=1)))

Judge according to the specified axis
[[0.  0.  0. ]
 [0.  0.  0.1]]
Is each col of a_2D all zeros?:  [ True  True False]
Is each row of a_2D all zeros?:  [ True False]

         When axis=0 is specified, it is equivalent to judging whether all 2-dimensional arrays are 0 by column; when axis=1 is specified, it is equivalent to judging whether all 2-dimensional arrays are 0 by row. Of course, the concept of row and column here is inherited from the traditional 2-dimensional array or matrix. When considering higher-dimensional array, the concept of row and column is no longer applicable. The axis of high-dimensional arrays (also known as tensors) will be introduced in another article.

        In addition, as mentioned earlier, "~" and "not" representing logical negation are different. Specifically, not only accepts one operand. Therefore, in the above example, if "~" is changed to not, an error will be reported, as shown below:

print('Is each col of a_2D all zeros?: ', not(np.any(a_2D, axis=0)))
print('Is each row of a_2D all zeros?: ', not(np.any(a_2D, axis=1)))

         Errors are reported as follows:  

         And "~" is the so-called Bitwise NOT   operator.

        If the input of "~" is an integer, it will reverse all bits of the input number. If it is a numpy array, the bitwise logical negation operation will be performed on each number. If it is an array of numpy Boolean types (True, False), the logical negation operation will be performed on each Boolean number -- this is the use in the above example.

 

Topics: Python numpy