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