本文摘自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 idle是什么
类的继承与方法的重载实例
Python求两个csv文件交集方法教程
Python如何计算平方和
Python操作sqlite数据库与文件操作的实例详解
在Python中导入哪个库可以进行大数据分析
Python怎么输出汉字
2018年最火的七个Python图形化gui开发框架
Python基础流程控制的介绍(代码示例)
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » 找出numpy array数组的最值及其索引方法