dpnp.count_nonzero

dpnp.count_nonzero(a, axis=None, *, keepdims=False)[source]

Counts the number of non-zero values in the array a.

For full documentation refer to numpy.count_nonzero.

Returns:

out -- Number of non-zero values in the array along a given axis. Otherwise, a zero-dimensional array with the total number of non-zero values in the array is returned.

Return type:

dpnp.ndarray

Limitations

Parameters a is supported as either dpnp.ndarray or dpctl.tensor.usm_ndarray. Otherwise TypeError exception will be raised. Input array data types are limited by supported DPNP Available array data types.

See also

dpnp.nonzero

Return the coordinates of all the non-zero values.

Examples

>>> import dpnp as np
>>> np.count_nonzero(np.eye(4))
array(4)
>>> a = np.array([[0, 1, 7, 0],
                  [3, 0, 2, 19]])
>>> np.count_nonzero(a)
array(5)
>>> np.count_nonzero(a, axis=0)
array([1, 1, 2, 1])
>>> np.count_nonzero(a, axis=1)
array([2, 3])
>>> np.count_nonzero(a, axis=1, keepdims=True)
array([[2],
       [3]])