numpy.argwhere() in Python

numpy.argwhere() function is used to find the indices of array elements that are non-zero, grouped by element.

Syntax : numpy.argwhere(arr)

Parameters :
arr : [array_like] Input array.



Return : [ndarray] Indices of elements that are non-zero. Indices are grouped by element.

Code #1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program explaining
# argwhere() function
   
import numpy as geek
  
# input array
in_arr = [[ 2, 0, 7], [ 0, 5, 9]]
print ("Input array : ", in_arr) 
  
out_arr = geek.argwhere(in_arr)
print ("Output indices of non zero array element: \n", out_arr)

chevron_right


Output:

Input array :  [[2, 0, 7], [0, 5, 9]]
Output indices of non zero array element: 
 [[0 0]
 [0 2]
 [1 1]
 [1 2]]

 
Code #2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program explaining
# argwhere() function
   
import numpy as geek
  
# input array
in_arr = geek.arange(8).reshape(4, 2)
print ("Input array : ", in_arr) 
  
out_arr = geek.argwhere(in_arr>4)
print ("Output indices greater than 4: \n", out_arr)

chevron_right


Output:

Input array :  [[0 1]
 [2 3]
 [4 5]
 [6 7]]
Output indices greater than 4: 
 [[2 1]
 [3 0]
 [3 1]]


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.