numpy 1.22.4 Pypi GitHub Homepage
Other Docs
ParametersReturns
nonzero(self)

Returns a tuple of arrays, one for each dimension, containing the indices of the non-zero elements in that dimension. The corresponding non-zero values can be obtained with:

a[a.nonzero()]

To group the indices by element, rather than dimension, use instead:

np.transpose(a.nonzero())

The result of this is always a 2d array, with a row for each non-zero element.

Parameters

None :

Returns

tuple_of_arrays : tuple

Indices of elements that are non-zero.

Return the indices of unmasked elements that are not 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.

numpy.ndarray.nonzero

Equivalent ndarray method.

numpy.nonzero

Function operating on ndarrays.

Examples

This example is valid syntax, but we were not able to check execution
>>> import numpy.ma as ma
... x = ma.array(np.eye(3))
... x masked_array( data=[[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]], mask=False, fill_value=1e+20)
This example is valid syntax, but we were not able to check execution
>>> x.nonzero()
(array([0, 1, 2]), array([0, 1, 2]))

Masked elements are ignored.

This example is valid syntax, but we were not able to check execution
>>> x[1, 1] = ma.masked
... x masked_array( data=[[1.0, 0.0, 0.0], [0.0, --, 0.0], [0.0, 0.0, 1.0]], mask=[[False, False, False], [False, True, False], [False, False, False]], fill_value=1e+20)
This example is valid syntax, but we were not able to check execution
>>> x.nonzero()
(array([0, 2]), array([0, 2]))

Indices can also be grouped by element.

This example is valid syntax, but we were not able to check execution
>>> np.transpose(x.nonzero())
array([[0, 0],
       [2, 2]])

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, ma.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 = ma.array([[1,2,3],[4,5,6],[7,8,9]])
... a > 3 masked_array( data=[[False, False, False], [ True, True, True], [ True, True, True]], mask=False, fill_value=True)
This example is valid syntax, but we were not able to check execution
>>> ma.nonzero(a > 3)
(array([1, 1, 1, 2, 2, 2]), array([0, 1, 2, 0, 1, 2]))

The nonzero method of the condition array can also be called.

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

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/ma/core.py#4889
type: <class 'function'>
Commit: