簡體   English   中英

比 numpy.where 更節省內存的選項?

[英]More memory efficient option than numpy.where?

我有一個大數組(幾百萬個元素),我需要根據幾個不同的標准切出其中的一小部分(幾百個)。 我目前正在使用 np.where,沿着以下路線:

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.where 行上收到 MemoryError 。 我在其他幾個相關帖子中看到 np.where 可能是內存占用和復制數據。

有多個 & 是否意味着數據被多次復制? 有沒有一種更有效的方式來切片數據,以減少內存密集度,同時保留我想要的索引列表,以便我以后可以在多個地方使用同一個切片?

請注意,我發布的這個示例實際上並沒有產生錯誤,但結構與我所擁有的相似。

在每個條件中,您都在創建一個與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])

對於此示例,這會將內存使用量從 160 MB 減少到 40 MB。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM