Max*_*Max 11 python arrays numpy max
我想使用 NumPy 找到二维数组中的最大值和 Python 中最大值的索引。我用了
np.amax(array)
Run Code Online (Sandbox Code Playgroud)
用于搜索最大值,但我不知道如何获取其索引。我可以通过使用“for”循环找到它,但也许有更好的方法。
Cha*_*shi 29
请参阅此答案,其中还详细说明了如何找到最大值及其(1D)索引,您可以使用argmax()
>>> a = array([[10,50,30],[60,20,40]])
>>> maxindex = a.argmax()
>>> maxindex
3
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用unravel_index(a.argmax(), a.shape)将索引作为元组获取:
>>> from numpy import unravel_index
>>> unravel_index(a.argmax(), a.shape)
(1, 0)
Run Code Online (Sandbox Code Playgroud)