IUnknown IUnknown - 1 year ago 201
Python Question

Test if numpy array contains only zeros

We initialize a numpy array with zeros as bellow:


But how do we check whether all elements in a given n*n numpy array matrix is zero.

The method just need to return a True if all the values are indeed zero.

Answer Source

Check out numpy.count_nonzero.

>>> np.count_nonzero(np.eye(4))
>>> np.count_nonzero([[0,1,7,0,0],[3,0,0,2,19]])
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download