np.argmax和np.argmin的使用举例

参考链接: 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)
>>> 
>>> 
>>> 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值