我有一个较大的数组(数百万个元素),我需要根据几个不同的标准将其中的少数(几百个)切成薄片。我目前正在使用np.
for threshold in np.arange(0,1,.1):
x=np.random.random(5000000)
y=np.random.random(5000000)
z=np.random.random(5000000)
inds=np.where((x < threshold) & (y > threshold) & (z > threshold) & (z < threshold+0.1))
DoSomeJunk(a[inds], b[inds], c[inds])
,然后使用IPT从各个数组中提取正确的点。但是,我在那个NP处得到了MemoryError。我已经在其他几个相关的帖子上看到了NP。
确实有多个&amp;在那里意味着数据被多次复制?是否有一种更有效的方法将数据切成少的方式,而该数据的记忆密集型较少,也可以保留我想要的索引列表,以便以后可以在多个位置使用相同的切片?
请注意,我发布的此示例实际上并没有生成错误,但是结构与我拥有的相似。
在您创建的每个条件下,大小与x
,y
和z
相同的大小。为了优化它,您可以迭代创建掩码:
for threshold in np.arange(0,1,.1):
x=np.random.random(5000000)
y=np.random.random(5000000)
z=np.random.random(5000000)
inds = x < threshold
inds &= y > threshold
inds &= z > threshold
inds &= z < threshold+0.1
DoSomeJunk(a[inds], b[inds], c[inds])
在此示例中,这将使内存使用率从160 MB减少到40 MB。