NJU1healer的个人博客分享 http://blog.sciencenet.cn/u/NJU1healer

博文

[转载]numpy.frombuffer

已有 2327 次阅读 2020-8-20 20:52 |个人分类:Python|系统分类:科研笔记|文章来源:转载

numpy.frombuffer

numpy.frombuffer(bufferdtype=floatcount=-1offset=0)

Interpret a buffer as a 1-dimensional array.

Parameters:

buffer : buffer_like

An object that exposes the buffer interface.

dtype : data-type, optional

Data-type of the returned array; default: float.

count : int, optional

Number of items to read. -1 means all data in the buffer.

offset : int, optional

Start reading the buffer from this offset (in bytes); default: 0.

Notes

If the buffer has data that is not in machine byte-order, this should be specified as part of the data-type, e.g.:

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

The data of the resulting array will not be byteswapped, but will be interpreted correctly.

Examples

>>> s = ‘hello world‘
>>> np.frombuffer(s, dtype=‘S1‘, count=5, offset=6)
array([‘w‘, ‘o‘, ‘r‘, ‘l‘, ‘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)

NumPy的ndarray数组对象不能像list一样动态地改变其大小,在做数据采集时很不方便。本文介绍如何通过np.frombuffer()实现动态数组。

【参考】

https://blog.csdn.net/weixin_36670529/article/details/102668346



https://wap.sciencenet.cn/blog-3428464-1247195.html

上一篇:Python中reshape函数(-1表示什么)与数据白化
下一篇:Pickle模块的dump()方法和load()方法
收藏 IP: 211.162.81.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-30 05:57

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部