2016-06-24 25 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在这里。

相关问题