numpy.ma.frombuffer

ma.frombuffer(buffer, dtype=float, count=- 1, offset=0, *, like=None) = <numpy.ma.core._convert2ma object>

将缓冲区解释为一维数组。

参数
bufferbuffer_like

公开缓冲区接口的对象。

dtype数据类型,可选

返回数组的数据类型;默认值:float。

count可选的

要读取的项目数。 -1 表示缓冲区中的所有数据。

offset可选的

从该偏移量开始读取缓冲区(以字节为单位);默认值:0。

likearray_like

引用对象以允许创建非NumPy数组的数组。如果像这样的数组传入为 like 支持 __array_function__ 协议,结果将由它定义。在这种情况下,它确保创建与通过此参数传入的对象兼容的数组对象。

注解

这个 like 关键字是一个实验性的特性,有待接受 NEP 35 .

1.20.0 新版功能.

笔记

如果缓冲区中的数据不是机器字节顺序,则应将其指定为数据类型的一部分,例如:

>>> dt = np.dtype(int)
>>> dt = dt.newbyteorder('>')
>>> np.frombuffer(buf, dtype=dt) 

结果数组的数据将不会被字节化,但将被正确解释。

实例

>>> s = b'hello world'
>>> np.frombuffer(s, dtype='S1', count=5, offset=6)
array([b'w', b'o', b'r', b'l', b'd'], dtype='|S1')
>>> np.frombuffer(b'\x01\x02', dtype=np.uint8)
array([1, 2], dtype=uint8)
>>> np.frombuffer(b'\x01\x02\x03\x04\x05', dtype=np.uint8, count=3)
array([1, 2, 3], dtype=uint8)