如何在numpy数组中逐轴查找最小值/最大值 [英] how to find minimum/maximum values axis by axis in numpy array

查看:164
本文介绍了如何在numpy数组中逐轴查找最小值/最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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?

推荐答案

您可以将 axis 指定为 numpy.min 函数的参数.

You can specify axis as parameter to numpy.min function.

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]])

这篇关于如何在numpy数组中逐轴查找最小值/最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