Jack Jack - 1 year ago 168
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:


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,


Answer Source

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download