numpy.count_nonzero(a, axis=None)
[source]
Counts the number of non-zero values in the array a
.
The word “non-zero” is in reference to the Python 2.x built-in method __nonzero__()
(renamed __bool__()
in Python 3.x) of Python objects that tests an object’s “truthfulness”. For example, any number is considered truthful if it is nonzero, whereas any string is considered truthful if it is not the empty string. Thus, this function (recursively) counts how many elements in a
(and in sub-arrays thereof) have their __nonzero__()
or __bool__()
method evaluated to True
.
Parameters: |
|
---|---|
Returns: |
|
See also
nonzero
>>> np.count_nonzero(np.eye(4)) 4 >>> np.count_nonzero([[0,1,7,0,0],[3,0,0,2,19]]) 5 >>> np.count_nonzero([[0,1,7,0,0],[3,0,0,2,19]], axis=0) array([1, 1, 1, 1, 1]) >>> np.count_nonzero([[0,1,7,0,0],[3,0,0,2,19]], axis=1) array([2, 3])
© 2005–2019 NumPy Developers
Licensed under the 3-clause BSD License.
https://docs.scipy.org/doc/numpy-1.17.0/reference/generated/numpy.count_nonzero.html