在基本级别上,我可以一个索引另一个numpy数组,以便返回数组的索引,例如:

a = [1,2,3,4,5,6]

b = [0.4, 0.5, 0.6, 0.7, 0.8, 0.9]


可以通过以下方式找到指数0.6:

c = a[b==0.6]


但是,现在我有了3D阵列,无法满足需要。

我有3个数组:

A = [[21,22,23....48,49,50]] # An index over the range 20-50 with shape (1,30)

B = [[0.1,0.6,0.5,0.4,0.8...0.7,0.2,0.4],
     ..................................
     [0.5,0.2,0.7,0.1,0.5...0.8,0.9,0.3]] # This is my data with shape (40000, 30)

C = [[0.8],........[0.9]] # Maximum values from each array in B with shape (40000,1)


我想通过索引数据(B)中每个数组的最大值(C)来了解位置(从A)

我试过了:

D = A[B==C]


但我不断收到错误消息:

IndexError: index 1 is out of bounds for axis 0 with size 1


我自己可以获得:

B==C # prints as arrays of True or False


但我无法从A检索索引位置。

任何帮助表示赞赏!

最佳答案

您在寻找这种东西吗?使用argmax函数获取最大值在每一行的索引,并使用索引获取A中的相应值。

In [16]: x = np.random.random((20, 30))
In [16]: max_inds = x.argmax(axis=1)

In [17]: max_inds.shape
Out[17]: (20,)

In [18]: A = np.arange(x.shape[1])

In [19]: A.shape
Out[19]: (30,)

In [20]: A[max_inds]
Out[20]:
array([20,  5, 27, 19, 27, 21, 18, 25, 10, 24, 16, 21,  6,  7, 27, 17, 24,
        8, 27,  8])

10-08 07:52
查看更多