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

How to create a Boolean array from an array of indices in numpy?

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

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

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

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

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

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

那么对于形状为 (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) 

第一种情况:

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)

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

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]

对于列表列表,只需嵌套列表理解:

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]]

第二种情况的数组版本是:

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)

不过,这仅在 a 的子列表长度相同时才有效。如果它们不同,我认为我们将不得不使用它的扁平化版本。实际上将 2d 的情况变成原来的 1d。


对于衣衫褴褛的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]]

但是数组版本有点复杂:

为行和列构造 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])

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

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

通过flat将其应用于二维数组:

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)

对于一维:

n = 10
inds_all = np.arange(n)
inds = [1, 5, 6]

b = np.isin(inds_all, inds)
# array([False, True, False, False, False, True, True, False, False, False])

对于 2D 它有点棘手,尤其是当列表索引列表的大小不均匀时:

n = 5
inds_all = np.arange(len(inds)*n).reshape(len(inds), n)
inds = [[1,3], [4,2], [3]]
inds = np.concatenate(inds) + np.repeat(inds_all[:,0], list(map(len, inds)))

b = np.isin(inds_all, inds)
# array([[False,  True, False,  True, False],
#        [False, False,  True, False,  True],
#        [False, False, False,  True, False]])