I have following numpy array:
a = array([[0.25077832, 0.42227767, 0.43744429],
[0.28539526, 0.44163316, 0.40298769],
[0.35807141, 0.2856717 , 0.33536935],
[0.55462028, 0.53807624, 0.38644028],
[0.18301549, 0.26485082, 0.1366992 ],
[0.26986122, 0.41347949, 0.43940707],
[0.25237023, 0.25293316, 0.32400949],
[0.73319735, 0.70901891, 0.42607705],
[0.43295485, 0.57804534, 0.48060421],
[0.32985147, 0.40481195, 0.27833032],
[0.70750743, 0.65494225, 0.31475339],
[0.31292153, 0.28715622, 0.37811341],
[1. , 0.88283385, 0.42551691],
[0.59371996, 0.58705522, 0.43142012],
[0.73690667, 0.63809236, 0.34826675],
[0.36728384, 0.48038203, 0.52019776],
[0.67126132, 0.56541162, 0.39211614],
[0.13064738, 0.17167055, 0.11997984],
[0.41040586, 0.38921193, 0.37591999],
[0.25267195, 0.30835439, 0.34994184],
[0.62562474, 0.57021446, 0.34875994],
[0.88283382, 1. , 0.44805126],
[0.45318085, 0.58569116, 0.51384947],
[0.23267012, 0.4015728 , 0.41292012],
[0.5935659 , 0.62583221, 0.36990236],
[0.62760122, 0.55182008, 0.44280949],
[0.55419133, 0.48549239, 0.23645478],
[0.66452635, 0.6102609 , 0.4181133 ]])
When i try a.argmax()
i get 36 and this is correct, because 12
row consist 36
element with value 1.
. But i need index 0
which mean 0
element in 12
row
expected output: (12, 0)
CodePudding user response:
IIUC, you can use np.divmod
:
np.divmod(a.argmax(), a.shape[1])
or np.nditer
:
list(np.ndindex(a.shape))[a.argmax()]
Or, as suggested by @Ali_Sh:
np.unravel_index
that will work with any number of dimensions!
np.unravel_index(a.argmax(), a.shape)
output: (12, 0)