42

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?

1 Answer 1

55

Just unpack the list:

In [273]: xmax, ymax = a.max(axis=0)

In [274]: print xmax, ymax
#10 6
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.