Numpy Max python

is there a way to get the index of the maximum element in python?

in matlab we have [V,I] = max(a,[],3) it will give us all the matrices along the axis 3 and the index of each max element. is there a way to do it in Python???
dadadudeAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
yuk99Connect With a Mentor Commented:
You can use ARGMAX to get index of maximum value in an array. Use axis parameter to select dimension. Remember, that Python has 0-based indexing and in 3D array pages go first, then rows, then columns.

>>> from numpy import *
>>> from numpy.random import *
>>> a=rand(2,3,4)
>>> a
array([[[ 0.77719312,  0.07528051,  0.329186  ,  0.57896034],
        [ 0.23969721,  0.84188311,  0.03823846,  0.00302184],
        [ 0.3186661 ,  0.56970729,  0.42438946,  0.93728107]],

       [[ 0.50081337,  0.31282687,  0.973639  ,  0.9663504 ],
        [ 0.96829553,  0.47095946,  0.55364473,  0.4134808 ],
        [ 0.86336784,  0.18058616,  0.82078642,  0.35064008]]])
>>> a.max(axis=0)
array([[ 0.77719312,  0.31282687,  0.973639  ,  0.9663504 ],
       [ 0.96829553,  0.84188311,  0.55364473,  0.4134808 ],
       [ 0.86336784,  0.56970729,  0.82078642,  0.93728107]])
>>> ind = a.argmax(axis=0)
>>> ind
array([[0, 1, 1, 1],
       [1, 0, 1, 1],
       [1, 0, 1, 0]])

Open in new window

0
 
dadadudeAuthor Commented:
Great answer. Thank you
0
All Courses

From novice to tech pro — start learning today.