python - Indicies or numpy array to tuple of integers? -


i trying tuple contains indicies of minimum value in numpy array.

import numpy np a=np.array(([2,3,1],[5,4,6],[8,7,9])) b=np.where(a==np.min(a)) print(b) 

gives:

(array([0]),array([2])) 

attempting map result tuple:

c=map(tuple,b) print(c) 

gives:

[(0,), (2,)] 

but want is:

(0,2) 

any suggestions other np.where acceptable. thanks.

the easiest way desired result is

>>> np.unravel_index(a.argmin(), a.shape) (0, 2) 

the argmin() method finds index of minimum element in flattened array in single pass, , more efficient first finding minimum , using linear search find index of minimum.

as second step, np.unravel_index() converts scalar index flattened array index tuple. note entries of index tuple have type np.int64 rather plain int.


Comments