本文摘自php中文网,作者不言,侵删。
下面为大家分享一篇python中找出numpy array数组的最值及其索引方法,具有很好的参考价值,希望对大家有所帮助。一起过来看看吧在list列表中,max(list)可以得到list的最大值,list.index(max(list))可以得到最大值对应的索引
但在numpy中的array没有index方法,取而代之的是where,其又是list没有的
首先我们可以得到array在全局和每行每列的最大值(最小值同理)
1 2 3 4 5 6 7 8 9 10 11 | >>> a = np.arange(9).reshape((3,3))
>>> a
array ([[0, 1, 2],
[9, 4, 5],
[6, 7, 8]])
>>> print (np.max(a)) #全局最大
8
>>> print (np.max(a,axis=0)) #每列最大
[6 7 8]
>>> print (np.max(a,axis=1)) #每行最大
[2 5 8]
|
然后用where得到最大值的索引,返回值中,前面的array对应行数,后者对应列数
1 2 3 4 | >>> print (np.where(a==np.max(a)))
( array ([2], dtype=int64), array ([2], dtype=int64))
>>> print (np.where(a==np.max(a,axis=0)))
( array ([2, 2, 2], dtype=int64), array ([0, 1, 2], dtype=int64))
|
如果array中有相同的最大值,where会将其位置全部给出
1 2 3 4 5 6 7 | >>> a[1,0]=8
>>> a
array ([[0, 1, 2],
[8, 4, 5],
[6, 7, 8]])
>>> print (np.where(a==np.max(a)))
( array ([1, 2], dtype=int64), array ([0, 2], dtype=int64))
|
相关推荐:
怎样取numpy数组指定行列
怎样用numpy找出数组里最大与最小值
以上就是找出numpy array数组的最值及其索引方法的详细内容,更多文章请关注木庄网络博客!!
相关阅读 >>
Python中什么是对象
Python和matlab哪个难
Python怎么转化为字符串
Python在实际工作中的应用有哪些
Python招聘要求高吗
vscode编写第一个Python程序helloworld步骤_Python
Python的发展史介绍
深入了解Python之django框架
Python中列表怎么排序
如何用Python正则表达式匹配字符串?
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » 找出numpy array数组的最值及其索引方法