Python 理解argmax
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36300334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Understanding argmax
提问by user71346
Let say I have the matrix
假设我有矩阵
import numpy as np
A = np.matrix([[1,2,3,33],[4,5,6,66],[7,8,9,99]])
I am trying to understand the function argmax, as far as I know it returns the largest value
我试图理解函数 argmax,据我所知它返回最大值
If I tried it on Python:
如果我在 Python 上尝试过:
np.argmax(A[1:,2])
Should I get the largest element in the second row till the end of the row (which is the third row) and along the third column? So it should be the array [6 9], and arg max should return 9? But why when I run it on Python, it returns the value 1?
我应该获得第二行中最大的元素,直到行尾(即第三行)和第三列?所以应该是数组[6 9],arg max 应该返回9?但是为什么当我在 Python 上运行它时,它返回值 1?
And if I want to return the largest element from row 2 onwards in column 3 (which is 9), how should I modify the code?
如果我想从第 2 行开始返回第 3 列(即 9)中的最大元素,我应该如何修改代码?
I have checked the Python documentation but still a bit unclear. Thanks for the help and explanation.
我已经检查了 Python 文档,但仍然有点不清楚。感谢您的帮助和解释。
回答by roadrunner66
No argmax
returns the position ofthe largest value. max
returns the largest value.
否argmax
返回最大值的位置。max
返回最大值。
import numpy as np
A = np.matrix([[1,2,3,33],[4,5,6,66],[7,8,9,99]])
np.argmax(A) # 11, which is the position of 99
np.argmax(A[:,:]) # 11, which is the position of 99
np.argmax(A[:1]) # 3, which is the position of 33
np.argmax(A[:,2]) # 2, which is the position of 9
np.argmax(A[1:,2]) # 1, which is the position of 9
回答by Niamat Zawad
It took me a while to figure this function out. Basically argmax returns you the indexof the maximum value in the array. Now the array can be 1 dimensional or multiple dimensions. Following are some examples.
我花了一段时间才弄明白这个功能。基本上 argmax 返回数组中最大值的索引。现在数组可以是一维或多维。以下是一些示例。
1 dimensional
一维
a = [[1,2,3,4,5]]
np.argmax(a)
>>4
The array is 1 dimensional so the function simply returns the index of the maximum value(5) in the array, which is 4.
该数组是一维的,因此该函数仅返回数组中最大值 (5) 的索引,即 4。
Multiple dimensions
多维度
a = [[1,2,3],[4,5,6]]
np.argmax(a)
>>5
In this example the array is 2 dimensional, with shape (2,3). Since no axis parameter is specified in the function, the numpy library flattens the array to a 1 dimensional array and then returns the index of the maximum value. In this case the array is transformed to [[1,2,3,4,5,6]] and then returns the index of 6, which is 5.
在这个例子中,数组是二维的,形状为 (2,3)。由于函数中没有指定轴参数,numpy 库将数组展平为一维数组,然后返回最大值的索引。在这种情况下,数组被转换为 [[1,2,3,4,5,6]],然后返回 6 的索引,即 5。
When parameter is axis = 0
当参数为axis = 0时
a = [[1,2,3],[4,5,6]]
np.argmax(a, axis=0)
>>array([1, 1, 1])
The result here was a bit confusing to me at first. Since the axis is defined to be 0, the function will now try to find the maximum value along the rows of the matrix. The maximum value,6, is in the second row of the matrix. The index of the second row is 1. According to the documentation https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.argmax.htmlthe dimension specified in the axis parameter will be removed. Since the shape of the original matrix was (2,3) and axis specified as 0, the returned matrix will have a shape of(3,) instead, since the 2 in the original shape(2,3) is removed.The row in which the maximum value was found is now repeated for the same number of elements as the columns in the original matrix i.e. 3.
这里的结果起初让我有点困惑。由于轴被定义为 0,函数现在将尝试沿矩阵的行找到最大值。最大值 6 位于矩阵的第二行。第二行的索引为 1。根据文档https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.argmax.html将删除轴参数中指定的维度. 由于原始矩阵的形状为 (2,3) 且轴指定为 0,因此返回的矩阵的形状将为 (3,),因为原始形状 (2,3) 中的 2 已被删除。行现在,对于与原始矩阵中的列(即 3)相同数量的元素,找到最大值的位置重复。
When parameter is axis = 1
当参数为axis = 1时
a = [[1,2,3],[4,5,6]]
np.argmax(a, axis=1)
>>array([2, 2])
Same concept as above but now index of the column is returned at which the maximum value is available. In this example the maximum value 6 is in the 3rd column, index 2. The column of the original matrix with shape (2,3) will be removed, transforming to (2,) and so the return array will display two elements, each showing the index of the column in which the maximum value was found.
与上面相同的概念,但现在返回最大值可用的列索引。在这个例子中,最大值 6 位于第 3 列,索引 2。原始矩阵的形状为 (2,3) 的列将被删除,转换为 (2,),因此返回数组将显示两个元素,每个元素显示找到最大值的列的索引。
回答by Mukul Taneja
argmax
is a function which gives the index of the greatest number in the given row or column and the row or column can be decided using axis attribute of argmax
funcion. If we give axis=0
then it will give the index from columns and if we give axis=1
then it will give the index from rows.
argmax
是一个函数,它给出给定行或列中最大数字的索引,并且可以使用函数的轴属性来确定行或列argmax
。如果我们给出axis=0
,它将给出列的索引,如果我们给出axis=1
,它将给出行的索引。
In your given example A[1:, 2]
it will first fetch the values from 1st row on wards and the only 2nd column value from those rows, then it will find the index of max value from into the resulted matrix.
在您给定的示例中A[1:, 2]
,它将首先从病房的第一行获取值,并从这些行中获取唯一的第二列值,然后它将从结果矩阵中找到最大值的索引。
回答by Emilio Chica Jimenéz
In my first steps in python i have tested this function. And the result with this example clarified me how works argmax.
在我使用 python 的第一步中,我测试了这个函数。这个例子的结果让我明白了 argmax 是如何工作的。
Example:
示例:
# Generating 2D array for input
array = np.arange(20).reshape(4, 5)
array[1][2] = 25
print("The input array: \n", array)
# without axis
print("\nThe max element: ", np.argmax(array))
# with axis
print("\nThe indices of max element: ", np.argmax(array, axis=0))
print("\nThe indices of max element: ", np.argmax(array, axis=1))
Result Example:
结果示例:
The input array:
[[ 0 1 2 3 4]
[ 5 6 25 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
The max element: 7
The indices of max element: [3 3 1 3 3]
The indices of max element: [4 2 4 4]
In that result we can see 3 results.
在那个结果中,我们可以看到 3 个结果。
- The highest element in all array is in position 7.
- The highest element in every column is in the last row which index is 3, except on third column where the highest value is in row number two which index is 1.
- The highest element in every row is in the last column which index is 4, except on second row where the highest value is in third columen which index is 2.
- 所有数组中最高的元素位于位置 7。
- 每列中的最高元素位于索引为 3 的最后一行,但在第三列中,最高值位于第二行,索引为 1。
- 每行中的最高元素位于索引为 4 的最后一列中,除了在第二行中最高值位于索引为 2 的第三列中。
Reference:https://www.crazygeeks.org/numpy-argmax-in-python/
参考:https : //www.crazygeeks.org/numpy-argmax-in-python/
I hope that it helps.
我希望它有帮助。