如何从numpy中的索引数组创建布尔数组?

时间:2023-01-05 13:36:21

Given I have an multidimensional array of indices, how do I create a Boolean array from these? For the 1D case it would look like this:

鉴于我有一个多维索引数组,如何从这些数组创建一个布尔数组?对于1D案例,它看起来像这样:

a = [1,5,6]
b = somefunction(total_array_length=10, a)
>>> [False, True, False, False, False, True, True, False, False, False]

For the 2D case it would look like this:

对于2D情况,它看起来像这样:

a = [[1,3],[4,2]]
b = somefunction(total_array_length=5, a)
>>> [[False, True, False, True, False], [False, False, True, False, True]]

I want to use this to create a mask for an array. I have a multi dimensional array of dimension 8 and for the last axis I can find the indices of the elements I want to keep. In other words, I have an 8D array in which the last axis consists all indices I want to keep in the original array. Does anyone know how to do this?

我想用它来为数组创建一个掩码。我有一个维度为8的多维数组,对于最后一个轴,我可以找到我想要保留的元素的索引。换句话说,我有一个8D数组,其中最后一个轴包含我想要保留在原始数组中的所有索引。有谁知道如何做到这一点?

In the function above the total_array_length would be equal to the length of the original array.

在上面的函数中,total_array_length将等于原始数组的长度。

So how would I do this for an array of shape (23,5,76,32,1,3,8,9) with indices array of shape (23,5,76,32,1,3,8,4)? Note 4<9 but other than that it has the same dimensions.

那么我怎么做一个形状数组(23,5,76,32,1,3,8,9),索引数组形状(23,5,76,32,1,3,8,4) ?注4 <9但除此之外它具有相同的尺寸。

a.shape = (23,5,76,32,1,3,8,4) 
b = somefunction(total_array_length=9, a)
b.shape =(23,5,76,32,1,3,8,9) 

1 个解决方案

#1


1  

For the first case:

对于第一种情况:

In [23]: a = [1,5,6]
In [24]: b = np.zeros(10, dtype=bool)
In [25]: b[a] = True
In [26]: b
Out[26]: array([False,  True, False, False, False,  True,  True, False, False, False], dtype=bool)

An equally simple, and possibly as fast, list version:

一个同样简单,可能同样快的列表版本:

In [27]: [True if i in a else False for i in range(10)]
Out[27]: [False, True, False, False, False, True, True, False, False, False]

And for the list of lists just nest that list comprehension:

对于列表列表只是嵌套列表理解:

In [34]: [[True if i in a1 else False for i in range(5)] for a1 in a]
Out[34]: [[False, True, False, True, False], [False, False, True, False, True]]

An array version is of this second case is:

第二种情况的阵列版本是:

In [41]: a = [[1,3],[4,2]]
In [42]: b = np.zeros((len(a),5), bool)
In [43]: b[[[0],[1]],a]
Out[43]: 
array([[False, False],
       [False, False]], dtype=bool)
In [44]: b[[[0],[1]],a]=True
In [45]: b
Out[45]: 
array([[False,  True, False,  True, False],
       [False, False,  True, False,  True]], dtype=bool)

This, though, will only work if the sublists of a are all the same length. If they differ, I think we'll have to work with a flattened version of this. In effect turn the 2d case into the original 1d.

但是,这只有在a的子列表长度相同时才有效。如果它们不同,我认为我们必须使用扁平版本。实际上将2d案例转换为原始1d。


For a ragged a, the list version is still easy:

对于衣衫褴褛的a,列表版本仍然很简单:

In [49]: a = [[1,2,3],[4,2],[1]]
In [50]: [[True if i in a1 else False for i in range(5)] for a1 in a]
Out[50]: 
[[False, True, True, True, False],
 [False, False, True, False, True],
 [False, True, False, False, False]]

But an array version is a bit more involved:

但阵列版本更多涉及:

Construct 2 arrays of indices, for rows and columns:

