numpy.ma.frombuffer#
- ma.frombuffer(buffer, dtype=float, count=-1, offset=0, *, like=None)[源码]#
将缓冲区解释为一维数组。
- 参数:
- bufferbuffer_like
一个对象,它暴露了 buffer 接口。
- dtype数据类型,可选
返回数组的数据类型。默认是
numpy.float64。- countint, optional
要读取的项目数。
-1表示缓冲区中的所有数据。- offsetint, optional
从该偏移量(以字节为单位)开始读取缓冲区;默认值:0。
- likearray_like, optional
用于创建非 NumPy 数组的引荐对象。如果传入的
like支持__array_function__协议,则结果将由它定义。在这种情况下,它确保创建与通过此参数传入的数组兼容的数组对象。版本 1.20.0 中新增。
- 返回:
- out: MaskedArray
另请参阅
ndarray.tobytes此操作的逆向操作,从数组中的原始数据字节构建 Python 字节。
备注
如果缓冲区中的数据不是机器字节序,则应在数据类型中指定,例如:
>>> dt = np.dtype(int) >>> dt = dt.newbyteorder('>') >>> np.frombuffer(buf, dtype=dt)
结果数组的数据将不会被字节交换,但将被正确解释。
此函数会创建一个对原始对象的视图。这通常是安全的,但当原始对象是可变的或不受信任时,复制结果可能更有意义。
示例
>>> import numpy as np >>> 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)