numpy: argmin()和argmax()函数的逻辑是什么?

时间:2021-12-12 21:21:56

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

使用轴参数时,我无法理解argmax和argmin的输出。例如:

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

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:

可以看到,最大值是点(1,1)最小值是点(0,0)在我的逻辑中,

  • np.argmin(a,axis=0) I expected array([0,0,0,0])
  • np.argmin(轴= 0)我期望阵列([0,0,0,0))
  • np.argmin(a,axis=1) I expected array([0,0,0])
  • np.argmin(,轴= 1)我期望阵列((0,0,0))
  • np.argmax(a,axis=0) I expected array([1,1,1,1])
  • np.argmax(轴= 0)我期望阵列([1,1,1,1)
  • np.argmax(a,axis=1) I expected array([1,1,1])
  • np.argmax(,轴= 1)我期望阵列((1 1 1))

What is wrong with my understanding of things?

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

5 个解决方案

#1


28  

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参数,NumPy单独查看行和列。当没有给定时,数组a被扁平成一个一维数组。

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

axis=0表示操作依次在2D数组a的列上执行。

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:

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

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

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

另一方面,axis=1意味着操作在a的行上执行。

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:

这意味着np。argmin(a, axis=1)返回[0,2,2],因为a有3行。第一行最小值的索引为0,第二行和第三行最小值的索引为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])

#2


4  

As a side note: if you want to find the coordinates of your maximum value in the full array, you can use

附带说明:如果您想要在整个数组中找到最大值的坐标,可以使用

a=np.array([[1,2,4,7],[9,88,6,45],[9,76,3,4]])
>>> a
[[ 1  2  4  7]
 [ 9 88  6 45]
 [ 9 76  3  4]]

c=(np.argmax(a)/len(a[0]),np.argmax(a)%len(a[0]))
>>> c
(1, 1)

#3


3  

The np.argmax function by default works along the flattened array, unless you specify an axis. To see what is happening you can use flatten explicitly:

np。默认情况下,argmax函数沿着扁平数组工作,除非指定一个轴。要看到发生了什么,你可以明确地使用flatten:

np.argmax(a)
>>> 5

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

I've numbered the indices under the array above to make it clearer. Note that indices are numbered from zero in numpy.

我在上面的数组下面标上了序号,以便更清楚。注意,索引在numpy中是从0编号的。

In the cases where you specify the axis, it is also working as expected:

在指定轴的情况下,它也按预期工作:

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

This tells you that the largest value is in row 1 (2nd value), for each column along axis=0 (down). You can see this more clearly if you change your data a bit:

这告诉您最大的值在第1行(第2值),对于每一列沿轴=0(向下)。如果你稍微改变一下你的数据,你会更清楚地看到这一点:

a=np.array([[100,2,4,7],[9,88,6,45],[9,76,3,100]])
a
>>> array([[100,   2,   4,   7],
           [  9,  88,   6,  45],
           [  9,  76,   3, 100]])

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

As you can see it now identifies the maximum value in row 0 for column 1, row 1 for column 2 and 3 and row 3 for column 4.

如您所见,它现在标识了第1列的第0行最大值,第2列的第1行最大值,第3列的第4列最大值。

There is a useful guide to numpy indexing in the documentation.

在文档中有一个关于numpy索引的有用指南。

#4


0  

The axis in the argmax function argument, refers to the axis along which the array will be sliced.

argmax函数参数中的轴,指的是数组被切片的轴。

In another word, np.argmin(a,axis=0) is effectively the same as np.apply_along_axis(np.argmin, 0, a), that is to find out the minimum location for these sliced vectors along the axis=0.

换句话说,np.argmin(a,axis=0)与np.apply_along_axis(np)实际上是相同的。argmin, 0, a),即求这些沿轴=0的切分向量的最小位置。

Therefore in your example, np.argmin(a, axis=0) is [0, 0, 2, 2] which corresponding to values of [1, 2, 3, 4] on respective columns

所以在你的例子中,np。argmin(a, axis=0)是[0,0,2,2],对应于各自列上[1,2,3,4]的值。

#5


0  

""" ....READ THE COMMENTS FOR CLARIFICATION....."""

import numpy as np
a = np.array([[1,2,4,7], [9,88,6,45], [9,76,3,4]])

"""np.argmax(a) will give index of max value in flatted array of given matrix """
>>np.arg(max)
5

"""np.argmax(a,axis=0) will return list of indexes of  max value coloumnwise"""
>>print(np.argmax(a,axis=0))
[1,1,1,1]

"""np.argmax(a,axis=1) will return list of indexes of  max value rowwise"""
>>print(np.argmax(a,axis=1))
[3,1,1]

