2016-03-01 45 views
1

在基本層面我可以由另一個指數一個numpy的陣列,所以我可以返回一個數組的索引,使得:子集的一個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) 

我想知道(從A)的位置由(B)

索引在我的數據中每一個陣列的最大值(C)我曾嘗試:

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

檢索索引位置

任何幫助表示讚賞!

+0

相應的值'(B == C).shape'是'(40000,30 )'。這與A的形狀如何相對應? – Eric

回答

1

這是你想要的嗎?獲得,其中最大值是在使用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]) 
+0

是的,它是我所需要的。它返回給我一個40000個值的數組。 argmax創造了不同。我正在使用amax! –