dpnp.count_nonzero

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

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

For full documentation refer to numpy.count_nonzero.

Parameters:
  • a ({dpnp.ndarray, usm_ndarray}) -- The array for which to count non-zeros.

  • axis ({None, int, tuple}, optional) -- Axis or tuple of axes along which to count non-zeros. Default value means that non-zeros will be counted along a flattened version of a. Default: None.

  • keepdims (bool, 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. Default: False.

  • out ({None, dpnp.ndarray, usm_ndarray}, optional) -- The array into which the result is written. The data type of out must match the expected shape and the expected data type of the result. If None then a new array is returned. Default: None.

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

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]])