2016-06-24 26 views
0

如何通過具有多個「標記」的另一個數組(標籤)獲取數組中值的索引(a)?例如,給定查找包含來自另一個數組的值之一的數組中的索引

label = array([1, 2]) 
a = array([1, 1, 2, 2, 3, 3]) 

的目標是找到的a具有1或2的值的索引;也就是0,1,2,3。

我嘗試過幾種組合。以下都不起作用。

label = array([1, 2]) 
a = array([1, 1, 2, 2, 3, 3]) 
idx = where(a==label) # gives me only the index of the last value in label 
idx = where(a==label[0] or label[1]) # Is confused by all or any? 
idx = where(a==label[0] | label[1]) # gives me results as if nor. idx = [4,5] 
idx = where(a==label[0] || label[1]) # syntax error 
idx = where(a==bolean.or(label,0,1) # I know, this is not the correct form but I don`t remember it correctly but remember the error: also asks for a.all or a.any 
idx = where(label[0] or label[1] in a)   # gives me only the first appearance. index = 0. Also without where(). 
idx = where(a==label[0] or a==label[1]).all()) # syntax error 
idx = where(a.any(0,label[0] or label[1])) # gives me only the first appearance. index=0. Also without where(). 
idx = where(a.any(0,label[0] | label[1])) # gives me only the first appearance. index=0. Also without where(). 
idx=where(a.any(0,label)) # Datatype not understood 

好的,我想你會得到我的問題。有誰知道如何正確地做到這一點?最好是使用通用標籤而不是標籤[x]的解決方案,以便標籤的使用對於以後的更改更加可變。

+1

您需要更準確地描述你想要做什麼。就目前而言,你的問題非常模糊。 – user2357112

+0

不知道我是否理解你,但試試這個:'idx = where(a == label [0] | a == label [1])' –

+1

在()中包裝這些'=='測試以獲得操作符的順序正確。 – hpaulj

回答

4

您可以使用numpy.in1d

>>> a = numpy.array([1, 1, 2, 2, 3, 3]) 
>>> label = numpy.array([1, 2]) 
>>> numpy.in1d(a, label) 
array([ True, True, True, True, False, False], dtype=bool) 

以上的回報口罩。如果您需要索引,則可以在屏蔽陣列上調用numpy.nonzero

此外,如果label數組中的值是唯一的,則可以將assume_unique=True傳遞給in1d以加快速度。

1

np.where(a==label)np.nonzeros(a==label)相同。它告訴我們數組中所有非零(或True)元素的座標(索引),a==label。的

因此,而不是嘗試所有這些不同的where表情,專注條件陣列上

沒有這裏的where就是一些你的表情的產生:

In [40]: a==label # 2 arrays don't match in size, scalar False 
Out[40]: False 

In [41]: a==label[0] # result is the size of a 
Out[41]: array([ True, True, False, False, False, False], dtype=bool) 

In [42]: a==label[0] or label[1] # or is a Python scalar operation 
... 
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() 

In [43]: a==label[0] | label[1] 
Out[43]: array([False, False, False, False, True, True], dtype=bool) 

最後這個是一樣的a==(label[0] | label[1])時,在==之前評估|

您需要了解每個這些數組(或標量或錯誤)是如何產生的,然後才能明白where爲您提供了什麼。的2個相等測試

正確組合(額外()是重要的):

In [44]: (a==label[1]) | (a==label[0]) 
Out[44]: array([ True, True, True, True, False, False], dtype=bool) 

使用廣播到分別測試的label的2個元素。結果是二維數組:

In [45]: a==label[:,None] 
Out[45]: 
array([[ True, True, False, False, False, False], 
     [False, False, True, True, False, False]], dtype=bool) 

In [47]: (a==label[:,None]).any(axis=0) 
Out[47]: array([ True, True, True, True, False, False], dtype=bool) 
+0

親愛的hpaulj,非常感謝您的深入解答。我標記了上面的那個,因爲其他人可能就在這裏爲了快速回答。但對我來說,這是非常好的,因爲我現在瞭解問題的背景。乾杯 –

0

據我所知,你需要在數組「a」1和2的指數。

在這種情況下,嘗試

label= [1,2] 
a= [1,1,2,2,3,3] 

idx_list = list() 
for x in label: 
    for i in range(0,len(a)-1): 
     if a[i] == x: 
      idx_list.append(i) 
0

我想我在讀什麼,因爲你的意圖是獲得第一個列表中'值'的第二個列表'a'的值。我認爲字典是存儲這些信息的好方法,其中標籤是鍵和索引將是值。如果你想要的1索引只需要調用結果

labels = [a,2] 
    a = [1,1,2,2,3,3] 
    results = {} 
    for label in labels: 
     results[label] = [i for i,x in enumerate(a) if x == label] 

[1]:

試試這個。列表理解是和枚舉函數是真正的MVP在這裏。

相關問題