numpy.count_nonzero¶
numpy.count_nonzero[a, axis=None, *, keepdims=False][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 objects 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.
Parametersaarray_likeThe array for which to count non-zeros.
axisint or tuple, optionalAxis or tuple of axes along which to count non-zeros. Default is None, meaning that non-zeros will be counted along a flattened version of a.
New in version 1.12.0.
If this is set to True, the axes that are counted are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.
New in version 1.19.0.
Number of non-zero values in the array along a given axis. Otherwise, the total number of non-zero values in the array is returned.
See also
nonzeroReturn the coordinates of all the non-zero values.
Examples