直接转换numpy的unit8原料阵列FLOAT32 [英] Convert numpy unit8 raw array directly to float32
问题描述
在我的情况,我得到一个大小为 numpy.ndarray
为 unit8
4 * ñ
从而重新presents FLOAT32
实体的原始二进制数据。因此,4个项目一起重新present有一个 FLOAT32
。要获得 FLOAT32
数字我现在转换 UINT8
原始数据为二进制字符串,不是从这个字符串<$ C阅读$ C> FLOAT32 值。
In my situation I get a numpy.ndarray
as unit8
of size 4 * n
which represents raw binary data of float32
entities. So 4 items together represent one float32
. To get float32
numbers I currently convert uint8
raw data to a binary string and than read from this string the float32
values.
np.fromstring(raw_unit8_data.tostring(), dtype='<f4')
是否有可能直接完成这种转换,而不 UINT8
数据转换为字符串第一?
Is there a possibility to do this conversion directly without converting the uint8
data to a string first?
推荐答案
您可以使用的 视图
有numpy的reinter preT的原始数据为适当的数据类型。例如:
You could use view
to have NumPy reinterpret the raw data as the appropriate data type. For example:
>>> raw_unit8_data = np.array([32, 14, 135, 241], dtype='uint8')
>>> raw_unit8_data.view('<f4')
array([ -1.33752168e+30], dtype=float32)
这有没有使用任何临时阵列或缓冲区的优势(我们只是改变了内存是如何读取),并给出了相同的值作为当前的方法:
This has the advantage of not using any temporary arrays or buffers (we're just changing how the memory is read) and gives the same values as your current method:
>>> np.fromstring(raw_unit8_data.tostring(), dtype='<f4')
array([ -1.33752168e+30], dtype=float32)
这篇关于直接转换numpy的unit8原料阵列FLOAT32的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!