numpy.argmax函数讲解
文章目录
- 0.简介
- 1.对一个一维向量
- 2.对2维向量(通常意义下的矩阵)
- 3.对高维数组
https://numpy.org/doc/stable/reference/generated/numpy.argmax.html
首先附上官方提供的源代码,供大家参考
源代码地址为:
https://github.com/numpy/numpy/blob/v1.22.0/numpy/core/fromnumeric.py#L1127-L1216
"""Module containing non-deprecated functions borrowed from Numeric.
"""
import functools
import types
import warningsimport numpy as np
from . import multiarray as mu
from . import overrides
from . import umath as um
from . import numerictypes as nt
from .multiarray import asarray, array, asanyarray, concatenate
from . import _methods_dt_ = nt.sctype2char# functions that are methods
__all__ = ['alen', 'all', 'alltrue', 'amax', 'amin', 'any', 'argmax','argmin', 'argpartition', 'argsort', 'around', 'choose', 'clip','compress', 'cumprod', 'cumproduct', 'cumsum', 'diagonal', 'mean','ndim', 'nonzero', 'partition', 'prod', 'product', 'ptp', 'put','ravel', 'repeat', 'reshape', 'resize', 'round_','searchsorted', 'shape', 'size', 'sometrue', 'sort', 'squeeze','std', 'sum', 'swapaxes', 'take', 'trace', 'transpose', 'var',
]_gentype = types.GeneratorType
# save away Python sum
_sum_ = sumarray_function_dispatch = functools.partial(overrides.array_function_dispatch, module='numpy')# functions that are now methods
def _wrapit(obj, method, *args, **kwds):try:wrap = obj.__array_wrap__except AttributeError:wrap = Noneresult = getattr(asarray(obj), method)(*args, **kwds)if wrap:if not isinstance(result, mu.ndarray):result = asarray(result)result = wrap(result)return resultdef _wrapfunc(obj, method, *args, **kwds):bound = getattr(obj, method, None)if bound is None:return _wrapit(obj, method, *args, **kwds)try:return bound(*args, **kwds)except TypeError:# A TypeError occurs if the object does have such a method in its# class, but its signature is not identical to that of NumPy's. This# situation has occurred in the case of a downstream library like# 'pandas'.## Call _wrapit from within the except clause to ensure a potential# exception has a traceback chain.return _wrapit(obj, method, *args, **kwds)def _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs):passkwargs = {k: v for k, v in kwargs.items()if v is not np._NoValue}if type(obj) is not mu.ndarray:try:reduction = getattr(obj, method)except AttributeError:passelse:# This branch is needed for reductions like any which don't# support a dtype.if dtype is not None:return reduction(axis=axis, dtype=dtype, out=out, **passkwargs)else:return reduction(axis=axis, out=out, **passkwargs)return ufunc.reduce(obj, axis, dtype, out, **passkwargs)def _take_dispatcher(a, indices, axis=None, out=None, mode=None):return (a, out)
0.简介
1.对一个一维向量
numpy.argmax(array, axis) 用于返回一个numpy数组中最大值的索引值。当一组中同时出现几个最大值时,返回第一个最大值的索引值。
在运算时,相当于剥掉一层中括号,返回一个数组,分为一维和多维。一维数组剥掉一层中括号之后就成了一个索引值,是一个数,而n维数组剥掉一层中括号后,会返回一个 n-1 维数组,而剥掉哪一层中括号,取决于axis的取值。
n维的数组的 axis 可以取值从 0 到 n-1,其对应的括号层数为从最外层向内递进。
one_dim_array = np.array([1, 4, 5])
print(np.argmax(one_dim_array))
输出结果为
2
2.对2维向量(通常意义下的矩阵)
遵循运算之后降一维的原则,因此返回的会是一个一维的array。同时,axis的取值为0和1,对应剥掉的中括号,将里面的内容直接按逗号分隔:
0 —— 外层
1 —— 内层
import numpy as np
a = np.array([[1, 5, 5, 2],[9, 6, 2, 8],[3, 7, 9, 1]])
b=np.argmax(a, axis=0)#对二维矩阵来讲a[0][1]会有两个索引方向,第一个方向为a[0],默认按列方向搜索最大值
#a的第一列为1,9,3,最大值为9,所在位置为1,
#a的第一列为5,6,7,最大值为7,所在位置为2,
#此此类推,因为a有4列,所以得到的b为1行4列,
print(b)#[1 2 2 1]c=np.argmax(a, axis=1)#现在按照a[0][1]中的a[1]方向,即行方向搜索最大值,
#a的第一行为1,5,5,2,最大值为5(虽然有2个5,但取第一个5所在的位置),索引值为1,
#a的第2行为9,6,2,8,最大值为9,索引值为0,
#因为a有3行,所以得到的c有3个值,即为1行3列
print(c)#[1 0 2]
3.对高维数组
主要在lstm中使用。以三维为例,计算思路与二维相同。
三维计算之后降维,将返回一个二维数组。
一个m×n×p维的矩阵,
axis为0,舍去m,返回一个 n×p 维的矩阵
axis为1,舍去n,返回一个 m×p 维的矩阵
axis为2,舍去p,返回一个 m×n 维的矩阵
numpy.argmax函数讲解相关推荐
- numpy.corrcoef()函数讲解
numpy.corrcoef(x, y=无, rowvar = True, 偏差=<无值>, ddof=<无值>) [学习参考]:Python Numpy库 numpy.cor ...
- Python Numpy库 numpy.corrcoef()函数讲解
例子: 代码: import numpy as npArray1 = [[1, 2, 3], [4, 5, 6]] Array2 = [[11, 25, 346], [734, 48, 49]] Ma ...
- Numpy库 numpy.corrcoef()函数
相关系数公式: 其他详见: 1. Python Numpy库 numpy.corrcoef()函数讲解 2. 协方差.方差.标准差.协方差系数 3. 标准差.方差.协方差三者的表示意义
- numpy使用np.argmax函数获取一维数组中最大值所在的索引(index of largest value in numpy array with np.argmax)
numpy使用np.argmax函数获取一维数组中最大值所在的索引(index of largest value in numpy array with np.argmax) 目录 numpy使用np ...
- numpy之argmax()函数
语法格式: numpy.argmax(a,axis) 作用:返回axis轴方向最大值的索引 a :为所需处理的矩阵 axis :为处理的轴向,axis=1为横轴方向,方向从左到右:axis=0为纵轴方 ...
- Tensor数据相关的运算、函数讲解及与numpy区别
Tensor tensorflow 中使用它来表示数据.可以看做多维数组或者list. 标量是张量,向量是张量,矩阵是张量,矩阵的矩阵是张量. 常用几种定义方法 1. variable变量,一般是可以 ...
- 【Python】Numpy中的argmax()函数
描述:在数组里查找相同元素,返回索引 argmax()函数 numpy.argmax(a,axis=None,out=None) 参数: 1.a:我们使用的(输入)数组如果是沿着0轴,则返回每一列最大 ...
- argmax函数_Python之Numpy库常用函数合集(附注释)
文末免费领取[亚马逊.阿里巴巴股票数据] 最近学习Python,才发现原来python里的各种库才是大头!于是乎找了学习资料对Numpy库常用的函数进行总结,并带了注释.在这里分享给大家,对于库的学习 ...
- python的empty函数_python中numpy.empty()函数实例讲解
在使用python编程的过程中,想要快速的创建ndarray数组,可以使用numpy.empty()函数.numpy.empty()函数所创建的数组内所有元素均为空,没有实际意义,所以它也是创建数组最 ...
最新文章
- 熟悉交换机与路由器组网(图解)
- php和python区别-php和python什么区别
- python自动测试p-python自动化测试报告(excel篇)
- DataGridView怎样实现添加、删除、上移、下移一行
- JavaScript实现冒泡排序 可视化
- Linux 的cp命令
- maven mysql的jdbctemplate_JDBC、JDBCTemplate、MyBatis、Hiberante 比较与分析
- SmartGWT入门,提供出色的GWT界面
- devc++不兼容_Mac压缩文件Win不支持?BetterZip帮你解决!
- 使用 Request.QueryString 接受参数时,跟编码有关的一些问题
- 总结了12个Numpy高级函数,完美解决数据处理,拿来即用!
- Python 数据结构与算法——二叉搜索树的实现
- WIN7系统没有USB驱动和以太网驱动如何操作
- http状态码为499的解决办法
- 问题 B: 零基础学C/C++25——判断某整数是正整数、负整数还是零
- 中国钢铁产业产量分析与市场需求状况研究报告2022版
- 树莓派通过API向企业微信推送图文
- Xshell连接虚拟机linux
- Python基础知识总结(期末复习精简版)
- InVEST模型 | HAbitat quality模块计算生境质量
热门文章
- Go语言defer关键字
- Python读取Excel文件时缺少第一行的解决办法
- □ 影片名:《我的野蛮女友》(108579) 在线播放
- 源码级调试的XNU内核
- 烤仔的朋友们 | 以太坊 Gas 费为何阻碍了 DeFi 发展?怎么办?
- mysql ormlite_Ormlite or()的使用
- java中''和单双引号有什么区别
- 我是程序员,今年 35 岁,依然可以“横行职场”
- The node was low on resource: ephemeral-storage. Container kube-proxy was using 12Ki, which exceeds
- 【已解决】关于Socket编程:客户端、服务端对象流交互数据丢失的问题