dask.array.count_nonzero

Contents

dask.array.count_nonzero#

dask.array.count_nonzero(a, axis=None)[source]#

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

This docstring was copied from numpy.count_nonzero.

Some inconsistencies with the Dask version may exist.

A non-zero value is one that evaluates to truthful in a boolean context, including any non-zero number and any string that is not empty. This function recursively counts how many elements in a (and its sub-arrays) are non-zero values.

Parameters:
aarray_like

The array for which to count non-zeros.

axisint or 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 a.

keepdimsbool, optional (Not supported in Dask)

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.

Returns:
countint or array of int

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.

See also

nonzero

Return the coordinates of all the non-zero values.

Examples

>>> import numpy as np
>>> np.count_nonzero(np.eye(4))
np.int64(4)
>>> a = np.array([[0, 1, 7, 0],
...               [3, 0, 2, 19]])
>>> np.count_nonzero(a)
np.int64(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]])