lemmE
lemmE

Reputation: 31

Python Max/Position, array [[]]

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)

Upvotes: 1

Views: 839

Answers (2)

user13959036
user13959036

Reputation:

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

Upvotes: 0

Michaelborn
Michaelborn

Reputation: 26

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)

Upvotes: 0

Related Questions