nlcpy.count_nonzero
- nlcpy.count_nonzero(a, axis=None, keepdims=False)[ソース]
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 toTrue
.- Parameters
- aarray_like
The array for which to count non-zeros.
- axisint or tuple, optional
Axis 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
.- keepdimsbool, optional
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.
- Returns
- countint or array of int
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.
参考
nonzero
Returns the indices of the elements that are non-zero.
Examples
>>> import nlcpy as vp >>> vp.count_nonzero(vp.eye(4)) array(4) >>> a = vp.array([[0, 1, 7, 0], [3, 0, 2, 19]]) >>> vp.count_nonzero(a) array(5) >>> vp.count_nonzero(a, axis=0) array([1, 1, 2, 1]) >>> vp.count_nonzero(a, axis=1) array([2, 3]) >>> vp.count_nonzero(a, axis=1, keepdims=True) array([[2], [3]])