如何在numpy数组中逐轴找到最小值/最大值

时间:2016-03-11 01:27:39

标签: python arrays numpy

我有一个形状为(3,1,2)的NumPy数组:

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

我想在每一栏中获得最低分。

在这种情况下,它们是1和2.我尝试使用np.amin,但它返回一个数组,这不是我想要的。有没有办法只使用一行或两行python代码而不使用循环?

1 个答案:

答案 0 :(得分:4)

您可以将axis指定为numpy.min功能的参数。

In [10]: A=np.array([[[1,4]],
                [[2,5]],
                [[3,6]]])

In [11]: np.min(A)
Out[11]: 1

In [12]: np.min(A, axis=0)
Out[12]: array([[1, 4]])

In [13]: np.min(A, axis=1)
Out[13]: 
array([[1, 4],
       [2, 5],
       [3, 6]])

In [14]: np.min(A, axis=2)
Out[14]: 
array([[1],
       [2],
       [3]])