"""np.argmin(a) will give index of min value in flatted array of given matrix """
>>np.arg(min)
0

"""np.argmin(a,axis=0) will return list of indexes of  min value coloumnwise"""
>>print(np.argmin(a,axis=0))
[0,0,2,2]

"""np.argmin(a,axis=0) will return list of indexes of  min value rowwise"""
>>print(np.argmin(a,axis=1))
[0,2,2]

#1


28  

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参数,NumPy单独查看行和列。当没有给定时,数组a被扁平成一个一维数组。

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

axis=0表示操作依次在2D数组a的列上执行。

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:

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

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

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

另一方面,axis=1意味着操作在a的行上执行。

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:

这意味着np。argmin(a, axis=1)返回[0,2,2],因为a有3行。第一行最小值的索引为0,第二行和第三行最小值的索引为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])

#2


4  

As a side note: if you want to find the coordinates of your maximum value in the full array, you can use

附带说明:如果您想要在整个数组中找到最大值的坐标,可以使用

a=np.array([[1,2,4,7],[9,88,6,45],[9,76,3,4]])
>>> a
[[ 1  2  4  7]
 [ 9 88  6 45]
 [ 9 76  3  4]]

c=(np.argmax(a)/len(a[0]),np.argmax(a)%len(a[0]))
>>> c
(1, 1)

#3


3  

The np.argmax function by default works along the flattened array, unless you specify an axis. To see what is happening you can use flatten explicitly:

np。默认情况下,argmax函数沿着扁平数组工作,除非指定一个轴。要看到发生了什么,你可以明确地使用flatten:

np.argmax(a)
>>> 5

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

I've numbered the indices under the array above to make it clearer. Note that indices are numbered from zero in numpy.

我在上面的数组下面标上了序号,以便更清楚。注意,索引在numpy中是从0编号的。

In the cases where you specify the axis, it is also working as expected:

在指定轴的情况下,它也按预期工作:

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

This tells you that the largest value is in row 1 (2nd value), for each column along axis=0 (down). You can see this more clearly if you change your data a bit:

这告诉您最大的值在第1行(第2值),对于每一列沿轴=0(向下)。如果你稍微改变一下你的数据,你会更清楚地看到这一点:

a=np.array([[100,2,4,7],[9,88,6,45],[9,76,3,100]])
a
>>> array([[100,   2,   4,   7],
           [  9,  88,   6,  45],
           [  9,  76,   3, 100]])

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

As you can see it now identifies the maximum value in row 0 for column 1, row 1 for column 2 and 3 and row 3 for column 4.

如您所见,它现在标识了第1列的第0行最大值,第2列的第1行最大值,第3列的第4列最大值。

There is a useful guide to numpy indexing in the documentation.

在文档中有一个关于numpy索引的有用指南。

#4


0  

The axis in the argmax function argument, refers to the axis along which the array will be sliced.

argmax函数参数中的轴,指的是数组被切片的轴。

In another word, np.argmin(a,axis=0) is effectively the same as np.apply_along_axis(np.argmin, 0, a), that is to find out the minimum location for these sliced vectors along the axis=0.

换句话说,np.argmin(a,axis=0)与np.apply_along_axis(np)实际上是相同的。argmin, 0, a),即求这些沿轴=0的切分向量的最小位置。

Therefore in your example, np.argmin(a, axis=0) is [0, 0, 2, 2] which corresponding to values of [1, 2, 3, 4] on respective columns

所以在你的例子中,np。argmin(a, axis=0)是[0,0,2,2],对应于各自列上[1,2,3,4]的值。

#5


0  

""" ....READ THE COMMENTS FOR CLARIFICATION....."""

import numpy as np
a = np.array([[1,2,4,7], [9,88,6,45], [9,76,3,4]])

"""np.argmax(a) will give index of max value in flatted array of given matrix """
>>np.arg(max)
5

"""np.argmax(a,axis=0) will return list of indexes of  max value coloumnwise"""
>>print(np.argmax(a,axis=0))
[1,1,1,1]

"""np.argmax(a,axis=1) will return list of indexes of  max value rowwise"""
>>print(np.argmax(a,axis=1))
[3,1,1]

"""np.argmin(a) will give index of min value in flatted array of given matrix """
>>np.arg(min)
0

"""np.argmin(a,axis=0) will return list of indexes of  min value coloumnwise"""
>>print(np.argmin(a,axis=0))
[0,0,2,2]

"""np.argmin(a,axis=0) will return list of indexes of  min value rowwise"""
>>print(np.argmin(a,axis=1))
[0,2,2]