Jack Jack - 21 days ago 5
Python Question

Extract indices of minimum in numpy array

There are lots of similar questions to this but none of them seemed to help me. I want to return the indices for the minimum value in a numpy array. Say for instance the array was:

[[4,3,5,1]
[2,6,5,1]
[8,3,2,4]]


I would like my program to return (0,3). I have tried using argmin but have made next to no progress with it.

Note, I only want to return one set of indices. I.E not (0,3) AND (1,3) in the above example.

Any help would be much appreciated,

Jack

Answer

Use unravel_index:

arr = np.array([[4,3,5,1],[2,6,5,1],[8,3,2,4]])
index = np.unravel_index(arr.argmin(), arr.shape)
# (0, 3)