numpy 1.22.4 Pypi GitHub Homepage
Other Docs
NotesParametersReturnsBackRef
nonzero(a)

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

>>> x = np.array([[3, 0, 0], [0, 4, 0], [5, 6, 0]])
... x array([[3, 0, 0], [0, 4, 0], [5, 6, 0]])
>>> np.nonzero(x)
(array([0, 1, 2, 2]), array([0, 1, 0, 1]))
>>> x[np.nonzero(x)]
array([3, 4, 5, 6])
>>> np.transpose(np.nonzero(x))
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.

>>> a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
... a > 3 array([[False, False, False], [ True, True, True], [ True, True, True]])
>>> np.nonzero(a > 3)
(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:

>>> a[np.nonzero(a > 3)]
array([4, 5, 6, 7, 8, 9])
>>> a[a > 3]  # prefer this spelling
array([4, 5, 6, 7, 8, 9])

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

>>> (a > 3).nonzero()
(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.

numpy.ma.core.where numpy.isin dask.array.core.Array.nonzero numpy.core._multiarray_umath.where numpy.count_nonzero numpy.where numpy.flatnonzero numpy.ma.core.MaskedArray.nonzero dask.array.routines.nonzero numpy.argwhere skimage.morphology._flood_fill.flood numpy.ma.core.nonzero skimage.transform.finite_radon_transform.ifrt2

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


GitHub : /numpy/core/fromnumeric.py#1866
type: <class 'function'>
Commit: