我可以使用myndarray.tobytes()
将numpy ndarray转换为bytes。现在,我该如何将其转换回ndarray呢?
使用.tobytes()
方法文档中的示例:
>>> x = np.array([[0, 1], [2, 3]])
>>> bytes = x.tobytes()
>>> bytes
b'\x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00'
>>> np.some_magic_function_here(bytes)
array([[0, 1], [2, 3]])
np.frombuffer()
是 NumPy 库中用于从缓冲区创建数组的函数。它将缓冲区解释为包含指定数据类型的连续元素序列,并使用该序列创建一个新的NumPy数组对象。 - saschanp.fromstring
。在这两种情况下,如果不是默认的float
类型,您还需要指定dtype
。 - ali_m