在numpy数组中查找包含最大值的行或列

Fer*_*uzz 12 python numpy

如何在2d numpy数组中找到包含数组范围最大值的行或列?

eca*_*mur 15

如果你只需要一个或另一个:

np.argmax(np.max(x, axis=1))
Run Code Online (Sandbox Code Playgroud)

对于列,和

np.argmax(np.max(x, axis=0))
Run Code Online (Sandbox Code Playgroud)

为行.


Aka*_*all 13

你可以用np.where(x == np.max(x)).

例如:

>>> x = np.array([[1,2,3],[2,3,4],[1,3,1]])
>>> x
array([[1, 2, 3],
       [2, 3, 4],
       [1, 3, 1]])
>>> np.where(x == np.max(x))
(array([1]), array([2]))
Run Code Online (Sandbox Code Playgroud)

第一个值是行号,第二个数是列号.

  • 如果存在平局,这可能会返回超过 1 个值 (3认同)

Geo*_*edy 7

您可以使用np.argmax连同np.unravel_index

x = np.random.random((5,5))
print np.unravel_index(np.argmax(x), x.shape)
Run Code Online (Sandbox Code Playgroud)