为行和列构造2个索引数组:

In [53]: a0 = np.repeat(np.arange(3),[len(i) for i in a])
In [54]: a0
Out[54]: array([0, 0, 0, 1, 1, 2])
In [55]: a1 = np.hstack(a)
In [56]: a1
Out[56]: array([1, 2, 3, 4, 2, 1])

Get the equivalent raveled (1d) indexing:

获得等效的raveled(1d)索引:

In [57]: np.ravel_multi_index((a0,a1),(3,5))
Out[57]: array([ 1,  2,  3,  9,  7, 11], dtype=int32)

Apply it to a 2d array via flat:

通过平板将其应用于2d阵列:

In [58]: b = np.zeros((3,5),bool)
In [59]: b.flat[Out[57]] = True
In [60]: b
Out[60]: 
array([[False,  True,  True,  True, False],
       [False, False,  True, False,  True],
       [False,  True, False, False, False]], dtype=bool)

#1


1  

For the first case:

对于第一种情况:

In [23]: a = [1,5,6]
In [24]: b = np.zeros(10, dtype=bool)
In [25]: b[a] = True
In [26]: b
Out[26]: array([False,  True, False, False, False,  True,  True, False, False, False], dtype=bool)

An equally simple, and possibly as fast, list version:

一个同样简单,可能同样快的列表版本:

In [27]: [True if i in a else False for i in range(10)]
Out[27]: [False, True, False, False, False, True, True, False, False, False]

And for the list of lists just nest that list comprehension:

对于列表列表只是嵌套列表理解:

In [34]: [[True if i in a1 else False for i in range(5)] for a1 in a]
Out[34]: [[False, True, False, True, False], [False, False, True, False, True]]

An array version is of this second case is:

第二种情况的阵列版本是:

In [41]: a = [[1,3],[4,2]]
In [42]: b = np.zeros((len(a),5), bool)
In [43]: b[[[0],[1]],a]
Out[43]: 
array([[False, False],
       [False, False]], dtype=bool)
In [44]: b[[[0],[1]],a]=True
In [45]: b
Out[45]: 
array([[False,  True, False,  True, False],
       [False, False,  True, False,  True]], dtype=bool)

This, though, will only work if the sublists of a are all the same length. If they differ, I think we'll have to work with a flattened version of this. In effect turn the 2d case into the original 1d.

但是,这只有在a的子列表长度相同时才有效。如果它们不同,我认为我们必须使用扁平版本。实际上将2d案例转换为原始1d。


For a ragged a, the list version is still easy:

对于衣衫褴褛的a,列表版本仍然很简单:

In [49]: a = [[1,2,3],[4,2],[1]]
In [50]: [[True if i in a1 else False for i in range(5)] for a1 in a]
Out[50]: 
[[False, True, True, True, False],
 [False, False, True, False, True],
 [False, True, False, False, False]]

But an array version is a bit more involved:

但阵列版本更多涉及:

Construct 2 arrays of indices, for rows and columns:

为行和列构造2个索引数组:

In [53]: a0 = np.repeat(np.arange(3),[len(i) for i in a])
In [54]: a0
Out[54]: array([0, 0, 0, 1, 1, 2])
In [55]: a1 = np.hstack(a)
In [56]: a1
Out[56]: array([1, 2, 3, 4, 2, 1])

Get the equivalent raveled (1d) indexing:

获得等效的raveled(1d)索引:

In [57]: np.ravel_multi_index((a0,a1),(3,5))
Out[57]: array([ 1,  2,  3,  9,  7, 11], dtype=int32)

Apply it to a 2d array via flat:

通过平板将其应用于2d阵列:

In [58]: b = np.zeros((3,5),bool)
In [59]: b.flat[Out[57]] = True
In [60]: b
Out[60]: 
array([[False,  True,  True,  True, False],
       [False, False,  True, False,  True],
       [False,  True, False, False, False]], dtype=bool)