Return the indices of the elements that are non-zero.
Returns a tuple of arrays, one for each dimension of a, containing the indices of the non-zero elements in that dimension. The corresponding non-zero values can be obtained with:
To group the indices by element, rather than dimension, use:
The result of this is always a 2-D array, with a row for each non-zero element.
Parameters : | a : array_like
|
---|---|
Returns : | tuple_of_arrays : tuple
|
See also
-
flatnonzero
- Return indices that are non-zero in the flattened version of the input array. ndarray.nonzero
- Equivalent ndarray method. count_nonzero
- Counts the number of non-zero elements in the input array.
Examples
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.
The nonzero method of the boolean array can also be called.