沿发生最大值的轴获取索引

时间:2019-12-20 12:45:53

标签: python numpy

我正在使用python和numpy进行某些数据分析。所以,说我有以下部分:

import numpy as np
x = np.random.rand(2, 2, 2)

导致:

array([[[0.7213753 , 0.89782739],
        [0.10375189, 0.02501165]],

       [[0.732744  , 0.17957702],
       [0.85643144, 0.7516079 ]]])

现在,我可以在第一个轴上找到最大值:

np.max(x, axis=0)

导致:

array([[0.732744  , 0.89782739],
       [0.85643144, 0.7516079 ]])

但是,我要代替最大值,而是要沿出现最大值的第一个轴索引。因此,对于此数据,输出应为:

[[1, 0
  1, 1]]

我尝试过类似的事情:

np.where(x == np.max(x, axis=0))

但这不会提供所需格式的输出。

1 个答案:

答案 0 :(得分:1)

>>> x.argmax(axis=0)
array([[1, 0],
       [1, 1]])
相关问题