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.
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.
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.
Input array.
Indices of elements that are non-zero.
Return the indices of the elements that are non-zero.
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.
>>> x = np.array([[3, 0, 0], [0, 4, 0], [5, 6, 0]]) # doctest: +SKIPThis example is valid syntax, but we were not able to check execution
... x # doctest: +SKIP array([[3, 0, 0], [0, 4, 0], [5, 6, 0]])
>>> 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.
>>> a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # doctest: +SKIPThis example is valid syntax, but we were not able to check execution
... a > 3 # doctest: +SKIP array([[False, False, False], [ True, True, True], [ True, True, True]])
>>> 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:
>>> 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.
>>> (a > 3).nonzero() # doctest: +SKIP (array([1, 1, 1, 2, 2, 2]), array([0, 1, 2, 0, 1, 2]))See :
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
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