内存效率比努比。哪里?

2024-04-25 15:12:14 发布

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

我有一个大的数组(几百万个元素),我需要根据几个不同的标准切掉其中的一小部分(几百个)。我目前正在使用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])

然后使用ipts从各种阵列中提取正确的点。我对那件事记忆犹新np.哪里然而,这条线。我在其他几个相关的帖子上看到np.哪里可能是内存占用和复制数据。你知道吗

有多个&;在那里意味着数据被复制多次吗?有没有一种更有效的方法对数据进行切片,这种方法占用的内存更少,而且还保留了我想要的索引列表,以便以后可以在多个地方使用同一个切片?你知道吗

请注意,我发布的这个示例实际上并没有生成错误,但其结构与我的类似。你知道吗


Tags: 数据方法内存in元素for标准threshold
1条回答
网友
1楼 · 发布于 2024-04-25 15:12:14

在每个条件中,您都要创建一个临时布尔数组,其大小与xyz相同。要对此进行优化,可以迭代创建掩码:

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

对于本例,这会将内存使用量从160MB减少到40MB。你知道吗

相关问题 更多 >