【问题标题】:how to find minimum/maximum values axis by axis in numpy array如何在numpy数组中逐轴查找最小值/最大值
【发布时间】:2016-03-11 01:27:39
【问题描述】:

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

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

我想得到每列的最小值。

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

【问题讨论】:

标签: python arrays numpy


【解决方案1】:

您可以将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]])

【讨论】:

  • @mengmengxyz,我不确定我是否理解你的问题。 np.min(np.array([[[1,4]],[[2,5]],[[3,2]]]), axis=0) 产生:array([[1, 2]]),这不是你要找的吗?
  • 那行得通.. 很抱歉让您感到困惑。我在做测试时弄乱了我的代码。非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-03
  • 1970-01-01
  • 2015-04-06
  • 2017-08-18
相关资源
最近更新 更多