numpy:argmin()和argmax()函数的逻辑是什么? [英] numpy: what is the logic of the argmin() and argmax() functions?

查看:247
本文介绍了numpy:argmin()和argmax()函数的逻辑是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当与axis参数一起使用时,我无法理解argmaxargmin的输出.例如:

I can not understand the output of argmax and argmin when use with the axis parameter. For example:

>>> a = np.array([[1,2,4,7], [9,88,6,45], [9,76,3,4]])
>>> a
array([[ 1,  2,  4,  7],
       [ 9, 88,  6, 45],
       [ 9, 76,  3,  4]])
>>> a.shape
(3, 4)
>>> a.size
12
>>> np.argmax(a)
5
>>> np.argmax(a,axis=0)
array([1, 1, 1, 1])
>>> np.argmax(a,axis=1)
array([3, 1, 1])
>>> np.argmin(a)
0
>>> np.argmin(a,axis=0)
array([0, 0, 2, 2])
>>> np.argmin(a,axis=1)
array([0, 2, 2])

如您所见,最大值是点(1,1),最小值是点(0,0).因此,按照我的逻辑,当我运行时:

As you can see, the maximum value is the point (1,1) and the minimum one is the point (0,0). So in my logic when I run:

  • np.argmin(a,axis=0)我期望array([0,0,0,0])
  • np.argmin(a,axis=1)我期望array([0,0,0])
  • np.argmax(a,axis=0)我应该是array([1,1,1,1])
  • np.argmax(a,axis=1)我期望array([1,1,1])
  • np.argmin(a,axis=0) I expected array([0,0,0,0])
  • np.argmin(a,axis=1) I expected array([0,0,0])
  • np.argmax(a,axis=0) I expected array([1,1,1,1])
  • np.argmax(a,axis=1) I expected array([1,1,1])

我对事物的理解出了什么问题?

What is wrong with my understanding of things?

推荐答案

通过添加axis参数,NumPy分别查看行和列.如果未指定,则将数组a展平为单个一维数组.

By adding the axis argument, NumPy looks at the rows and columns individually. When it's not given, the array a is flattened into a single 1D array.

axis=0表示依次在二维数组a的列中 down 进行操作.

axis=0 means that the operation is performed down the columns of a 2D array a in turn.

例如,np.argmin(a, axis=0)返回四列中每一列的最小值的索引.每列的最小值显示在下面的粗体中:

For example np.argmin(a, axis=0) returns the index of the minimum value in each of the four columns. The minimum value in each column is shown in bold below:

>>> a
array([[ 1,  2,  4,  7],  # 0
       [ 9, 88,  6, 45],  # 1
       [ 9, 76,  3,  4]]) # 2

>>> np.argmin(a, axis=0)
array([0, 0, 2, 2])

另一方面,axis=1表示该操作是在a行中跨 行执行的.

On the other hand, axis=1 means that the operation is performed across the rows of a.

这意味着np.argmin(a, axis=1)返回[0, 2, 2],因为a具有三行.第一行的最小值的索引为0,第二行和第三行的最小值的索引为2:

That means np.argmin(a, axis=1) returns [0, 2, 2] because a has three rows. The index of the minimum value in the first row is 0, the index of the minimum value of the second and third rows is 2:

>>> a
#        0   1   2   3
array([[ 1,  2,  4,  7],
       [ 9, 88,  6, 45],
       [ 9, 76,  3,  4]])

>>> np.argmin(a, axis=1)
array([0, 2, 2])

这篇关于numpy:argmin()和argmax()函数的逻辑是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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