访问不在给定索引列表中的NumPy数组元素

2024-04-25 00:01:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个形状为(100170256)的NumPy数组。我有一个由索引[0,10,20,40,70]组成的数组

我可以得到索引对应的子数组,如下所示:

sub_array = array[..., index]

这将返回一个具有预期形状(100170,5)的数组。现在,我尝试获取补码并得到与这些索引不对应的子数组。所以,我做到了:

sub_array = array[..., ~index]

出于某种原因,这仍然会返回一个形状数组(1001705)。我想知道如何在python中对这些索引进行这种补充操作

[编辑]

还尝试:

sub_array = array[..., not(index.any)]

但是,这并不能实现我想要的功能(获取形状数组(100170251)


Tags: 功能numpy编辑indexnotany数组array
3条回答

以获取数据的方式,最简单的方法是使用^{}

sub_array = np.delete(array, index, axis=2)

或者,您尝试使用的逻辑运算符可以应用于布尔数组,如@DSM所示:

mask = np.ones(a.shape[2], dtype=bool)
mask[index] = False
sub_array = array[:,:, mask]

(我不会称你的数组为array,但我遵循了你问题中的名称)

问题已经得到了回答,但我在这里提出了三种方法的基准

最快的解决方案是布尔掩码(索引数组大小较小且较大)

mask = np.ones(arr.size, dtype=bool)
mask[indexes] = False
result = arr[mask]

它比列表理解快2000倍,比np.delete稍快

要复制的代码

三个建议的解决方案:列表理解(sol1)、布尔掩码(sol2)或np.deletesol3

d = 100000
a = np.random.rand(d)
idx = np.random.randint(d, size = 10)


# list comprehension
def sol1(arr, indexes):
    return arr[[i for i in range(arr.size) if i not in indexes]]
sol1(a, idx)
# Out[30]: array([0.13044518, 0.68564961, 0.03033223, ..., 0.03796257, 0.40137137, 0.45403929])

# boolean mask
def sol2(arr, indexes):
    mask = np.ones(arr.size, dtype=bool)
    mask[indexes] = False
    return arr[mask]
sol2(a, idx)
# Out[32]: array([0.13044518, 0.68564961, 0.03033223, ..., 0.03796257, 0.40137137, 0.45403929])

# np.delete
def sol3(arr, indexes):
    return np.delete(arr, indexes)
sol3(a, idx)
# Out[36]: array([0.13044518, 0.68564961, 0.03033223, ..., 0.03796257, 0.40137137, 0.45403929])

结果


%timeit sol1(a, idx)
384 ms ± 2.75 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit sol2(a, idx)
154 µs ± 15.7 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit sol3(a, idx)
194 µs ± 18.8 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)


idx = np.random.randint(d, size = 1000)
%timeit sol1(a, idx)
386 ms ± 7.75 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit sol2(a, idx)
171 µs ± 11.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit sol3(a, idx)
205 µs ± 10.5 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

看看~index给了你什么——我想是:

array([ -1, -11, -21, -41, -71])

那么,你的电话

sub_array = array[..., ~index]

将返回5个条目,对应于索引[-1、-11、-21、-41、-71],即在您的情况下为255、245、235、215和185

类似地,not(index.any)给出

False

因此,为什么你的第二次尝试不起作用

这应该起作用:

sub_array = array[..., [i for i in xrange(256) if i not in index]]

相关问题 更多 >