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, ...]]], default: None) – 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 the input array.

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

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

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

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)[source]#

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, ...]]], default: None) – 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 the input array.

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

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

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

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)[source]#

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, ...], Container]], default: None) – 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 the input array.

  • keepdims (Union[bool, Container], default: False) – 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.

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

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

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

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

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

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

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