mindspore.numpy.count_nonzero

View Source On AtomGit
mindspore.numpy.count_nonzero(x, axis=None, keepdims=False)[source]

Counts the number of non-zero values in the tensor x.

Parameters:
  • x (Tensor) – The tensor for which to count non-zeros.

  • axis (Union[int,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 x.

  • keepdims (bool, optional) – If 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 x. Default: False .

Returns:

Tensor, indicating number of non-zero values in x along a given axis. Otherwise, the total number of non-zero values in x is returned.

Raises:
  • TypeError – If axis is not int or tuple.

  • ValueError – If axis is not in range [-x.ndim, x.ndim).

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore.numpy as np
>>> x = np.asarray([1, 2, 3, -4, 0, 3, 2, 0])
>>> output = np.count_nonzero(x)
>>> print(output)
6