1

I have an array. I got the max value with max = np.max(List). Now I need the position of the max value. I used this code:

x = np.where(a == a.max())

but it gives me that:

(array([0], dtype=int64), array([9], dtype=int64))

It contains the position but I don't know how to get it out of there. It would also be nice to know how to get the array below out of one of the two [].

array([[10.39219 , 12.018309, 13.810752, 10.646565, 13.779528, 13.29911 ,
        13.650783, 12.464462, 13.427543, 14.388401]], dtype=float32)
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
lemmE
  • 27
  • 4
  • Does this answer your question? [Get the position of the biggest item in a multi-dimensional numpy array](https://stackoverflow.com/questions/3584243/get-the-position-of-the-biggest-item-in-a-multi-dimensional-numpy-array) – mkrieger1 Sep 04 '20 at 19:50
  • `where` gives the coordinates as 2 arrays. It found one match at row `0` and column `9`. Read `np.nonzero` docs for more explanation. – hpaulj Sep 05 '20 at 00:00

2 Answers2

1

Methods:

  1. Use np.argmax:
List = np.array([10.39219 , 12.018309, 13.810752, 10.646565, 13.779528, 13.29911 , 13.650783, 12.464462, 13.427543, 14.388401])
_max = np.argmax(List)
_max
>>> 9
  1. Change to list and use max and index methods:
List = np.array([10.39219 , 12.018309, 13.810752, 10.646565, 13.779528, 13.29911 , 13.650783, 12.464462, 13.427543, 14.388401])
_max = List.tolist().index(max(List))
_max
>>> 9
0

use np.argmax() to get the index

arr = array([[10.39219 , 12.018309, 13.810752, 10.646565, 13.779528, 13.29911 , 13.650783, 12.464462, 13.427543, 14.388401]], dtype=float32)
np.argmax(arr)