有效地对大型图像数据矩阵求和

Swo*_*poo 3 python numpy

如何最有效地对大文件求和(100, 1024, 1024) .npz?有没有更好的文件格式来存储给定维度的Python数据?

我目前用它来对矩阵求和:

summed_matrix = np.sum([np.load(file) for file in files_list])
Run Code Online (Sandbox Code Playgroud)

这超出了我的可用内存。

abe*_*502 7

目前,您一次加载所有文件,然后才开始求和。如果内存是一个问题,更好的方法是一次读取并添加一个:

summed_matrix = np.zeros((100, 1024, 1024))

for file in files_list:
    summed_matrix += np.load(file)
Run Code Online (Sandbox Code Playgroud)


moz*_*way 5

functools.reduce您可以尝试使用和生成器numpy.add:

from functools import reduce

summed_matrix = reduce(np.add, (np.load(file) for file in files_list))

# or functional-style
summed_matrix = reduce(np.add, map(np.load, files_list))
Run Code Online (Sandbox Code Playgroud)

  • @JérômeRichard 我实际上是在设置一个最小的示例来仔细检查这一点。然后需要使用 `functools.reduce` 来代替 (2认同)