python - 如何在numpy数组中逐轴查找最小值/最大值

标签 python arrays numpy

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

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

我想得到每一列的最小值。

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

最佳答案

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

关于python - 如何在numpy数组中逐轴查找最小值/最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35930617/

相关文章:

c - 二维数组的段错误

php - 使用 unset() 删除 XML 中的节点; PHP/simplexml_load_file

python - 使用 python 中的 api 将图像从驱动器插入幻灯片

python - 了解聚类中的 np.zeros

python - 无法使用python在sqlite3中创建数据库

numpy - 如何可视化或绘制多维张量?

python/numpy ValueError : Expected 1D or 2D array, 得到了 0D 数组

python - 在Docker Python SDK中找不到 'docker node ps'的等效项

c - 具有此索引的数组如何工作? K&R练习

python - 常规与使用 For 循环时的 Numpy 数组乘法差异