NumPy | flatnonzero method
flatnonzero(~) method returns the indices of non-zero values in the flattened version of the input array.
The input array.
A Numpy array of indices of non-zero values in the flattened version of the input array.
One-dimensional arrays are already flat, so this is equivalent to just applying the
np.flatnonzero([5,3,0,2])array([0, 1, 3])
Here, the indices 0, 1 and 3 are returned since the values 5, 3 and 2 are non-zeros.
Suppose we had a 2D array:
np.flatnonzero([[1,0],[2,3]])array([0, 2, 3])
Here, the 2D array is first flattened out to
[1,0,2,3], and then the indices of the non-zero value are extracted.