从压缩字节加载 numpy 数组



我正在尝试将(使用savez_compressed(一堆numpy数组保存到BytesIO对象中,然后将它们加载回变量。到目前为止,我可以使用以下代码保存数组

# Arrays
a = numpy.random.uniform(size=(10,10)) # dtype is float64
b = numpy.random.uniform(size=(10,10)) # dtype is float64
# Create bytes object
buf = io.BytesIO()
# Save the arrays into the buffer
numpy.savez_compressed(buf, a=a, b=b)
# Closed the buffer
buf.close()

我一直在尝试不同的方法来加载它们。例如

ab = numpy.frombuffer(buf.read(), dtype='float64')

这提高了ValueError: buffer size must be a multiple of element size.并尝试像文件一样加载

ab = numpy.load(buf)

提高ValueError: Cannot load file containing pickled data when allow_pickle=False,当buf.read()我得到ValueError: embedded null byte.

以下内容对我有用 [ Numpy version: numpy==1.16.1 ]:

import numpy
import io
# Arrays
a = numpy.random.uniform(size=(10,10)) # dtype is float64
b = numpy.random.uniform(size=(10,10)) # dtype is float64
# Create bytes object
buf = io.BytesIO()
# Save the arrays into the buffer
numpy.savez_compressed(buf, a=a, b=b)
buf.seek(0)
ab = numpy.load(buf)
print(ab['a'])
print(ab['b'])
# Closed the buffer
buf.close()

相关内容

  • 没有找到相关文章

最新更新