Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find row or column containing maximum value in numpy array

Tags:

python

numpy

How do I find the row or column which contains the array-wide maximum value in a 2d numpy array?

like image 410
Ferguzz Avatar asked Jul 04 '12 15:07

Ferguzz


2 Answers

If you only need one or the other:

np.argmax(np.max(x, axis=1))

for the column, and

np.argmax(np.max(x, axis=0))

for the row.

like image 132
ecatmur Avatar answered Oct 11 '22 10:10

ecatmur


You can use np.argmax along with np.unravel_index as in

x = np.random.random((5,5))
print np.unravel_index(np.argmax(x), x.shape)
like image 22
Geoff Reedy Avatar answered Oct 11 '22 11:10

Geoff Reedy