程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

How to find out the maximum value and index of numpy array in Python?

編輯:Python

from : Micro reading   https://www.weidianyuedu.com

stay list In the list ,max(list) You can get list The maximum of ,list.index(max(list)) You can get the index corresponding to the maximum value

But in numpy Medium array No, index Method , In its place where, And then again list There is no the

First of all, we can get array The maximum value in the global and per row and per column ( The minimum is the same )

>>> a = np.arange(9).reshape((3,3))>>> aarray([[0, 1, 2], [9, 4, 5], [6, 7, 8]])>>> print(np.max(a)) # Global maximum 8>>> print(np.max(a,axis=0)) # Maximum per column [6 7 8]>>> print(np.max(a,axis=1)) # Each line is the largest [2 5 8]

And then use where Get the maximum index , Return value , Ahead array Number of corresponding lines , The latter corresponds to the number of columns

>>> 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))

If array There is the same maximum value in ,where Will give all their positions

>>> a[1,0]=8>>> aarray([[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))


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved