Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find max value in a numpy array column?

I can find quite a few permutations of this question, but not this (rather simple) one: how do I find the maximum value of a specific column of a numpy array (in the most pythonic way)?

a = array([[10, 2], [3, 4], [5, 6]]) 

What I want is the max value in the first column and second column (these are x,y coordinates and I eventually need the height and width of each shape), so max x coordinate is 10 and max y coordinate is 6.

I've tried:

xmax = numpy.amax(a,axis=0) ymax = numpy.amax(a,axis=1) 

but these yield

array([10, 6]) array([10, 4, 6]) 

...not what I expected.

My solution is to use slices:

xmax = numpy.max(a[:,0]) ymax = numpy.max(a[:,1]) 

Which works but doesn't seem to the best approach.

Suggestions?

like image 967
TheGeographer Avatar asked Mar 02 '14 15:03

TheGeographer


People also ask

What is the way to find the maximum number in the NumPy?

maximum() function is used to find the element-wise maximum of array elements. It compares two arrays and returns a new array containing the element-wise maxima. If one of the elements being compared is a NaN, then that element is returned.

How do you find the max and min value of a NumPy array?

amax() will find the max value in an array, and numpy. amin() does the same for the min value.

How do you find the max of a column of a 2d array Python?

To get the indizes of the maxima in the columns, you can use the ndarray. argmax() function. You can also pass the axis argument ot this function, but there is no keepdims option. In both commands axis=0 describes the columns, axis=1 describes the rows.


1 Answers

Just unpack the list:

In [273]: xmax, ymax = a.max(axis=0)  In [274]: print xmax, ymax #10 6 
like image 142
zhangxaochen Avatar answered Sep 18 '22 06:09

zhangxaochen