dask 2021.10.0

NotesParametersReturnsBackRef
nonzero(a)

This docstring was copied from numpy.nonzero.

Some inconsistencies with the Dask version may exist.

Returns a tuple of arrays, one for each dimension of a, containing the indices of the non-zero elements in that dimension. The values in a are always tested and returned in row-major, C-style order.

To group the indices by element, rather than dimension, use argwhere , which returns a row for each non-zero element.

note

When called on a zero-d array or scalar, nonzero(a) is treated as nonzero(atleast_1d(a)) .

.. deprecated:: 1.17.0
    Use `atleast_1d` explicitly if this behavior is deliberate.

Notes

While the nonzero values can be obtained with a[nonzero(a)] , it is recommended to use x[x.astype(bool)] or x[x != 0] instead, which will correctly handle 0-d arrays.

Parameters

a : array_like

Input array.

Returns

tuple_of_arrays : tuple

Indices of elements that are non-zero.

Return the indices of the elements that are non-zero.

See Also

count_nonzero

Counts the number of non-zero elements in the input array.

flatnonzero

Return indices that are non-zero in the flattened version of the input array.

ndarray.nonzero

Equivalent ndarray method.

Examples

This example is valid syntax, but we were not able to check execution
>>> x = np.array([[3, 0, 0], [0, 4, 0], [5, 6, 0]])  # doctest: +SKIP
... x # doctest: +SKIP array([[3, 0, 0], [0, 4, 0], [5, 6, 0]])
This example is valid syntax, but we were not able to check execution
>>> np.nonzero(x)  # doctest: +SKIP
(array([0, 1, 2, 2]), array([0, 1, 0, 1]))
This example is valid syntax, but we were not able to check execution
>>> x[np.nonzero(x)]  # doctest: +SKIP
array([3, 4, 5, 6])
This example is valid syntax, but we were not able to check execution
>>> np.transpose(np.nonzero(x))  # doctest: +SKIP
array([[0, 0],
       [1, 1],
       [2, 0],
       [2, 1]])

A common use for nonzero is to find the indices of an array, where a condition is True. Given an array a, the condition a > 3 is a boolean array and since False is interpreted as 0, np.nonzero(a > 3) yields the indices of the a where the condition is true.

This example is valid syntax, but we were not able to check execution
>>> a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])  # doctest: +SKIP
... a > 3 # doctest: +SKIP array([[False, False, False], [ True, True, True], [ True, True, True]])
This example is valid syntax, but we were not able to check execution
>>> np.nonzero(a > 3)  # doctest: +SKIP
(array([1, 1, 1, 2, 2, 2]), array([0, 1, 2, 0, 1, 2]))

Using this result to index a is equivalent to using the mask directly:

This example is valid syntax, but we were not able to check execution
>>> a[np.nonzero(a > 3)]  # doctest: +SKIP
array([4, 5, 6, 7, 8, 9])
This example is valid syntax, but we were not able to check execution
>>> a[a > 3]  # prefer this spelling  # doctest: +SKIP
array([4, 5, 6, 7, 8, 9])

nonzero can also be called as a method of the array.

This example is valid syntax, but we were not able to check execution
>>> (a > 3).nonzero()  # doctest: +SKIP
(array([1, 1, 1, 2, 2, 2]), array([0, 1, 2, 0, 1, 2]))
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

dask.array.routines.count_nonzero dask.array.routines.where dask.array.routines.argwhere dask.array.routines.flatnonzero

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


File: /dask/array/routines.py#2052
type: <class 'function'>
Commit: