how to find minimum/maximum values axis by axis in numpy array

Viewed 14477

I have a NumPy array with a shape of (3,1,2):

A=np.array([[[1,4]],
            [[2,5]],
            [[3,2]]]).

I'd like to get the min in each column.

In this case, they are 1 and 2. I tried to use np.amin but it returns an array and that is not what I wanted. Is there a way to do this in just one or two lines of python code without using loops?

2 Answers

You need to specify the axes along which you need to find the minimum. In your case, it is along the first two dimensions. So:

>>> np.min(A, axis=(0, 1))
array([1, 2])
Related