参考链接: numpy.ndarray.argmax
参考链接: numpy.argmax
参考链接: numpy.ndarray.argmin
参考链接: numpy.argmin
函数功能说明:
返回多维数组指定维度上最大值或最小值所在小标.
默认情况下是先将多维数组展平成一维再进行运算,
返回的是全局范围里的最小值或最大值的小标.
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> import numpy as np
>>> np.random.seed(20200910)
>>> a = np.random.randn(2,3,4)
>>> a
array([[[ 0.12623007, 0.56616119, 0.72780637, -0.19852579],
[-0.63707299, -0.32897011, -1.4146631 , -0.04081202],
[ 0.04607789, -2.97790223, 0.72168288, -0.64314785]],
[[-0.01423725, -0.29393679, 0.82269532, -0.44465187],
[ 0.41797499, -0.6447249 , -0.78528072, 0.66156221],
[ 0.06651097, 0.64710217, 0.85498253, 1.03020921]]])
>>> t = np.argmax(a)
>>> t
23
>>> type(t)
<class 'numpy.int64'>
>>> t = np.argmax(a, axis=0)
>>> t
array([[0, 0, 1, 0],
[1, 0, 1, 1],
[1, 1, 1, 1]], dtype=int64)
>>> type(t)
<class 'numpy.ndarray'>
>>> t = np.argmax(a, axis=1)
>>> t
array([[0, 0, 0, 1],
[1, 2, 2, 2]], dtype=int64)
>>> t = np.argmax(a, axis=2)
>>> t
array([[2, 3, 2],
[2, 3, 3]], dtype=int64)
>>>
>>>
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> import numpy as np
>>> np.random.seed(20200910)
>>> a = np.random.randn(2,3,4)
>>> a
array([[[ 0.12623007, 0.56616119, 0.72780637, -0.19852579],
[-0.63707299, -0.32897011, -1.4146631 , -0.04081202],
[ 0.04607789, -2.97790223, 0.72168288, -0.64314785]],
[[-0.01423725, -0.29393679, 0.82269532, -0.44465187],
[ 0.41797499, -0.6447249 , -0.78528072, 0.66156221],
[ 0.06651097, 0.64710217, 0.85498253, 1.03020921]]])
>>> t = np.argmin(a)
>>> t
9
>>> type(t)
<class 'numpy.int64'>
>>> t = np.argmin(a, axis=0)
>>> t
array([[1, 1, 0, 1],
[0, 1, 0, 0],
[0, 0, 0, 0]], dtype=int64)
>>> type(t)
<class 'numpy.ndarray'>
>>> t = np.argmin(a, axis=1)
>>> t
array([[1, 2, 1, 2],
[0, 1, 1, 0]], dtype=int64)
>>> t = np.argmin(a, axis=2)
>>> t
array([[3, 2, 1],
[3, 2, 0]], dtype=int64)
>>>
>>>
>>>