count_nonzero#

ivy.count_nonzero(a, /, *, axis=None, keepdims=False, dtype=None, out=None)[source]#

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

Parameters:
  • a (Union[Array, NativeArray]) – array for which to count non-zeros.

  • axis (Optional[Union[int, Tuple[int, ...]]]) – optional axis or tuple of axes along which to count non-zeros. Default is (default: None) None, meaning that non-zeros will be counted along a flattened version of the input array.

  • keepdims (bool) – optional, if this is set to True, the axes that are counted are left in the (default: False) result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

  • dtype (Optional[Union[Dtype, NativeDtype]]) – optional output dtype. Default is of type integer. (default: None)

  • out (Optional[Union[Array, NativeArray]]) – optional output array, for writing the result to. (default: None)

Return type:

Array

Returns:

ret – 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.

Examples

>>> a = ivy.array([[0, 1, 2, 3],[4, 5, 6, 7]])
>>> ivy.count_nonzero(a)
ivy.array(7)
>>> a = ivy.array([[0, 1, 2, 3],[4, 5, 6, 7]])
>>> ivy.count_nonzero(a, axis=0)
ivy.array([1, 2, 2, 2])
>>> a = ivy.array([[[0,1],[2,3]],[[4,5],[6,7]]])
>>> ivy.count_nonzero(a, axis=(0,1), keepdims=True)
ivy.array([[[3, 4]]])
Array.count_nonzero(self, /, *, axis=None, keepdims=False, dtype=None, out=None)#

ivy.Array instance method variant of ivy.count_nonzero. This method simply wraps the function, and so the docstring for ivy.count_nonzero also applies to this method with minimal changes.

Parameters:
  • self (Array) – input array for which to count non-zeros.

  • axis (Optional[Union[int, Tuple[int, ...]]]) – optional axis or tuple of axes along which to count non-zeros. Default is (default: None) None, meaning that non-zeros will be counted along a flattened version of the input array.

  • keepdims (bool) – optional, if this is set to True, the axes that are counted are left in the (default: False) result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

  • dtype (Optional[Union[Dtype, NativeDtype]]) – optional output dtype. Default is of type integer. (default: None)

  • out (Optional[Array]) – optional output array, for writing the result to. (default: None)

Return type:

Array

Returns:

ret – 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.

Examples

>>> x = ivy.array([1, 2, 3])
>>> x.count_nonzero()
ivy.array(3)
>>> x = ivy.array([[[0,1],[2,3]],[[4,5],[6,7]]])
>>> x.count_nonzero(axis=0)
ivy.array([[1, 2],
       [2, 2]])
>>> x = ivy.array([[[0,1],[2,3]],[[4,5],[6,7]]])
>>> x.count_nonzero(axis=(0,1), keepdims=True)
ivy.array([[[3, 4]]])
Container.count_nonzero(self, /, *, axis=None, keepdims=False, dtype=None, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)#

ivy.Container instance method variant of ivy.count_nonzero. This method simply wraps the function, and so the docstring for ivy.count_nonzero also applies to this method with minimal changes.

Parameters:
  • self (Container) – container with the base input arrays.

  • axis (Optional[Union[int, Tuple[int, ...]]]) – optional axis or tuple of axes along which to count non-zeros. Default is (default: None) None, meaning that non-zeros will be counted along a flattened version of the input array.

  • keepdims (bool) – optional, if this is set to True, the axes that are counted are left in the (default: False) result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

  • dtype (Optional[Union[Dtype, NativeDtype]]) – optional output dtype. Default is of type integer. (default: None)

  • key_chains (Optional[Union[List[str], Dict[str, str]]]) – The key-chains to apply or not apply the method to. Default is None. (default: None)

  • to_apply (bool) – If True, the method will be applied to key_chains, otherwise key_chains (default: True) will be skipped. Default is True.

  • prune_unapplied (bool) – Whether to prune key_chains for which the function was not applied. (default: False) Default is False.

  • map_sequences (bool) – Whether to also map method to sequences (lists, tuples). (default: False) Default is False

  • out (Optional[Container]) – optional output container, for writing the result to. (default: None)

Return type:

Container

Returns:

ret – Container including number of non-zero values in the array along a given axis. Otherwise, container with the total number of non-zero values in the array is returned.

Examples

>>> x = ivy.Container(a=ivy.array([[0, 1, 2, 3],[4, 5, 6, 7]]),                        b=ivy.array([[[0,1],[2,3]],[[4,5],[6,7]]]))
>>> x.count_nonzero()
{
    a: ivy.array(7),
    b: ivy.array(7)
}
>>> x = ivy.Container(a=ivy.array([[0, 1, 2, 3],[4, 5, 6, 7]]),                        b=ivy.array([[[0,1],[2,3]],[[4,5],[6,7]]]))
>>> x.count_nonzero(axis=0)
{
    a: ivy.array([1, 2, 2, 2]),
    b: ivy.array([[1, 2],
                  [2, 2]])
}
>>> x = ivy.Container(a=ivy.array([[0, 1, 2, 3],[4, 5, 6, 7]]),                        b=ivy.array([[[0,1],[2,3]],[[4,5],[6,7]]]))
>>> x.count_nonzero(axis=(0,1), keepdims=True)
{
    a: ivy.array([[7]]),
    b: ivy.array([[[3, 4]]])
}